题目描述:

n属于1到200,找到对应的一个数只含有0和1,并且是n的倍数;

分析:

本题有几个数会是大数;所以要考虑大数;

用到余数的性质;例如n=6,1%6=1;

1*10%6=4;              (1*10+1)%6=5;

4*10%6=4;               (4*10+1)%6=5;

5*10%6=2;                (5*10+1)%6=3;

(重复4,5)

2*10%6=2;                  。。。。=3;

3*10%6=0;

这时候发现余数为0,说明这个数可以是6的倍数;倒退回去,数分别是1,10,11,100,101,110,111,。。。。1110;

可以发现余数是一样的,同余定理;

(a*b)%n = (a%n *b%n)%n

(a+b)%n = (a%n +b%n)%n

由同余模定理  (110*10+1)%6 = ((110*10)%6+1%6 )%6 = ((110%6 * 10%6)%6 +1 )%6;

 

用这个同余定理就可以解决大数问题了;然后就是记录路径,这里就是巧妙的地方;我还不太清除是怎么搞的,总之就是一共进行了k次操作,就相当于01全排列,首项是1,然后排到第一个符合的数的时候,这个数是第几个,它对应的二进制就是相应的串;这一题用bfs居然超时了;所以我打了个表,有一个不打表的做法;

代码如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<queue>
 6 using namespace std;
 7 
 8 queue<int> q;
 9 int n;
10 char a[205][200]={"1","10","111","100","10","1110","1001","1000","111111111","10","11","11100","1001","10010","1110","10000","11101","1111111110","11001","100","10101","110","110101","111000","100","10010","1101111111","100100","1101101","1110","111011","100000","111111","111010","10010","11111111100","111","110010","10101","1000","11111","101010","1101101","1100","1111111110","1101010","10011","1110000","1100001","100","100011","100100","100011","11011111110","110","1001000","11001","11011010","11011111","11100","100101","1110110","1111011111","1000000","10010","1111110","1101011","1110100","10000101","10010","10011","111111111000","10001","1110","11100","1100100","1001","101010","10010011","10000","1111111101","111110","101011","1010100","111010","11011010","11010111","11000","11010101","1111111110","1001","11010100","10000011","100110","110010","11100000","11100001","11000010","111111111111111111","100","101","1000110","11100001","1001000","101010","1000110","100010011","110111111100","1001010111","110","111","10010000","1011011","110010","1101010","110110100","10101111111","110111110","100111011","111000","11011","1001010","10001100111","11101100","1000","11110111110","11010011","10000000","100100001","10010","101001","11111100","11101111","11010110","11011111110","11101000","10001","100001010","110110101","100100","10011","100110","1001","1111111110000","11011010","100010","1100001","11100","110111","11100","1110001","11001000","10111110111","10010","1110110","1010100","10101101011","100100110","100011","100000","11101111","11111111010","1010111","1111100","1111110","1010110","11111011","10101000","10111101","111010","1111011111","110110100","1011001101","110101110","100100","110000","100101111","110101010","11010111","11111111100","1001111","10010","100101","110101000","1110","100000110","1001011","1001100","1010111010111","110010","11101111","111000000","11001","111000010","101010","110000100","1101000101","1111111111111111110","111000011","1000"};
11 /*
12 int cou=0;
13 void bfs()
14 {
15     while(!q.empty())
16     {
17         int t=q.front();
18         q.pop();
19             cou++;
20         if(t%n==0)
21             return;
22         q.push(t*10%n);
23         q.push((t*10+1)%n);
24     }
25 }
26 int main()
27 {
28     freopen("out","w",stdout);
29     for(int z=1;z<=200;z++)
30     {
31         n=z;
32         while(!q.empty())
33             q.pop();
34         memset(a,0,sizeof(a));
35         cou=0;
36         a[0]=1;
37        q.push(1);
38        bfs();
39        int i=0;
40        while(cou)
41        {
42            a[i++]=cou%2;
43            cou=cou/2;
44        }
45        printf("\"");
46        for(int j=i-1;j>=0;j--)
47        {
48            printf("%d",a[j]);
49        }
50        printf("\",");
51     }
52     return 0;
53 }*/
54 
55 int main()
56 {
57     while(cin>>n&&n)
58     {
59         printf("%s\n",a[n-1]);
60     }
61     return 0;
62 }
View Code

相关文章:

  • 2021-06-23
  • 2022-12-23
  • 2022-02-05
  • 2021-07-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-27
  • 2022-12-23
  • 2022-12-23
  • 2021-12-07
  • 2022-12-23
  • 2021-12-11
  • 2021-07-20
相关资源
相似解决方案