fork(6) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define MAX 20005
  4. int value[MAX];
  5. int parent[MAX];
  6.  
  7. void bfs(int n){
  8. queue<int> q;
  9. q.push(1);
  10. parent[1] = 0;
  11. while(!q.empty()){
  12. int current = q.front();
  13. q.pop();
  14. if(current == 0){
  15. stack<int> st;
  16. while(parent[current]){
  17. st.push(value[current]);
  18. current = parent[current];
  19. }
  20. st.push(1);
  21. while(!st.empty()){
  22. cout<<st.top();
  23. st.pop();
  24. }
  25. cout<<endl;
  26. break;
  27. }
  28. int temp = (current*10)%n;
  29. if(parent[temp]==-1){
  30. q.push(temp);
  31. parent[temp] = current;
  32. value[temp] = 0;
  33. }
  34.  
  35. temp = (current*10)+1;
  36. temp = temp%n;
  37. if(parent[temp]==-1){
  38. q.push(temp);
  39. parent[temp] = current;
  40. value[temp] = 1;
  41. }
  42. }
  43. }
  44.  
  45. int main(){
  46.  
  47.  
  48. ios_base::sync_with_stdio(false);
  49. #ifndef ONLINE_JUDGE
  50. freopen("input.in","r",stdin);
  51. freopen("output.out","w",stdout);
  52. #endif
  53.  
  54. int t,n;
  55. cin>>t;
  56. while(t--){
  57. cin>>n;
  58. for(int i=0;i<=n;i++)
  59. parent[i] = -1;
  60. bfs(n);
  61. }
  62. return 0;
  63. }
Success #stdin #stdout 0s 3624KB
stdin
3
17
11011
15
stdout
11101
11011
1110