POJ - 1426 Find The Multiple(15.10.10 搜索专题)bfs

来源:互联网 发布:淘宝拒收可以退款吗 编辑:程序博客网 时间:2024/06/07 01:01
Find The Multiple
Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u

Submit Status

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

26190

Sample Output

10100100100100100100111111111111111111


题意:求一个数位上只有1或0的十进制数,满足可以被n整除。

思路:打表可以看到n<=200的时候所求数的最高位不会超过long long的范围,直接暴搜还是过不了,加一点剪枝就过了。但还是不理解为什么可以这样剪枝,想明白后再来写。


#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;#define LL long longbool vis[2001];LL bfs(int n){memset(vis, false, sizeof(vis));queue<LL>que;que.push(1);vis[1] = true;while (!que.empty()){LL loc = que.front();vis[loc%n] = true;que.pop();if (loc%n == 0)return loc;if(!vis[(loc*10)%n]) que.push(loc * 10);if(!vis[(loc*10+1)%n]) que.push(loc * 10 + 1);}return -1;}int main(){int n;while (scanf("%d",&n)!=EOF && n){LL ans = bfs(n);printf("%I64d\n", ans);}}


0 0
原创粉丝点击