zoj 1530 Find The Multiple(bfs)
来源:互联网 发布:手机版电音制作软件 编辑:程序博客网 时间:2024/05/23 02:15
题意:
求n的倍数m,要求m中只能有0或者1.即构造一个只有0,1,组成的整数并且可以整除n.
YY:
本题有多种做法,dfs,bfs,dp。bfs里有数论做法和zoj 1889 1136类似。但我还是喜欢用bfs做,直观又简单。…………
我是因为要做广搜才去碰这题目的。一开始真的是没有想法,怎么搜?搜什么?
后来在纸上画了个类似二叉树的01序列,貌似有点明白怎么个bfs法。
但是一开始写出的bfs太挫了。MLE,还漏掉了很多情况。
如下(吸取教训)
#include <string.h>#include <iostream>#include <queue>#include <cstdio>using namespace std;queue<long long>Q;void bfs(long long n){long long i;long long s=0;Q.push(1);while(!Q.empty ()){i=Q.front ();Q.pop ();s=s*10+i;if(s%n==0) break;Q.push (1);Q.push (0);}printf("%lld\n",s);}int main(){long long n;while(scanf("%lld",&n)!=EOF){if(n==0) break;bfs(n);}return 0;}
在无限的MLE (不断压入,使节点过多爆了空间)中偶参考了下牛人的代码,发现是直接在压入队列的时候才对s进行处理,这样避免了情况的漏考虑,
#include <string.h>#include <iostream>#include <queue>#include <cstdio>using namespace std;queue<long long>Q;void bfs(long long n){long long i;long long s=0;Q.push(1);while(!Q.empty ()){s=Q.front ();if(s%n==0) break;Q.pop ();Q.push (s*10);Q.push (s*10+1);}printf("%lld\n",s);}int main(){long long n;while(scanf("%lld",&n)!=EOF){if(n==0) break;while(!Q.empty ()) Q.pop ();bfs(n);}return 0;}
参考链接:
dfs 做法:http://hi.baidu.com/vivyli/blog/item/21b549dc3c22cba5cd1166d4.html
数论做法:http://hi.baidu.com/ly01kongjian/blog/item/d21e666f4734f5f1421694a8.html
- zoj 1530 Find The Multiple(bfs)
- zoj 1530 Find The Multiple
- ZOJ 1530 find the multiple 找倍数
- POJ 1426/ ZOJ 1530 Find The Multiple
- BFS | 1426 | Find The Multiple
- POJ1426 Find The Multiple BFS
- POJ1426 Find The Multiple(bfs)
- BFS POJ1426 Find The Multiple
- Find The Multiple (BFS)
- POJ 1426 Find The Multiple bfs
- POJ1426-Find The Multiple (BFS 余数)
- poj 1426 Find The Multiple(bfs)
- POJ1426:Find The Multiple(BFS)
- Find The Multiple (poj 1426 bfs)
- poj 1426 Find The Multiple(bfs)
- POJ 1426-Find The Multiple(bfs)
- C - Find The Multiple(BFS)
- POJ 1426 Find The Multiple(BFS)
- UIViewController Class Reference
- 3g:如何查找一个字符串中重复次数最多的字符,返回这个字符的个数和字符
- Ibatis3中基于接口,class对象调用的例子
- DP HDU 1494
- 计划-USACO
- zoj 1530 Find The Multiple(bfs)
- 1.1.1 Your Ride Is Here
- 3g:Get与Post,Head的区别
- 插入排序
- mysql获取刚插入(添加)记录的自动编号id
- 老师的表扬。
- poj 2387 Til the Cows Come Home spfa基础题,入门,我的第一个
- MY---第二天(香车美女)
- http://projecteuler.net/problem=4 【参考解法】