Find The Multiple
64-bit integer IO format: %lld Java class name: Main
Input
Output
Sample Input
2 6 19 0
Sample Output
10 100100100100100100 111111111111111111
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 int n,limit = 20; 18 unsigned LL ans; 19 bool dfs(unsigned LL cur,int step) { 20 if(step == limit) return false; 21 if(cur%n == 0) { 22 ans = cur; 23 return true; 24 } 25 if(dfs(cur*10,step+1)) return true; 26 if(dfs(cur*10+1,step+1)) return true; 27 return false; 28 } 29 int main() { 30 while(scanf("%d",&n),n) { 31 if(dfs(1,0)) printf("%I64u\n",ans); 32 } 33 return 0; 34 }
BFS 貌似要快那么点点
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 int n,head,tail; 18 unsigned LL q[1000000]; 19 unsigned LL bfs() { 20 head = tail = 0; 21 q[tail++] = 1; 22 while(head < tail) { 23 unsigned LL now = q[head++]; 24 if(now%n == 0) return now; 25 unsigned LL tmp = now*10; 26 if(tmp%n == 0) return tmp; 27 q[tail++] = tmp; 28 tmp = now*10+1; 29 if(tmp%n == 0) return tmp; 30 q[tail++] = tmp; 31 } 32 } 33 int main() { 34 while(scanf("%d",&n),n) printf("%I64u\n",bfs()); 35 return 0; 36 }