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
给出一个数n,要求找到它的一个倍数,使这个倍数仅由0和1组成,一开始看到丝毫没有思路,但仔细分析,可以得到:
对于一个数,如果其仅由0,1组成,那么在扩展这个数的时候,只能在其末尾添加0或1,换成十进制,其实就是乘10或者乘10加1。
所以这就是我们求解的思路,比较容易想到BFS,但是需要注意,在使用BFS的时候,实际上相当于构造了一颗二叉树,每个父节点都有两个子节点,而且,停止的唯一条件就是找到答案,那么节点的数量会呈指数级增长,这时候自己写队列会导致空间不足,用STL又会超时。
考虑到本题是special judge,也就是说结果不唯一,那么使用DFS能有效控制计算的次数,虽然答案并不是最短的。递归出口有两个,一个是找到答案,另一个是位数超过20的时候。
其实这是一类很特殊的题目,数据范围很小,数据类型简单,而且输入与输出有直接的关系,所以可以采用Doubi mode:超级无敌打表法!!!
首先用哪个BFS的程序算出1到200所有的答案,然后直接复制粘贴输出,估计没有比这更简单更快的了
BFS程序 超时
#include <iostream>
#include <queue>
using namespace std;
unsigned long long num;
int n;
bool flag;
void bfs()
{
queue<unsigned long long> q;
unsigned long long temp;
q.push(1);
while(1)
{
temp=q.front();
q.pop();
if(temp%n==0)
{
num=temp;
break;
}
q.push(temp*10);
q.push((temp*10+1));
}
}
int main()
{
n=1;
while(n<=200)
{
flag=false;
num=0;
bfs();
cout<<num<<","<<endl;
n++;
}
return 0;
}
DFS程序 AC
#include <iostream>
using namespace std;
unsigned long long num;
int n;
bool flag;
void dfs(unsigned long long temp,int k)
{
if((temp%n)==0)
{
num=temp;
flag=true;
return;
}
if(k>=20)
{
return;
}
if(flag==false)
{
dfs(temp*10,k+1);
dfs(temp*10+1,k+1);
}
}
int main()
{
while(cin>>n,n)
{
flag=false;
num=0;
dfs(1,1);
cout<<num<<endl;
}
return 0;
}
打表程序 AC
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <iomanip>
#include <algorithm>
#include <memory.h>
using namespace std;
unsigned long long num[]={1,
10,
111,
100,
10,
1110,
1001,
1000,
111111111,
10,
11,
11100,
1001,
10010,
1110,
10000,
11101,
1111111110,
11001,
100,
10101,
110,
110101,
111000,
100,
10010,
1101111111,
100100,
1101101,
1110,
111011,
100000,
111111,
111010,
10010,
11111111100,
111,
110010,
10101,
1000,
11111,
101010,
1101101,
1100,
1111111110,
1101010,
10011,
1110000,
1100001,
100,
100011,
100100,
100011,
11011111110,
110,
1001000,
11001,
11011010,
11011111,
11100,
100101,
1110110,
1111011111,
1000000,
10010,
1111110,
1101011,
1110100,
10000101,
10010,
10011,
111111111000,
10001,
1110,
11100,
1100100,
1001,
101010,
10010011,
10000,
1111111101,
111110,
101011,
1010100,
111010,
11011010,
11010111,
11000,
11010101,
1111111110,
1001,
11010100,
10000011,
100110,
110010,
11100000,
11100001,
11000010,
111111111111111111,
100,
101,
1000110,
11100001,
1001000,
101010,
1000110,
100010011,
110111111100,
1001010111,
110,
111,
10010000,
1011011,
110010,
1101010,
110110100,
10101111111,
110111110,
100111011,
111000,
11011,
1001010,
10001100111,
11101100,
1000,
11110111110,
11010011,
10000000,
100100001,
10010,
101001,
11111100,
11101111,
11010110,
11011111110,
11101000,
10001,
100001010,
110110101,
100100,
10011,
100110,
1001,
1111111110000,
11011010,
100010,
1100001,
11100,
110111,
11100,
1110001,
11001000,
10111110111,
10010,
1110110,
1010100,
10101101011,
100100110,
100011,
100000,
11101111,
11111111010,
1010111,
1111100,
1111110,
1010110,
11111011,
10101000,
10111101,
111010,
1111011111,
110110100,
1011001101,
110101110,
100100,
110000,
100101111,
110101010,
11010111,
11111111100,
1001111,
10010,
100101,
110101000,
1110,
100000110,
1001011,
1001100,
1010111010111,
110010,
11101111,
111000000,
11001,
111000010,
101010,
110000100,
1101000101,
1111111111111111110,
111000011,
1000};
int main()
{
int n;
while(cin>>n,n)
{
cout<<num[n-1]<<endl;
}
return 0;
}