首页 > 代码库 > poj 1426 Find The Multiple(bfs)
poj 1426 Find The Multiple(bfs)
Find The Multiple
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 Dhaka 2002 |
[Submit] [Go Back] [Status] [Discuss]
Home Page Go Back To top
题意:
给你一个n,求一个只由1和0组成的十进制数对n取摸为0。
CODE:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<algorithm> #include<cstdlib> #include<set> #include<queue> #include<stack> #include<vector> #include<map> #define N 100010 #define Mod 10000007 #define lson l,mid,idx<<1 #define rson mid+1,r,idx<<1|1 #define lc idx<<1 #define rc idx<<1|1 const double EPS = 1e-11; const double PI = acos ( -1.0 ); const double E = 2.718281828; typedef long long ll; const int INF = 1000010; using namespace std; int n; struct node { string s; int mod;//保留前一个的摸值 }; bool vis[222]; queue<node>que; void bfs() { while(que.size()) que.pop(); memset(vis,0,sizeof vis); node a,t; a.mod=1; a.s="1"; que.push(a); vis[1]=1; while(que.size()) { a=que.front(); que.pop(); if(a.mod==0) { cout<<a.s<<endl; return; } for(int i=0; i<2; i++) { t.mod=a.mod*10+i; t.mod%=n; if(!vis[t.mod]) { if(i) t.s=a.s+"1"; else t.s=a.s+"0"; que.push(t); vis[t.mod]=1; } } } } int main() { while(cin>>n&&n) { bfs(); } return 0; }
poj 1426 Find The Multiple(bfs)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。