POJ 1426 - Find The Multiple - [DP][BFS]

题目链接: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[1]=1%n;
        int now;
        for(int i=1;;i++)
        {
            if(!(dp[i*2]=dp[i]*10%n)) {now=i*2;break;}
            if(!(dp[i*2+1]=(dp[i]*10+1)%n)) {now=i*2+1;break;}
        }

        ans.clear();
        while(now)
        {
            ans.push_back(now%2);
            now/=2;
        }
        for(int i=ans.size()-1;i>=0;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(1);
        while(!q.empty())
        {
            ll x=q.front();q.pop();
            ll y=x*10,z=x*10+1;
            if(y%n==0) {cout<<y<<endl;break;}
            if(z%n==0) {cout<<z<<endl;break;}
            q.push(y),q.push(z);
        }
    }
}

原文地址:https://www.cnblogs.com/dilthey/p/9693468.html

时间: 2024-10-04 16:24:49

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

poj 1426 Find The Multiple (BFS)

Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22121   Accepted: 9081   Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains

(简单) 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 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 mo

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

题目传送门 1 /* 2 题意:找出一个0和1组成的数字能整除n 3 DFS:200的范围内不会爆long long,DFS水过~ 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-2 14:21:51 8 File Name :POJ_1426.cpp 9 ******************************************

[题解] [BFS] POJ 1426 - Find The Multiple

p { margin-bottom: 0.25cm; line-height: 115% } a:link { } VJudge题目:https://cn.vjudge.net/contest/279018#problem/L 即POJ 1426 - Find The Multiple:http://poj.org/problem?id=1426 题目要求: 给出一个不大于200的整数n,要求找到任意一个n的倍数(十进制),并且这个倍数只由1与0组成,最多100位. 输入输出:输入以0结束. 示

POJ 1426 Find The Multiple(寻找倍数)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

广搜+打表 POJ 1426 Find The Multiple

POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Accepted: 10613   Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representati

POJ 2356 Find a multiple (dp + 鸽笼原理)

OJ题目:click here~~ 题目分析:n个数,从中取若干个数,和为n的倍数.给出一种取法. 因为只要给出其中一种方案就行,鸽笼原理可以求出取出的数为连续的方案. 关于鸽笼原理,点这里~ 直接贴过来: 有n+1件或n+1件以上的物品要放到n个抽屉中,那么至少有一个抽屉里有两个或两个以上物品. 如果你知道这个结论: a1,a2,a3...am是正整数序列,至少存在整数k和r,1<=k<r<=m,使得ak+a(k+1)+...+a(r)是m的倍数. 证明比较简单: Sk表示前k个数之和

20200202 POJ - 3126 Prime Path POJ - 1426 Find The Multiple

>>>>>>>>>POJ 1426直达?? >>>>>>>>>POJ 3126直达?? 做了这么几道搜索题,感觉差不多也摸出了门路,模板差不多记下来了,只是面对不同的题目算法不同罢了 简单写一下想法,搞明白搜索的主题到底是什么(之前的mutiple那题,一开始就没想明白到底搜谁,就没想到算法 TBC(晚上再写 ===================================分割线=======

poj 1426 Find The Multiple (bfs 搜索)

Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18012   Accepted: 7297   Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains