1495. One-two, One-two 2

Myth丶恋晨 2022-01-05 15:37 310阅读 0赞

1495. One-two, One-two 2

Time limit: 2.0 second Memory limit: 64 MB

A year ago the famous gangster Vito Maretti woke up in the morning and realized that he was bored of robbing banks of round sums. And for the last year he has been taking from banks sums that have only digits 1 and 2 in their decimal notation. After each robbery, Vito divides the money between N members of his gang. Your task is to determine the minimal stolen sum which is a multiple of N.

Input

The input contains the number N (1 ≤ N ≤ 10 6).

Output

Output the minimal number which is a multiple of N and whose decimal notation contains only digits 1 and 2. If it contains more than 30 digits or if there are no such numbers, then output “Impossible”.

Samples
















input output
  1. 5
  1. Impossible
  1. 8
  1. 112

Problem Author: Igor Chevdar Problem Source: XIII-th USU Junior Contest, October 2006

***************************************************************************************

广搜优化(需要对 数 的感应能力)

***************************************************************************************

ContractedBlock.gif ExpandedBlockStart.gif

  1. 1 #include<iostream>
  2. 2 #include<string>
  3. 3 #include<cstring>
  4. 4 #include<cmath>
  5. 5 #include<cstdio>
  6. 6 #include<vector>
  7. 7 #include<queue>
  8. 8 #include<algorithm>
  9. 9 #define LL long long
  10. 10 using namespace std;
  11. 11 struct que
  12. 12 {
  13. 13 int L,k;//k记录数,L记录长度
  14. 14 int mod,f;//mod记录余数,f记录前驱
  15. 15 }q[1000001];
  16. 16 int had[1000001];//记录余数防止余数重
  17. 17 int mod1,mod2;//记录取1和2时的余数
  18. 18 int I,J;//如果余数出现循环时说明无解(两个指针)
  19. 19 int n,m,k,t;
  20. 20 int gs;
  21. 21 void bfs()
  22. 22 {
  23. 23 while(I<J)
  24. 24 {
  25. 25 int L=q[I].L;
  26. 26 if(L==30){
  27. return;I++;}
  28. 27 int mod=q[I].mod;
  29. 28 mod1=(mod*10+1)%n;
  30. 29 mod2=(mod*10+2)%n;
  31. 30
  32. 31 if(had[mod1]==false)
  33. 32 {
  34. 33 had[mod1]=true;
  35. 34 q[J].f=I;q[J].k=1;q[J].mod=mod1;
  36. 35 q[J].L=L+1;
  37. 36 if(mod1==0)
  38. 37 {
  39. 38 gs=J;
  40. 39 return;
  41. 40 }
  42. 41 ++J;
  43. 42 }
  44. 43 if(had[mod2]==false)
  45. 44 {
  46. 45 had[mod2]=true;
  47. 46 q[J].f=I;q[J].k=2;q[J].mod=mod2;
  48. 47 q[J].L=L+1;
  49. 48 if(mod2==0)
  50. 49 {
  51. 50 gs=J;
  52. 51 return;
  53. 52 }
  54. 53 ++J;
  55. 54 }
  56. 55 I++;
  57. 56 }
  58. 57 }
  59. 58 void print(int k)
  60. 59 {
  61. 60 if(q[k].L>1)
  62. 61 print(q[k].f);
  63. 62 cout<<q[k].k;
  64. 63 }
  65. 64 int main()
  66. 65 {
  67. 66 cin>>n;
  68. 67 if(n==1||n==2)
  69. 68 {
  70. 69 cout<<n<<endl;
  71. 70 return 0;
  72. 71 }
  73. 72 memset(had,false,sizeof(had));//初始化
  74. 73 I=J=0;
  75. 74 q[J].L=1;q[J].k=1;q[J].mod=1;++J;
  76. 75 q[J].L=1;q[J].k=2;q[J].mod=2;++J;
  77. 76 had[1]=had[2]=true;
  78. 77 gs=-1;
  79. 78 bfs();
  80. 79 if(gs==-1)
  81. 80 cout<<"Impossible"<<endl;
  82. 81 else
  83. 82 {
  84. 83 print(gs);
  85. 84 cout<<endl;
  86. 85 }
  87. 86 return 0;
  88. 87 }

转载于:https://www.cnblogs.com/sdau--codeants/p/3260669.html

发表评论

表情:
评论列表 (有 0 条评论,310人围观)

还没有评论,来说两句吧...

相关阅读