Poj 1426 find the multiple
Http://poj.org/problem? Id = 1426
Question: given a positive integer N, write a programFind out a nonzero multiple (multiple) m of NWhose decimal (decimal) Representation contains only the digits 0 and 1. (n <200, the maximum number of digits of M is 100)
Analysis: the positive thinking will think of brute force, judge whether the multiples of n m meet, Ms timeout, and need to handle large numbers.
But the other way around, how can we see that the decimal system is a number consisting of 1 and 0?Multiply by 10OrMultiply by 10 plus 1,Write a deep search with a depth of 100, so that the worst cycle is 100*100, and no timeout will occur.
But if the depth is 100, the number cannot be saved.
In the solution obtained by wide search, the longest number of digits is 198,19. Therefore, you can use the unsigned long storage number to write a deep search with a depth of 19. OK
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <algorithm> 5 #include <cstdio> 6 #include <cstring> 7 #include <cmath> 8 #include <stack> 9 #include <queue>10 #include <functional>11 #include <vector>12 #include <map>13 /*10^8-----1s*/14 using namespace std;15 //#define M 0x0fffffff16 #define M 100000000417 #define min(a,b) (a>b?b:a)18 #define max(a,b) (a>b?a:b)19 #define N 100120 int flag,n;21 void dfs(unsigned long long x,int deep)22 {23 if(deep==19||flag)24 return ;25 if(x%n==0)26 {27 flag=1;28 printf("%I64u\n",x);29 return ;30 }31 dfs(x*10,deep+1);32 dfs(x*10+1,deep+1);33 }34 int main()35 {36 37 while(scanf("%d",&n)&&n)38 {39 flag=0;40 dfs(1,0);41 }42 return 0;43 }View code
Poj 1426 find the multiple