Loading

POJ 1426 Find The Multiple

Find The Multiple
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 46927   Accepted: 19608   Special Judge

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

 

思路: 使用dfs枚举所有可能的结果(01串),当该结果可以整除n时,就输出。由于long long最多可以表示19位数字,所以当该数的位数大于19时,return。

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int n, flag;
 6 
 7 void dfs(int k, long long cur)  // k:位数 cur:当前数字
 8 {     
 9     if (k == 19 || flag)        // long long最多只能表示19位数
10         return;
11 
12     if (cur % n == 0) {
13         cout << cur << endl;
14         flag = 1;
15         return;
16     }
17     for (int i = 0; i <= 1; ++i)
18     {
19         dfs(k + 1, cur * 10 + i);
20     }
21 }
22 
23 
24 int main()
25 {
26     while (cin >> n, n != 0) {
27         flag = 0;
28         dfs(0, 1);
29     }
30     return 0;
31 }

 

posted @ 2019-03-11 16:44  拾月凄辰  阅读(124)  评论(0编辑  收藏  举报