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. 国内程序员的十大疑问之一:为什么老外不愿意用MyBatis?

    老外用MyBatis吗 昨天我在我在知乎看到了一张比较Hibernate和MyBatis使用情况的图,顺手发了条朋友圈: Hibernate vs MyBatis ,谁能告诉我什么样的国情导致了这么大 ...

  2. 【NOIP2017练习】函数变换(DP,dfs)

    题意: 思路: 极限步数大概不会超过30 ; ..max,..]of longint; eul:..max]of longint; cas,v,n,k,i,ans,j:longint; functio ...

  3. CentOS虚拟机与本机同步时间

    接着之前的任务,还是为了在VMWare上搭建分布式hadoop集群.搭着搭着注意到虚拟机上的时间和本机是不同步的,而且可以说是乱七八糟,3台虚拟机时间都与本机差了8个小时以上.首先确认不是时区的问题, ...

  4. AjaxFileUpload文件上传组件(php+jQuery+ajax)

    jQuery插件AjaxFileUpload可以实现ajax文件上传,下载地址:http://www.phpletter.com/contents/ajaxfileupload/ajaxfileupl ...

  5. Spring Boot - how to configure port

    https://stackoverflow.com/questions/21083170/spring-boot-how-to-configure-port

  6. Mayor's posters POJ - 2528

    The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...

  7. MongoDB小结27 - 聚合管道【$project】

    我们有这样的数据 { "_id" : 1, title: "abcdef", isbn: "6969696969", author: { l ...

  8. Ansible 2.0公布

    本文来源于我在InfoQ中文站翻译的文章,原文地址是:http://www.infoq.com/cn/news/2016/02/ansible-2-released 经过了一年的开发工作后,Ansib ...

  9. JS原生DOM操作总结

    DOM的主要操作——增.删.改.查节点 (1) 查找节点 document.getElementById('div1') document.getElementsByName('uname') doc ...

  10. Hadoop 知识

    Map Reduce & YARN 简介 Apache Hadoop 是一个开源软件框架,可安装在一个商用机器集群中,使机器可彼此通信并协同工作,以高度分布式的方式共同存储和处理大量数据.最初 ...