题目链接:http://poj.org/problem?id=1426

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

题意:

给出一个在 $[1,200]$ 范围内的整数 $n$,要求找到一个只包含 $0$ 和 $1$ 的十进制整数,是 $n$ 的倍数,可以保证 $m$ 不会超过 $100$ 位。

题解:

首先,不同于从低位向高位搜索,我们从高位向低位搜索,

根据手算除法的原理,高位模 $n$ 的余数,应当乘 $10$ 后加到其低一位上去,

而由于一位一位的搜索产生的肯定是一棵二叉树,不妨参考完全二叉树按数组形式存储的方式开一个 $dp[i]$ 数组,

对于任意一个节点 $i$,从根节点 $1$ 走到当前节点 $i$ 生成的就是一个01十进制整数 $m$,而 $dp[i]$ 存储的,就是这个 $m$ 模 $n$ 的余数,

所以就有状态转移方程:

$\begin{array}{l} dp[2 \times i] = (dp[i] \times 10)\% n \\ dp[2 \times i + 1] = (dp[i] \times 10 + 1)\% n \\ \end{array}$

考虑完全二叉树的存储形式,我们完全可以用循环代替BFS。

AC代码:

#include<iostream>
#include<vector>
using namespace std;
const int maxn=5e6;
int n;
int dp[maxn];
vector<int> ans;
int main()
{
while(cin>>n && n)
{
dp[]=%n;
int now;
for(int i=;;i++)
{
if(!(dp[i*]=dp[i]*%n)) {now=i*;break;}
if(!(dp[i*+]=(dp[i]*+)%n)) {now=i*+;break;}
} ans.clear();
while(now)
{
ans.push_back(now%);
now/=;
}
for(int i=ans.size()-;i>=;i--) cout<<ans[i]; cout<<endl;
}
}

当然,不难发现,其实所有的答案不会超过 $1,111,111,111,111,111,110$,也就是 $1e18$ 量级,不超过 long long 类型的 $9,223,372,036,854,775,807$,

所以就算用从低位向高位进行01枚举的普通BFS也完全可以搞233:

#include<iostream>
#include<queue>
using namespace std;
typedef long long ll; int n;
queue<ll> q; int main()
{
while(cin>>n && n)
{
while(!q.empty()) q.pop();
q.push();
while(!q.empty())
{
ll x=q.front();q.pop();
ll y=x*,z=x*+;
if(y%n==) {cout<<y<<endl;break;}
if(z%n==) {cout<<z<<endl;break;}
q.push(y),q.push(z);
}
}
}

POJ 1426 - Find The Multiple - [DP][BFS]的更多相关文章

  1. POJ.1426 Find The Multiple (BFS)

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

  2. poj 1426 Find The Multiple( bfs )

    题目:http://poj.org/problem?id=1426 题意:输入一个数,输出这个数的整数 倍,且只有0和1组成 程序里写错了一个数,结果一直MLE.…… #include <ios ...

  3. (简单) POJ 1426 Find The Multiple,BFS+同余。

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

  4. 题解报告:poj 1426 Find The Multiple(bfs、dfs)

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

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

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

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

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

  7. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

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

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

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

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

随机推荐

  1. 使用pyenv安装多个Python版本

    pyenv是一个便于使用多版本Python环境的工具 pyenv使用shell脚本编写的,只需要下载就可以使用了,不需要root用户,这个我比较喜欢. 具体介绍看网址:https://github.c ...

  2. MySQL 5.5主从关于‘复制过滤’的深入探究

    关于MySQL主从复制的过滤,例如通过binlog-ignore-db.replicate-do-db.replicate-wild-do-table等.如果不好好研究过这些过滤选项就用的话,是有可能 ...

  3. iOS UITextView 设置 NSLinkAttributeName 属性,点击链接跳转

    @interface ViewController ()<UITextViewDelegate> - (void)viewDidLoad{    [super viewDidLoad];  ...

  4. Spark RDD、DataFrame原理及操作详解

    RDD是什么? RDD (resilientdistributed dataset),指的是一个只读的,可分区的分布式数据集,这个数据集的全部或部分可以缓存在内存中,在多次计算间重用. RDD内部可以 ...

  5. 解决stackoverflow打开慢的问题

    Stack Overflow是国外一个与程序相关的IT技术问答网站.类似于国内的segmentfault.程序员们对于这2个网站应该都不陌生.但是我们打开stackoverflow的时候,会发现打开速 ...

  6. .NET Core 2.1中的HttpClientFactory最佳实践

    ASP.NET Core 2.1中出现一个新的HttpClientFactory功能, 它有助于解决开发人员在使用HttpClient实例从其应用程序发出外部Web请求时可能遇到的一些常见问题. 介绍 ...

  7. [JVM] IDEA集成VisualVM

    VisualVM是集成命令行JDK工具和轻量级分析功能的可视化工具. 参考: https://blog.csdn.net/qq_22741461/article/details/80451675 ht ...

  8. 20模板方法模式TemplateMethod

    一.什么是模板方法模式 Template Method模式也叫模板方法模式,是 行为模式之一,它把具有特定步骤算法中的某些 必要的处理委让给抽象方法,通过子类继承对抽 象方法的不同实现改变整个算法的行 ...

  9. iOS 库操作

    目录 库操作 主工程和子工程的引用关系 库之间的引用关系 ar命令 nm命令 库操作 主工程和子工程的引用关系 子工程引用主工程中的文件需要在子工程的search path中加入头文件的目录 子工程引 ...

  10. Centos7 Crontab

    Centos7 Crontab # 查看crontab -l # 编辑 crontab -e # 重启 service crond restart ccrontab 编写规则 # 分 时 日 月 周 ...