题目链接: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. C# 对轻量级(IoC Container)依赖注入Unity的使用

    概述 Unity是一个轻量级的可扩展的依赖注入容器,支持构造函数,属性和方法调用注入.Unity可以处理那些从事基于组件的软件工程的开发人员所面对的问题.构建一个成功应用程序的关键是实现非常松散的耦合 ...

  2. js实现sleep

    1.这种不是匀速, 写到for循环中出现1,2,3.......456....的情况 function sleep(milliseconds) { var start = new Date().get ...

  3. RTP推流及验证

    [时间:2018-07] [状态:Open] [关键词:rtp,rtcp, ffmpeg,ffplay,sdp,h264,mp2,ts,推流] 近期在学习有关RTP/RTCP的资料,发现看了很多资料, ...

  4. Linux中rz和sz命令用法详解

    http://www.111cn.net/sys/linux/67810.htm 在linux中rz 和 sz 命令允许开发板与主机通过串口进行传递文件了,下面我们就来简单的介绍一下rz 和 sz 命 ...

  5. git学习小游戏

    学习git可以试试这个游戏:https://learngitbranching.js.org/

  6. Kafka基本架构及原理

    本文转载自http://www.cnblogs.com/cyfonly/p/5954614.html  一.为什么需要消息系统 1.解耦: 允许你独立的扩展或修改两边的处理过程,只要确保它们遵守同样的 ...

  7. find ctime 加减n时间范围

    看下atime的时间解释:-atime n File was last accessed n*24 hours ago. When find figures out how many 24-hour ...

  8. 【css】垂直居中的几种写法

    结构 <div class="vam"> <div class="vam-body">垂直居中</div> </div ...

  9. mybatis-generator 自动生成查询Vo

    package com.witwicky.plugins; import org.mybatis.generator.api.GeneratedJavaFile; import org.mybatis ...

  10. Flask学习笔记(2)--最简单的小应用

    0x01 第一个小程序 PyCharm新建一个flask项目,第一个小程序,我们来看一下 #引入flask类 from flask import Flask #将Flask对象实例化 app = Fl ...