Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2015
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long int LL;
  5.  
  6. #define inp_s ios_base::sync_with_stdio(false)
  7. #define DRT() int test_case;cin>>test_case;while(test_case--)
  8.  
  9. #define VI vector<int>
  10. #define VS vector<string>
  11. #define VLL vector<LL>
  12. #define PII pair<LL,LL>
  13. #define all(c) c.begin(),c.end()
  14. #define sz(c) c.size()
  15. #define clr(c) c.clear()
  16. #define msi map<string,int>
  17. #define msit map<string,int>::iterator
  18. #define pb push_back
  19. #define mp make_pair
  20.  
  21. #define GI(x) scanf("%d",&x)
  22.  
  23. #define FOR(i,a,b) for(int i=a;i<b;i++)
  24. #define RFOR(i,a,b) for(int i=b-1;i>=a;i--)
  25.  
  26. #define gcd(a,b) __gcd(a,b)
  27. #define MOD 1000000007
  28. #define EPS 1E-10
  29. #define ELR 2.71828182845904523536
  30. #define PI acos(-1)
  31.  
  32. #define CASE(x) cout<<"Case "<<x<<": ";
  33.  
  34. typedef struct
  35. {
  36. string s;
  37. int mod;
  38. int sod;
  39. } node;
  40.  
  41. node getNode(string ans,int mod,int sod)
  42. {
  43. node ret;
  44. ret.s = ans;
  45. ret.mod = mod;
  46. ret.sod = sod;
  47. return ret;
  48. }
  49.  
  50. int main()
  51. {
  52. inp_s;
  53. DRT()
  54. {
  55. int n;
  56. cin >> n;
  57. queue<node> bfs;
  58. bfs.push(getNode("",0,0));
  59.  
  60. int visited[n][n+1];
  61. FOR(i,0,n) FOR(j,0,n+1) visited[i][j] = 0;
  62. visited[0][0] = 1;
  63.  
  64. while(!bfs.empty())
  65. {
  66. node top = bfs.front();
  67. bfs.pop();
  68.  
  69. if(top.mod == 0 && top.sod == n)
  70. {
  71. cout << top.s << endl;
  72. break;
  73. }
  74.  
  75. int lwr = 1;
  76. if(top.sod) lwr = 0;
  77.  
  78. FOR(i,lwr,10)
  79. {
  80. int nmod = (top.mod * 10 + i) % n;
  81. int nsod = (top.sod + i);
  82. if(nsod <= n && !visited[nmod][nsod])
  83. {
  84. visited[nmod][nsod] = 1;
  85. string q = top.s + (char)('0' + i);
  86. bfs.push(getNode(q,nmod,nsod));
  87. }
  88. }
  89. }
  90. }
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement