POJ 1426 Find The Multiple

Find The Multiple

Time Limit: 1000ms

Memory Limit: 10000KB

This problem will be judged on PKU. Original ID: 1426
64-bit integer IO format: %lld      Java class name: Main

Special Judge

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

Dhaka 2002

解题:搜索。题意就是输入一个数,找出它的一个倍数,该值只包含0和1

DFS

 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 }

时间: 2024-10-18 01:19:16

POJ 1426 Find The Multiple的相关文章

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

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 ******************************************

[题解] [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结束. 示

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

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

POJ 1426 Find The Multiple(数论——中国同余定理)

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

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

poj 1426 Find The Multiple (bfs 搜索)

Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18012   Accepted: 7297   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 思路,线性同余,搜索 难度:2

http://poj.org/problem?id=1426 测试了一番,从1-200的所有值都有long long下的解,所以可以直接用long long 存储 从1出发,每次向10*s和10*s+1转移,只存储余数即可, 对于余数i,肯定只有第一个余数为i的最有用,只记录这个值即可 #include <cstdio> #include <cstring> #include <queue> using namespace std; const int maxn=222