poj-1426 Find The Multiple

绝地灬酷狼 2022-06-01 01:17 265阅读 0赞

Find The Multiple
















Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18390   Accepted: 7445   Special Judge

Description

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.

Input

The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.

Output

For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.

Sample Input

?








1

2

3

4

2

6

19

0

Sample Output

?








1

2

3

10

100100100100100100

111111111111111111

题意:找一个大于等于N的数,使这个数每一位只有0和1,且能被N整除

同余模定理:找一个大于等于N的数,使这个数每一位只有0和1,且能被N整除

代码:1

  1. #include<stdio.h>
  2. #define N 600000
  3. int mod[N];
  4. int ans[200];
  5. int main()
  6. {
  7. int i,k,n;
  8. while(scanf("%d",&n),n)
  9. {
  10. mod[1]=1%n;
  11. for(i=2;mod[i-1]!=0;i++)
  12. {
  13. mod[i]=(mod[i/2]*10+i%2)%n;
  14. }
  15. i--;
  16. k=0;
  17. while(i)
  18. {
  19. ans[k++]=i%2;
  20. i/=2;
  21. }
  22. for(i=k-1;i>=0;i--)
  23. printf("%d",ans[i]);
  24. puts("");
  25. }
  26. return 0;
  27. }
  28. 代码:2
  29. #include<stdio.h>
  30. #include<math.h>
  31. #include<string.h>
  32. #include<algorithm>
  33. using namespace std;
  34. int n,flag;
  35. void f( unsigned __int64 x,int n,int k )
  36. {
  37. if(flag)
  38. return ;
  39. if(x%n==0)
  40. {
  41. printf("%I64u\n",x);
  42. flag=1;
  43. return ;
  44. }
  45. if(k==19)
  46. return ;
  47. f(x*10,n,k+1);
  48. f(x*10+1,n,k+1);
  49. }
  50. int main()
  51. {
  52. while(scanf("%d",&n)!=EOF)
  53. {
  54. if(n==0)
  55. break;
  56. flag=0;
  57. f(1,n,0);
  58. }
  59. }

发表评论

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

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

相关阅读