poj1426 宽搜

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.

考试的时候一看200位直接就被吓到了,直接跳过,后来听说答案开long long 可过,就是普通宽搜,感觉亏了一个亿,既然每一位不是0就是1,首先第一位一定是1,然后每次这个数乘10,或者乘10加1,就是可以产生每一位都是1或者0的数字了。

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #define ll long long
 5 using namespace std;
 6 int n;
 7 queue<long long>q;
 8 ll ans;
 9 int main()
10 {
11     while(1)
12     {
13         cin>>n;
14         if(!n)break;
15         while(!q.empty())q.pop();
16         q.push(1);
17         while(!q.empty())
18         {
19             ll k=q.front();q.pop();
20             if(k%n==0)
21             {
22                 ans=k;break;
23             }
24             q.push(k*10),q.push(k*10+1);
25         }
26         printf("%lld
",ans);
27     }
28 
29     return 0;
30 }
原文地址:https://www.cnblogs.com/yuelian/p/12565294.html