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. 【spring boot 系列】spring data jpa 全面解析(实践 + 源码分析)

    前言 本文将从示例.原理.应用3个方面介绍spring data jpa. 以下分析基于spring boot 2.0 + spring 5.0.4版本源码 概述 JPA是什么? JPA (Java ...

  2. [bzoj2946][Poi2000]公共串_后缀数组_二分

    公共串 bzoj-2946 Poi-2000 题目大意:给定$n$个字符串,求他们的最长公共子串. 注释:$1\le n\le 5$,$1\le minlen<maxlen\le 2000$. ...

  3. 英特尔固态盘 说明书PDF

    http://www.intel.cn/content/www/cn/zh/solid-state-drives/solid-state-drives-ssd.html

  4. Java:删除某文件夹下的所有文件

    import java.io.File;public class Test{ public static void main(String args[]){ Test t = new Test(); ...

  5. TensorFlow-GPU环境配置之二——CUDA环境配置

    1.安装最新显卡驱动 到系统设置->软件和更新->附加驱动中选中最新的显卡驱动,并应用 2.下载CUDA8.0 https://developer.nvidia.com/cuda-down ...

  6. DHCP Snooping的实现

    DHCP Snooping的实现 DHCP Snooping的实现 主要作用:1.防止在动态获得IP地址的网络环境中用户手动配置PC的IP地址;2.防止A用户的PC静态配置的IP地址顶掉B用户PC动态 ...

  7. SQL语句小结

    1.创建数据库 create database 数据库名 2.删除数据库 drop  database 数据库名 3.创建表 1>.create table 表名 (col1 type1 [no ...

  8. iOS xcode6最新提交app方法

    依照之前方式打包.打包成功后.直接submit提交AppStore.然后再选择build,假设上传成功,但在build选择上未出现,你能够耐心等待.有可能要等上一天,然后选择相应的build,直接提交 ...

  9. Woody的逻辑游戏--怎样换轮胎

    题目:有一个做长途运输的司机要出发了,他用作运输的车是三轮车.轮胎的寿命是2万里,如今他要进行5万里的长途运输.计划用8个轮胎完毕运输任务,如何才干做到呢? 首先将轮胎从1-8依次编号,然后例如以下所 ...

  10. Python爬虫【第3篇】【多线程】

    一.多线程 Python标准库提供2个模块,thread是低级模块,threading是高级模块 1.threading模块创建多线程 方式1:把1个函数传入并创建Thread实例,然后调用start ...