POJ1426 Find The Multiple
Find The Multiple
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 12097 | Accepted: 4968 | 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
思路:寻找能整除N的,由0,1组成的数。本题采用DFS,虽然题目要求最后的结果可能在100digits。但是实际情况下,在unsigned long long 范围内完全可以找到相应的答案。
采用 unsigned long long 的版本:
1 #include <cstdlib> 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <map> 7 #include <string> 8 #include <algorithm> 9 10 11 12 13 using namespace std; 14 15 unsigned long long answer; 16 17 18 19 int dfs(unsigned long long prenum,unsigned long long lev,int n) 20 { 21 if(prenum>=lev) 22 return 0; 23 24 if(lev%n==0) 25 {answer=lev;return 1;} 26 27 if(dfs(lev,lev*10,n)) 28 {return 1;} 29 30 return dfs(lev,lev*10+1,n); 31 32 33 } 34 35 36 37 38 39 40 int main(int argc, char *argv[]) 41 { 42 int n; 43 int i,j,k; 44 45 while(scanf("%d",&n)!=EOF) 46 { 47 if(!n) 48 break; 49 50 if(dfs(0,1,n)) 51 cout<<answer<<endl; 52 } 53 54 55 56 57 // system("PAUSE"); 58 return EXIT_SUCCESS; 59 }