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 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

Source

開始做这道题的时候半天没有读懂题意,例子数据是吓人的,把一个单词理解错误,multiple在这里是倍数的意思。这个题目就是要求我们求出给定的一个数的一个由0,1组成的倍数,100位也是吓人的。開始看那个例子,全然不好理解啊。看了半天没有理解题意,也想不到要用bfs来做。看了别人的一点提示就清晰啦。就用一个队列来模拟运算的过程,假设能够整除就直接输出,不能整除把他*10,和*10+1入队;
由于都是0,1组成的倍数,所以组成直接*10。或者是*10+1;
以下是用stl写的,第一次用stl写。有点水,用stl在poj上c++过不了,g++才过的;
#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
void bfs(int n)
{
queue<__int64>q;//这里后面的数据可能会有大的,所以用个64位的就够了
q.push(1);
while(!q.empty())
{
__int64 x;
x=q.front();
q.pop();
if(x%n==0)//能够整除就直接输出
{
printf("%I64d\n",x);
return ;
}
q.push(x*10);//把x的10的倍数入队。
q.push(x*10+1);
}
}
int main()
{
int n;
while(scanf("%d",&n)&&n)
{
bfs(n);
}
return 0;
}

自己用数组模拟队列写了一下;貌似效率比stl好了一些;

主要的思想都是一样的;
#include <cstdio>
#include <cstring>
long long q[2000000];
void bfs(int n)
{
int front=0;
int rear=0;
q[front]=1;
rear++;
long long temp;
while(rear>front)
{
temp=q[front];
if(temp%n==0)
{
break;
}
temp*=10;
q[rear]=temp;
rear++;
q[rear]=temp+1;
rear++;
front++;
}
printf("%lld\n",temp);
}
int main()
{
int n;
while(scanf("%d",&n)&&n)
{
bfs(n);
}
return 0;
}

在网上还看到了大神的代码用了同余取模定理,还是有点没看懂。还有的直接用满二叉树模拟的队列。

(a*b)%n = (a%n *b%n)%n

(a+b)%n = (a%n +b%n)%n



看到其它大神对这道的思路:

再贴一段pl大牛关于此题的分析思路。以学习:

要m整除n,那么能够用对n的余数来表示当前的状态。

搜到一个余数为0的状态就能够了。

直接bfs出去。

假设当前的余数是r。添在当前答案后面的数为,k

那么新的余数。也就是新的状态为:

( r * 10 + k ) % n 。

这样最多200个状态,判重一下。能够非常快出解。

大牛的代码:

#include<iostream>
using namespace std;
int a[524300],i,n;
int main(){
while(cin>>n){
if (!n) break;
i=1; a[1]=1%n;
while(a[i]){i++; a[i]=(a[i/2]*10+i%2)%n;}
n=0; while(i){a[n++]=i%2;i>>=1;}
while(n--) cout<<a[n]; cout<<endl;
}
return 0;
}

这原来也是bfs,直接用一个满二叉树实现(左儿子是0,右儿子是1);

还是要多学习大神们的代码~





poj 1426 Find The Multiple (bfs 搜索)的更多相关文章

  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(搜索+数论)

    转载自:優YoU  http://user.qzone.qq.com/289065406/blog/1303946967 以下内容属于以上这位dalao http://poj.org/problem? ...

  3. POJ 1426 Find The Multiple BFS

    没什么好说的 从1开始进行广搜,因为只能包涵0和1,所以下一次需要搜索的值为next=now*10 和 next=now*10+1,每次判断一下就可以了,但是我一直不太明白我的代码为什么C++提交会错 ...

  4. poj 1426 Find The Multiple ( BFS+同余模定理)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18390   Accepted: 744 ...

  5. POJ.1426 Find The Multiple (BFS)

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

  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. k8s的Dashboard

     前面章节 Kubernetes 所有的操作我们都是通过命令行工具 kubectl 完成的.为了提供更丰富的用户体验,Kubernetes 还开发了一个基于 Web 的 Dashboard,用户可以用 ...

  2. 初始github——git的简单使用

    初学者~ 有两篇吧,一篇在github上  https://github.com/DefaultYuan/Git-Pro/wiki/Introduction 文章来源:<git的简单使用> ...

  3. ZCMU训练赛-A(模拟)

    A - Applications https://vjudge.net/contest/174208#overview Recently, the ACM/ICPC team of Marjar Un ...

  4. Codeforces 702D Road to Post Office(模拟 + 公式推导)

    题目链接:http://codeforces.com/problemset/problem/702/D 题意: 一个人要去邮局取东西,从家到达邮局的距离为 d, 它可以选择步行或者开车,车每走 k 公 ...

  5. hdu6086(AC 自动机)

    hdu6086 题意 字符串只由 \(01\) 组成,求长度为 \(2L\) 且包含给定的 \(n\) 个子串的字符串的个数(且要求字符串满足 \(s[i] \neq s[|s| - i + 1]\) ...

  6. 索引(Index)

    无索引的表就是一个无序的行集.比如下面的人员表中有一些数据: 这个表上没有索引,因此如果我们查找年龄等于17的人员时,必须查看表中的每一行,看它是否与所需的值匹配.这是一个全表扫描,很慢,如果表中只有 ...

  7. 微信小程序开发教程(八)视图层——.wxml详解

    框架的视图层由WXMKL(WeiXin Markup language)与WXSS(WeiXin Style Sheet)编写,由组件进行展示. 对于微信小程序而言,视图层就是所有.wxml文件与.w ...

  8. Ze_Min Tree 主席树

    前言 主席树,也叫可持久化线段树,所以他的本质是颗线段树,而可持久化指的是这颗线段树可以访问过去某个时刻线段树上的信息. 应用 应用的比较多的是查询区间的第k大值(因为其他的数据结构不好做). 实现 ...

  9. 【枚举】【前缀和】【map】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals

    处理出前缀和,枚举k的幂,然后从前往后枚举,把前面的前缀和都塞进map,可以方便的查询对于某个右端点,有多少个左端点满足该段区间的和为待查询的值. #include<cstdio> #in ...

  10. boost 1.57 vs2013 编译

    启动vs2013中的命令行注意区分32/64, 进入boost目录,  再次运行 bootstrap.bat 编译: bjam.exe stage --toolset=msvc-12.0  --sta ...