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. Thinkphp5.0 控制器向视图view赋值

    Thinkphp5.0 控制器向视图view的赋值 方式一(使用fetch()方法的第二个参数赋值): <?php namespace app\index\controller; use thi ...

  2. runOnUiThread在子进程中更新主进程UI

    package com.pingyijinren.test; import android.support.v7.app.AppCompatActivity; import android.os.Bu ...

  3. SharedPreferences保存用户偏好参数

    package com.example.administrator.myapplication; import android.content.Context; import android.cont ...

  4. 网易2018校招 合唱 DP

      时间限制:2秒 空间限制:131072K 小Q和牛博士合唱一首歌曲,这首歌曲由n个音调组成,每个音调由一个正整数表示.对于每个音调要么由小Q演唱要么由牛博士演唱,对于一系列音调演唱的难度等于所有相 ...

  5. Mayor's posters POJ - 2528

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

  6. 洛谷—— P3386 【模板】二分图匹配

    P3386 [模板]二分图匹配(复习) 题目背景 二分图 题目描述 给定一个二分图,结点个数分别为n,m,边数为e,求二分图最大匹配数 输入输出格式 输入格式: 第一行,n,m,e 第二至e+1行,每 ...

  7. JSP中操作Java Beans

    以下内容引用自http://wiki.jikexueyuan.com/project/jsp/beans.html: JavaBean是在编写Java时专门创建的Java类,根据JavaBean AP ...

  8. 009 NAT

    static nat r1(config)#ip nat inside source static 192.168.1.2 10.0.0.2 r1(config)#int f0/0 r1(config ...

  9. MariaDB -- 数据类型

    Mariadb 的数据类型 MariaDB数据类型可以分为数字,日期和时间以及字符串值. 使用数据类型的原则:够用就行, 尽量使用范围小的,而不用大的 常用的数据类型 整数:int, bit 小数:d ...

  10. Myeclipse10集成Flex4.6

    安装好flash builder4.6 执行fb安装文件夹下utilities\Adobe Flash Builder 4.6 Plug-in Utility.exe 插件. 第一次选择flash b ...