poj 1426 Find The Multiple (bfs 搜索)
| Time Limit: 1000MS | Memory Limit: 10000K | |||
| Total Submissions: 18012 | Accepted: 7297 | Special Judge | ||
Description
digits.
Input
Output
Sample Input
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111
Source
#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 搜索)的更多相关文章
- POJ 1426 Find The Multiple --- BFS || DFS
POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...
- POJ - 1426 Find The Multiple(搜索+数论)
转载自:優YoU http://user.qzone.qq.com/289065406/blog/1303946967 以下内容属于以上这位dalao http://poj.org/problem? ...
- POJ 1426 Find The Multiple BFS
没什么好说的 从1开始进行广搜,因为只能包涵0和1,所以下一次需要搜索的值为next=now*10 和 next=now*10+1,每次判断一下就可以了,但是我一直不太明白我的代码为什么C++提交会错 ...
- poj 1426 Find The Multiple ( BFS+同余模定理)
Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18390 Accepted: 744 ...
- POJ.1426 Find The Multiple (BFS)
POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...
- DFS/BFS(同余模) POJ 1426 Find The Multiple
题目传送门 /* 题意:找出一个0和1组成的数字能整除n DFS:200的范围内不会爆long long,DFS水过~ */ /************************************ ...
- 广搜+打表 POJ 1426 Find The Multiple
POJ 1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25734 Ac ...
- POJ 1426 Find The Multiple(寻找倍数)
POJ 1426 Find The Multiple(寻找倍数) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given ...
- POJ 1426 Find The Multiple (DFS / BFS)
题目链接:id=1426">Find The Multiple 解析:直接从前往后搜.设当前数为k用long long保存,则下一个数不是k*10就是k*10+1 AC代码: /* D ...
随机推荐
- 回车和换行有什么区别?我们平时按下的Enter键是回车还是换行?
来源:http://www.52rd.com/blog/Detail_RD.Blog_imjacob_12317.html -------------------------------------- ...
- Java 5大内存区域和对象的创建过程
1.Java运行时数据区 方法区,堆线程共享.虚拟机栈,本地方法栈和程序计数器线程私有. 2.程序计数器(PC计数器) 占用较小的一块内存空间,当执行Java方法时记录正在执行的虚拟机字节码指令地址, ...
- Go嵌入类型及内部提升样例
这个有点新鲜哟... package main import ( "fmt" ) type notifier interface { notify() } type user st ...
- magento 搬家
1.删除浏览记录: log_customer log_visitor log_visitor_info log_url log_url_info log_quote report_viewed_pro ...
- 转 IntelliJ IDEA 快捷键
https://www.cnblogs.com/clwydjgs/p/9390488.html 一.视图查看 Ctrl+F12 查看file,method结构图.类继承机构图 (不知道方法结构,Ctr ...
- RPD Volume 168 Issue 4 March 2016 评论3
Design and fabrication of a multipurpose thyroid phantom for medical dosimetry and calibration Abs ...
- java中的3大特性之继承
继承的特点:继承父类的属性和方法.单继承(多层继承)c++里的继承是多继承 特性 :方法的复写(重写) java中的继承和OC中一样. 比如:人可以养狗; 人---->狗 :整体和部分(拥有)关 ...
- 微信小程序开发教程(一)准备
1.成为微信公众平台开发者 成为微信公众平台的开发者,是小程序开发的首要条件.只有成为微信公众平台的开发者,才可以使用公众平台的各种开发接口.如果你已经是开发者,则可以跳过本章. ①.进入微信公众平台 ...
- 【并查集】【set】AtCoder - 2159 - 連結 / Connectivity
Problem Statement There are N cities. There are also K roads and L railways, extending between the c ...
- 【莫队算法】bzoj3781 小B的询问
莫队经典. 开个数组维护a[i]出现的次数. #include<cstdio> #include<cmath> #include<algorithm> using ...