Find The Multiple (poj 1426 bfs)
2014-10-20 22:43
405 查看
Language: Default Find The Multiple
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 Dhaka 2002 |
思路:运用同余定理+bfs,看这位大神这一题的同余定理。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define maxn 1005 #define MAXN 2005 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 typedef long long ll; using namespace std; struct Node { int num; int mod; Node *pre;//记录路径 }; int N; int visit[maxn]; void print(Node *x)//递归输出路径 { if (x->pre==NULL) printf("%d",x->num); else { print(x->pre); printf("%d",x->num); } } void bfs() { int i; queue<Node>Q; memset(visit,0,sizeof(visit)); Node sst,*now=new Node; visit[1]=1; while (!Q.empty()) Q.pop(); sst.mod=1;sst.num=1;sst.pre=NULL; Q.push(sst); while (!Q.empty()) { Node *st=new Node; *st=Q.front(); Q.pop(); if (st->mod==0) { print(st); printf("\n"); return ; } for (i=0;i<2;i++) { int xx=(st->mod*10+i)%N; if (!visit[xx]) { now->mod=xx; now->num=i; now->pre=st; Q.push(*now); visit[xx]=1; } } } return ; } int main() { while (scanf("%d",&N)&&N) { if (N==1) { printf("1\n"); continue; } bfs(); } return 0; }
相关文章推荐
- POJ1426-Find The Multiple (BFS 余数)
- poj 1426 Find The Multiple (bfs 搜索)
- POJ 1426 Find The Multiple(BFS)
- POJ1426 Find The Multiple(bfs)
- poj 1426 Find The Multiple(bfs)
- poj 1426 Find The Multiple( bfs )
- POJ-1426-Find The Multiple【BFS】
- poj 1426 Find The Multiple (BFS)
- POJ - 1426 Find The Multiple(15.10.10 搜索专题)bfs
- POJ_1426_Find The Multiple(BFS)
- poj - 1426-Find The Multiple-BFS
- poj1426 - Find The Multiple [bfs 记录路径]
- POJ1426——BFS——Find The Multiple
- poj 1426 Find The Multiple(bfs)
- POJ训练计划1426_Find The Multiple(BFS)
- Find The Multiple (poj 1426 bfs)
- POJ 1426 Find The Multiple(BFS 水~)
- POJ 1426 Find The Multiple --- BFS || DFS
- POJ 1426 Find The Multiple (bfs搜索)
- DFS/BFS(同余模) POJ 1426 Find The Multiple