您的位置:首页 > 其它

POJ 1426 Find The Multiple【dfs】

2015-11-25 11:34 267 查看

Find The Multiple

Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 22877
Accepted: 9431
Special Judge
Description
Given a positiveinteger n, write a program to find out a nonzero multiple m of n whose decimalrepresentation contains only the digits 0 and 1. You may assume that n is notgreater than 200 and there is a corresponding m containing no more than
100decimal digits.
Input
The input file maycontain 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 ofn in the input print a line containing the corresponding value of m. Thedecimal representation of m must not contain more than 100 digits. If there aremultiple 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(1≤N≤200),求任意一个N的倍数M*N【这个是我设】,使得数的十进制位每位都是由0或1组成。任意输出一组解就行。题目保证有解。

思路:

__int64可以装下。那么最多到达20个十进制位,那么就可以直接dfs水过了=_=

实现代码:

 

#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
typedef long long LL;
int N;
bool suc;
void dfs(LL M, int bit) {
if(bit >= 20 || suc) return;
if(M % N == 0) {
suc = true;
printf("%I64d\n", M);
return;
}
dfs(M * 10, bit + 1);
dfs(M * 10 + 1, bit + 1);
}
int main()
{
//    FIN;
while(~scanf("%d", &N) && N) {
suc = false;
dfs(1, 1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: