Find The Multiple(bfs)

时间:2021-05-03 16:58:41

Find The Multiple

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 43   Accepted Submission(s) : 21
Special Judge
Problem 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
 
Sample Output
10
100100100100100100
111111111111111111
 
Source
PKU

题意:构造一个十进制由0和1组成的整数m,让m能够被n整除;题目中给出了m是小于等于100位的。

思路:bfs可以做出的,只是100位这个条件让我很头疼,我先用string试了试交了之后是超时,后来看了看答案用long long也给过了,所以m肯定没有100位的;

string类型代码:

Find The Multiple(bfs)Find The Multiple(bfs)
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; queue<string>s;
int a,c;
string b; int chu(string s)
{
c=;
for(int i=;i<s.length();i++){
c*=;
c+=s[i]-'';
c%=a;
}
if(c==)
return ;
else
return ;
} int bfs()
{
while(!s.empty()){
b=s.front();
s.pop();
if(b.length()>)
continue;
if(chu(b+'')==){
cout<<b+''<<endl;
while(!s.empty()){
s.pop();
}
return ;
}
else{
s.push(b+'');
} if(chu(b+'')==){
cout<<b+''<<endl;
while(!s.empty()){
s.pop();
}
return ;
}
else{
s.push(b+'');
}
}
return ;
} int main()
{
// freopen("input.txt","r",stdin);
while(cin>>a){
if(a==)
break;
else{
s.push("");
bfs();
}
}
return ;
}

long long类型代码:

Find The Multiple(bfs)Find The Multiple(bfs)
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; queue<long long>s;
int a,c;
long long b; int bfs()
{
while(!s.empty()){
b=s.front();
s.pop();
if(b%a==){
cout<<b<<endl;
while(!s.empty()){
s.pop();
}
return ;
}
else{
s.push(b*);
s.push(b*+);
}
}
return ;
} int main()
{
// freopen("input.txt","r",stdin);
while(cin>>a){
if(a==)
break;
else{
s.push();
bfs();
}
}
return ;
}