码迷,mamicode.com
首页 > 其他好文 > 详细

DFS/BFS(同余模) POJ 1426 Find The Multiple

时间:2015-08-02 21:15:31      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

 

题目传送门

 1 /*
 2     题意:找出一个0和1组成的数字能整除n
 3     DFS:200的范围内不会爆long long,DFS水过~
 4 */
 5 /************************************************
 6 Author        :Running_Time
 7 Created Time  :2015-8-2 14:21:51
 8 File Name     :POJ_1426.cpp
 9 *************************************************/
10 
11 #include <cstdio>
12 #include <algorithm>
13 #include <iostream>
14 #include <sstream>
15 #include <cstring>
16 #include <cmath>
17 #include <string>
18 #include <vector>
19 #include <queue>
20 #include <deque>
21 #include <stack>
22 #include <list>
23 #include <map>
24 #include <set>
25 #include <bitset>
26 #include <cstdlib>
27 #include <ctime>
28 using namespace std;
29 
30 typedef long long ll;
31 const int MAXN = 1e4 + 10;
32 const int INF = 0x3f3f3f3f;
33 const int MOD = 1e9 + 7;
34 ll n;
35 bool ok;
36 
37 void DFS(ll x, int step, int dep)  {
38     if (ok) return ;
39     if (step >= dep)    return ;
40     if (x % n == 0) {
41         printf ("%I64d\n", x);
42         ok = true;  return ;
43     }
44     DFS (x * 10, step + 1, dep);
45     DFS (x * 10 + 1, step + 1, dep);
46 }
47 
48 int main(void)    {       //POJ 1426 Find The Multiple
49     while (scanf ("%I64d", &n) == 1)   {
50         if (!n) break;
51         ok = false; ll dep = 1;
52         while (true)    {
53             if (ok) break;
54             DFS (1, 0, dep);    dep++;
55         }
56     }
57 
58     return 0;
59 }
技术分享
 1 /*
 2     BFS+同余模定理:mod数组保存,每一位的余数,当前的数字由少一位的数字递推,
 3         比如4(100)可以从2(10)递推出,网上的代码太吊,膜拜之
 4     详细解释:http://blog.csdn.net/lyy289065406/article/details/6647917
 5 */
 6 /************************************************
 7 Author        :Running_Time
 8 Created Time  :2015-8-2 15:14:33
 9 File Name     :POJ_1426_BFS.cpp
10 *************************************************/
11 
12 #include <cstdio>
13 #include <algorithm>
14 #include <iostream>
15 #include <sstream>
16 #include <cstring>
17 #include <cmath>
18 #include <string>
19 #include <vector>
20 #include <queue>
21 #include <deque>
22 #include <stack>
23 #include <list>
24 #include <map>
25 #include <set>
26 #include <bitset>
27 #include <cstdlib>
28 #include <ctime>
29 using namespace std;
30 
31 #define lson l, mid, rt << 1
32 #define rson mid + 1, r, rt << 1 | 1
33 typedef long long ll;
34 const int MAXN = 1e6 + 10;
35 const int INF = 0x3f3f3f3f;
36 const int MOD = 1e9 + 7;
37 int mod[MAXN];
38 int ans[MAXN];
39 
40 int main(void)    {
41     int n;
42     while (scanf ("%d", &n) == 1)   {
43         if (!n) break;
44         mod[1] = 1; int i;
45         for (i=2; mod[i-1]; ++i)    {
46             mod[i] = (mod[i/2] * 10 + (i&1)) % n;       //BFS双入口模拟
47         }
48         int t = 0;  i--;
49         while (i)   {
50             ans[++t] = i & 1;
51             i /= 2;
52         }
53         while (t)   {
54             printf ("%d", ans[t--]);
55         }
56         puts ("");
57     }
58 
59     return 0;
60 }
BFS

 

DFS/BFS(同余模) POJ 1426 Find The Multiple

标签:

原文地址:http://www.cnblogs.com/Running-Time/p/4696607.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!