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

题目传送门

 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

时间: 2024-08-06 11:58:17

DFS/BFS(同余模) POJ 1426 Find The Multiple的相关文章

[题解] [BFS] POJ 1426 - Find The Multiple

p { margin-bottom: 0.25cm; line-height: 115% } a:link { } VJudge题目:https://cn.vjudge.net/contest/279018#problem/L 即POJ 1426 - Find The Multiple:http://poj.org/problem?id=1426 题目要求: 给出一个不大于200的整数n,要求找到任意一个n的倍数(十进制),并且这个倍数只由1与0组成,最多100位. 输入输出:输入以0结束. 示

POJ 1426 Find The Multiple(寻找倍数)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

广搜+打表 POJ 1426 Find The Multiple

POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Accepted: 10613   Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representati

20200202 POJ - 3126 Prime Path POJ - 1426 Find The Multiple

>>>>>>>>>POJ 1426直达?? >>>>>>>>>POJ 3126直达?? 做了这么几道搜索题,感觉差不多也摸出了门路,模板差不多记下来了,只是面对不同的题目算法不同罢了 简单写一下想法,搞明白搜索的主题到底是什么(之前的mutiple那题,一开始就没想明白到底搜谁,就没想到算法 TBC(晚上再写 ===================================分割线=======

POJ 1426 Find The Multiple &amp;amp;&amp;amp; 51nod 1109 01组成的N的倍数 (BFS + 同余模定理)

Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21436   Accepted: 8775   Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains

POJ 1426 Find The Multiple(DFS,BFS)

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

poj 1426 Find The Multiple ( BFS+同余模定理)

Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18390   Accepted: 7445   Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains

POJ 1426 Find The Multiple(大数取模)【DFS】||【BFS】

<题目链接> 题目大意: 给一个小于200的正整数n,问只有0和1组成的位数小于100的最小能被n整除的数是多少. 解题分析: 用DFS或者BFS沿着位数进行搜索,每一次搜索到下一位都有两种情况,0或者1,还要注意的是,大数取模的方法.但是我有一个疑问,这题位数最高为100,用dfs为什么不会超时?还是说只是此题数据太弱吗? BFS解法 #include<iostream> #include<cstdio> #include<queue> #include&

POJ 1426 - Find The Multiple - [DP][BFS]

题目链接:http://poj.org/problem?id=1426 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 correspo