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
解题思路:题目的意思就是找一个不小于n的10进制数x,x只由1和0组成,并且满足x%n==0,输出这个x即可。由于x只由0和1组成,因此容易想到构建一棵平衡二叉树,从根节点值为1开始,每个左节点的值都为父节点值的10倍,每个右节点的值都为父节点值的10倍加1,这样只要找到x%n==0即可。亲测了一下x的值在long long范围内,于是bfs或dfs暴力即可。
AC代码之bfs:
 #include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
typedef long long LL;LL n;
queue<LL> que;
void bfs(LL n){//在long long的范围内查找,按层遍历
while(!que.empty())que.pop();//清空
que.push(1LL);//模拟一棵平衡二叉树,先将入根节点的值1入队
while(!que.empty()){
LL x=que.front();que.pop();
if(x%n==){cout<<x<<endl;return;}//只要满足条件,立即退出
que.push(x*);//左节点的值为父节点值的10倍
que.push(x*+);//右节点的值为父节点值的10倍加1
}
}
int main(){
while(cin>>n&&n){bfs(n);}
return ;
}

AC代码之dfs:

 #include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
typedef long long LL;LL n;bool flag;
void dfs(int num,LL x,LL n){
if(num>||flag)return;//如果x的位数num超过19位即溢出了long long 范围,直接返回到上一层;如果已找到即flag为true则一直回退,直到栈空,表示不再深搜下去
if(x%n==){cout<<x<<endl;flag=true;return;}
dfs(num+,x*,n);//左递归
dfs(num+,x*+,n);//右递归
}
int main(){
while(cin>>n&&n){flag=false;dfs(,,n);}//从位数1,根节点值为1开始深搜
return ;
}

题解报告:poj 1426 Find The Multiple(bfs、dfs)的更多相关文章

  1. POJ 1426 Find The Multiple --- BFS || DFS

    POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...

  2. POJ 1426 Find The Multiple (DFS / BFS)

    题目链接:id=1426">Find The Multiple 解析:直接从前往后搜.设当前数为k用long long保存,则下一个数不是k*10就是k*10+1 AC代码: /* D ...

  3. poj 1426 Find The Multiple (bfs 搜索)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18012   Accepted: 729 ...

  4. POJ 1426 Find The Multiple BFS

    没什么好说的 从1开始进行广搜,因为只能包涵0和1,所以下一次需要搜索的值为next=now*10 和 next=now*10+1,每次判断一下就可以了,但是我一直不太明白我的代码为什么C++提交会错 ...

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

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18390   Accepted: 744 ...

  6. POJ - 1426 Find The Multiple 【DFS】

    题目链接 http://poj.org/problem?id=1426 题意 给出一个数 要求找出 只有 0 和 1 组成的 十进制数字 能够整除 n n 不超过 200 十进制数字位数 不超过100 ...

  7. POJ 1426 Find The Multiple (dfs??!!)

    Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...

  8. POJ.1426 Find The Multiple (BFS)

    POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...

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

    题目传送门 /* 题意:找出一个0和1组成的数字能整除n DFS:200的范围内不会爆long long,DFS水过~ */ /************************************ ...

  10. POJ 1426 Find The Multiple(寻找倍数)

    POJ 1426 Find The Multiple(寻找倍数) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given ...

随机推荐

  1. 破损的键盘(codevs 4650)

    题目描述 Description 有一天,你需要打一份文件,但是你的键盘坏了,上面的"home"键和"end"键会时不时地按下,而你却毫不知情,甚至你都懒得打开 ...

  2. hdu4778(状态压缩dp)

    题意: 有G种颜色的宝石,共B袋.两个人轮流拿宝石,每次从B袋中拿一袋,把其中的所有宝石倒入一个公共容器,每袋宝石只能取一次. 当容器中有S个相同颜色的宝石时,将失去这S个宝石,当前操作者得到一个魔法 ...

  3. JSP的调试

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/debugging.html: 一.使用System.out.println() System.out.p ...

  4. 【nginx】nginx与apache的优缺点比较

    参考: http://zyan.cc/nginx_php_v6/ nginx相对于apache的优点: 1.轻量级,同样的web 服务,比apache服务器占用更少的内存及资源 2.抗并发,nginx ...

  5. nyoj 739 笨蛋的难题四

    笨蛋难题四 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 这些日子笨蛋一直研究股票,经过调研.最终发现xxx公司股票规律,更可喜的是 笨蛋推算出这家公司每天的股价, ...

  6. Eclipse + CDT引入OpenCV失败的解决的方法

    Android JNI开发中用到了OpenCV,由于想通过JNI实现,就没有去用Android层的Lib引用. 可是操作中发如今.cpp文件中include的时候发现"#include &l ...

  7. 约瑟夫环问题(Josephus)

    约瑟夫环:用户输入M,N值,从1至N开始顺序循环数数,每数到M输出该数值,直至最后一个元素并输出该元素的值. 一.循环链表:建立一个有N个元素的循环链表,然后从链表头开始遍历并记数,如果计数值为M,则 ...

  8. CSS设置元素居中的方法

    1.margin:0 auto; 2.body{text-align:center;} 3.position:absolute; left:50%; margin-left:-(宽/2);

  9. c# DataGridView样式设置无效

    对DataGridView中的某些行设置样式时,无效,最后发现,我是先设置完样式再进行展现的this.controls.Add,应该先展现完了,再设置样式.

  10. (三)Java 开发环境配置

    window系统安装java 下载JDK   首先我们需要下载java开发工具包JDK,下载地址:http://www.oracle.com/technetwork/java/javase/downl ...