POJ1426-Find The Multiple(BFS||DFS)


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

Means:

给你一个数n要你找出能整除n的一个只有0||1组成的十进制数,随便输出一个都行

Solve:

水题,其实就是从1开始枚举每次为p * 10 || p * 10 + 1,然后,蜜汁爆CPP,蜜汁ACGPP

Code:

 1 #include <queue>
 2 #include <cstdio>
 3 using namespace std;
 4 typedef unsigned long long ll;
 5 ll n;
 6 inline void Bfs()
 7 {
 8     queue<ll> qu;
 9     qu.push(1);
10     while(!qu.empty())
11     {
12         ll p = qu.front();
13         qu.pop();
14         if(p % n == 0)  {printf("%lld\n" , p); return ;}
15         qu.push(p * 10);
16         qu.push(p * 10 + 1);
17     }
18 }
19 int main()
20 {
21     while(~scanf("%lld" , &n) && n)
22     {
23         Bfs();
24     }
25 }

时间: 2024-08-07 00:13:38

POJ1426-Find The Multiple(BFS||DFS)的相关文章

poj1426 - Find The Multiple [bfs 记录路径]

传送门 转:http://blog.csdn.net/wangjian8006/article/details/7460523 (比较好的记录路径方案) 1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue>

Collect More Jewels(hdu1044)(BFS+DFS)

Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6857    Accepted Submission(s): 1592 Problem Description It is written in the Book of The Lady: After the Creation, the cruel

邻结矩阵的建立和 BFS,DFS;;

邻结矩阵比较简单,, 它的BFS,DFS, 两种遍历也比较简单,一个用队列, 一个用数组即可!!!但是邻接矩阵极其浪费空间,尤其是当它是一个稀疏矩阵的时候!!!---------------------------------------------------------------------------------------------------------------------------------------//邻接矩阵的建立和 其BFS, DFS, 遍历 #include <

HDU ACM 1044 Collect More Jewels BFS+DFS

题意:在一个迷宫中,有一些宝物,从起点走到终点,问在给定的时间内,到达终点后所能拾取珠宝的最大价值. 分析(BFS+DFS): 1.求入口到第一个取宝物的地方的最短距离 2.求第i个取宝物的地方到第i+1个取宝物的地方的最短距离 3.求第n个取宝物的地方到出口的最短距离 4.保证以上3点能在时间L内实现的情况下,取得的宝石价值最大. BFS特点:对于解决最短或最少问题特别有效,而且寻找深度小,但缺点是内存耗费量大(需要开大量的数组单元来存储状态) DFS特点:对于解决遍历和求所有问题有效,对于问

建图方式一 之 ”前向星“ BFS&amp;&amp;DFS 简单应用

三种建图方式,邻接矩阵.前向星(边表集).邻接链表! 耗时一晚上 ,好好研究了一下 前向星,因为我的指针用的实在是很烂,所以还是 入赘 前向星吧. 问了学长,看了大牛们的博客,终于有点收获了,个人认为 前向星Very Well. 前向星 建图方法: 以储存边的方式来储存图.在构造图时,把边存放在数组里,不需使用指针,只需一个 next  即可是整个图构建完成 . 适用条件: 点集特别多的稀疏图,边数多且繁杂,开邻接矩阵会浪费大量内存. 时间复杂度: O(m),并不比邻接链表差. #include

UVALive - 6455 Stealing Harry Potter&#39;s Precious (bfs+dfs)

https://cn.vjudge.net/problem/UVALive-6455 题目大意:题目上给出一个地图,其中@是人在地图上的出发点,#是墙,' . '是路.然后给出几个点,这些点表示财宝的所在地.问人是否能够得到所有的宝藏,如果能够的话给出所有的宝藏的最短的路径. 解题思路:由于只有最多4个宝藏所在地,所以只要用bfs找出出发点和财宝所在地距离到达其他点的步数.因为最多只有4个宝藏,所以可以暴力找出出发点到所有宝藏的最短距离. 暴力代码: 1 #include<stdio.h> 2

POJ Multiple (BFS,同余定理)

http://poj.org/problem?id=1465 Multiple Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 6164   Accepted: 1339 Description a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM

hdu 4771 Stealing Harry Potter&#39;s Precious (2013亚洲区杭州现场赛)(搜索 bfs + dfs) 带权值的路径

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4771 题目意思:'@'  表示的是起点,'#' 表示的是障碍物不能通过,'.'  表示的是路能通过的: 目的:让你从 '@' 点出发,然后每个点只能走一次,求出最小的距离: 解题思路:先用 bfs 求解出任意两点之间的距离,用 ans[i][j],表示点 i 到点  j 的距离: 然后用 dfs 递归求出从起点经过所有点的距离中,比较出最小的: AC代码: 1 #include<iostream>

POJ 1130(一道纯水,bfs+dfs)

POJ 1130 大概题意:给出一副图,求从起点到终点 (0->ET) 必须经过的一点. 我的思路:首先DFS求出经过每点的次数,必过的一点的次数一定最高,但是就这样吗?有可能有多个必过的点,所以还要找出离ET最近的点,这里就利用BFS逐层搜索的性质求每点到ET的距离. #include<iostream> #include<cstdio> #include<string.h> #include<iomanip> #include<queue&g