POJ 1426 BFS

Find The Multiple
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 35790   Accepted: 14957   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

2
6
19
0

Sample Output

10
100100100100100100
111111111111111111

Source

题意:给定一个数,找到由一个01串使得是它的倍数。

分析:直接bfs每次加0加1,同时每次取模,判断是不是为0就可以了。

代码:

 1 ////#include "bits/stdc++.h"
 2 #include "cstdio"
 3 #include "map"
 4 #include "set"
 5 #include "cmath"
 6 #include "queue"
 7 #include "vector"
 8 #include "string"
 9 #include "cstring"
10 #include "time.h"
11 #include "iostream"
12 #include "stdlib.h"
13 #include "algorithm"
14 #define db double
15 #define ll long long
16 #define vec vector<ll>
17 #define Mt  vector<vec>
18 #define ci(x) scanf("%d",&x)
19 #define cd(x) scanf("%lf",&x)
20 #define cl(x) scanf("%lld",&x)
21 #define pi(x) printf("%d\n",x)
22 #define pd(x) printf("%f\n",x)
23 #define pl(x) printf("%lld\n",x)
24 #define rep(i, x, y) for(int i=x;i<=y;i++)
25 const int N   = 1e6 + 5;
26 const int mod = 1e9 + 7;
27 const int MOD = mod - 1;
28 const db  eps = 1e-18;
29 const db  PI  = acos(-1.0);
30 using namespace std;
31 int n;
32 struct P
33 {
34     int s[20];
35     int ans,cnt;
36 };
37 bool v[205];
38 //int R()
39 //{
40 //    int x=0,f=1;char ch=getchar();
41 //    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
42 //    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
43 //    return x*f;
44 //}
45 inline void bfs(int x)
46 {
47     queue<P> q;
48     P p;
49     memset(p.s,0,sizeof(p.s));
50     p.s[0]=1;
51     p.cnt=1;
52     p.ans=1;
53     v[p.ans]=1;
54     q.push(p);
55     while(q.size())
56     {
57         P p1=q.front();
58         q.pop();
59         if(!p1.ans){
60             for(int i=0;i<p1.cnt;i++)
61                 printf("%d",p1.s[i]);
62             puts("");
63             return;
64         }
65         for(int i=0;i<=1;i++){
66             P p2;
67             for(int ii=0;ii<p1.cnt;ii++) p2.s[ii]=p1.s[ii];
68             p2.cnt=p1.cnt,p2.ans=p1.ans;
69             p2.ans=(p2.ans*10+i)%x;
70             if(v[p2.ans]==1) continue;//限制队列中元素的个数,否则会MLE
71             v[p2.ans]=1;
72             p2.s[p2.cnt++]=i;
73             q.push(p2);
74         }
75     }
76 }
77 int main()
78 {
79     while(scanf("%d",&n)==1&&n)
80     {
81         memset(v,0, sizeof(v));
82         bfs(n);
83     }
84 }

 

posted @ 2017-12-08 21:29  thges  阅读(194)  评论(0编辑  收藏  举报