POJ-1426 Find The Multiple(BFS)
原创
©著作权归作者所有:来自51CTO博客作者MichaelZona的原创作品,请联系作者获取转载授权,否则将追究法律责任
Find The Multiple
Time Limit: 1000MS |
| Memory Limit: 10000K |
Total Submissions: 64606 |
| Accepted: 26229 |
| 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
Sample Output
10
100100100100100100
111111111111111111
Source
Dhaka 2002
一开始以为是一道数学题,做了以后发现似乎并没有什么特别的规律,所以这里其实还是用的二进制枚举,值得注意的是由于空间范围限制的非常小,所以结构体里面存数字用的是bool形式存的。
1 #include <cstdio>
2 #include <cmath>
3 #include <cstring>
4 #include <cstdlib>
5 #include <queue>
6 #include <stack>
7 #include <vector>
8 #include <iostream>
9 #include "algorithm"
10 using namespace std;
11 const int MAX=21;
12 int n;
13 struct Num{
14 bool num[MAX];
15 int len;
16 int mod;
17 }zt,tt;
18 queue <Num> q;
19 int main(){
20 freopen ("multiple.in","r",stdin);
21 freopen ("multiple.out","w",stdout);
22 int i,j;
23 while (scanf("%d",&n),n!=0){
24 while (!q.empty()) q.pop();
25 zt.len=0;
26 zt.num[++zt.len]=1;zt.mod=1%n;
27 q.push(zt);
28 while (!q.empty()){
29 zt=q.front();
30 if (zt.mod==0) break;
31 q.pop();
32 tt=zt;
33 tt.num[++tt.len]=1;tt.mod=(tt.mod*10+1)%n;
34 q.push(tt);
35 tt=zt;
36 tt.num[++tt.len]=0;tt.mod=(tt.mod*10)%n;
37 q.push(tt);
38 }
39 for (i=1;i<=zt.len;i++)
40 printf("%d",zt.num[i]);
41 printf("\n");
42 }
43 return 0;
44