题目链接: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. 基于java实现的简单区块链

    技术:maven3.0.5 + jdk1.8   概述 区块链是分布式数据存储.点对点传输.共识机制.加密算法等计算机技术的新型应用模式.所谓共识机制是区块链系统中实现不同节点之间建立信任.获取权益的 ...

  2. Effective Java 第三版——67. 明智谨慎地进行优化

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  3. 时间序列分解算法:STL

    1. 详解 STL (Seasonal-Trend decomposition procedure based on Loess) [1] 为时序分解中一种常见的算法,基于LOESS将某时刻的数据\( ...

  4. [db]mysql全量迁移db

    机房要裁撤, 原有的老业务机的mysql需要迁移到新的. 方案1: 全量打包拷贝data目录, 发现拷过去各种毛病 方案2: mysqldump逻辑导出解决问题 新的db刚安装好. 步骤记录下. # ...

  5. Android内存泄漏杂谈

    内存泄漏:是指内存得不到GC的及时回收,从而造成内存占用过多.从而导致程序Crash,也就是常说的OOM. 一.static 先来看以下一段代码 public class DBHelper { pri ...

  6. .NET Core +NuGet 创建打包发布自己的类库包

    1. 创建类库项目 你可以使用现有的 .NET 类库项目用于要打包的代码,或者创建一个简单的项目,.NET CORE 2.1 项目的 类库如下所示: NugetDemo.class using Sys ...

  7. 通过itools安装ipa时,如果装不上提示"Mismatche...onIdentifierEntitlement"

    最后安装ipa时,如果装不上提示"Mismatche...onIdentifierEntitlement",一定要卸载设备里的现有的微信app!!!!!!!!!!!!!还有就是,如 ...

  8. Hadoop集群datanode磁盘不均衡的解决方案【转】

    一.引言: Hadoop的HDFS集群非常容易出现机器与机器之间磁盘利用率不平衡的情况,比如集群中添加新的数据节点,节点与节点之间磁盘大小不一样等等.当hdfs出现不平衡状况的时候,将引发很多问题,比 ...

  9. Android Studio编译报错“java.lang.OutOfMemoryError: GC overhead limit exceeded

    1.在build.gradle添加脚本指定编译堆内存 如果在整个工程中生效,则在build.gradle中增加如下配置: android { .............. dexOptions { i ...

  10. 图解JAVA参数传递

    今天做项目,发现了一个问题,当String作为参数传递的时候,在函数内部改变值对外部的变量值无影响,如下代码: public static void main(String[] args) { Str ...