fork(3) download
  1. //INUMBER
  2. #include <cstdio>
  3. #include <queue>
  4. #include <utility>
  5. #include <string>
  6. #include <algorithm>
  7. #include <ctype.h>
  8. #include <iostream>
  9.  
  10. #define MAX 1000
  11.  
  12. using namespace std;
  13.  
  14. int n;
  15.  
  16. string lngstr(string z, int j)
  17. {
  18. string tmp;
  19. char c[2]; c[0] = '0' + j; c[1] = '\0';
  20. tmp = z + c;
  21. return tmp;
  22. }
  23.  
  24. void bfs()
  25. {
  26. pair < pair <int, int>, string> tmp;
  27. queue < pair < pair <int,int >, string > > q;
  28. tmp.first.first = tmp.first.second = 0;
  29. tmp.second = "";
  30. q.push(tmp);
  31. bool vis[MAX+1][MAX+1] = {0};
  32. while(!q.empty())
  33. {
  34. tmp = q.front();
  35. q.pop();
  36. int x,y;
  37. string z;
  38. x = tmp.first.first; y = tmp.first.second; z=tmp.second;
  39. if(x == n && y == 0)
  40. {
  41. printf("%s\n",z.c_str());
  42. break;
  43. }
  44. if(vis[x][y]) continue;
  45. vis[x][y] = true;
  46. for(int j=0; j<=9; j++)
  47. {
  48. q.push(make_pair(make_pair(x + j, (y*10 + j)%n), lngstr(z,j)));
  49. }
  50. }
  51. return ;
  52. }
  53. int main()
  54. {
  55. int t;
  56. //freopen("input.txt", "r", stdin);
  57. scanf("%d",&t);
  58. while(t--)
  59. {
  60. scanf("%d",&n);
  61. bfs();
  62. }
  63. }
  64.  
Success #stdin #stdout 0.79s 4328KB
stdin
5
67
197
20
50
999
stdout
58999999
19998999999999999999998
3980
9999950
999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999