POJ-1426-Find the multiply
这题深搜广搜都可以做,深搜的做法就是把每个由1 和 0 组成的数字拓展10倍以及拓展10倍+1,然后压入队列。
这样可以走过所有由10组成的数字,且两个方向平行发展(*10 +0和+1)。
bfs
#include <cstdio>
#include <queue>
using namespace std;
long long n, m; void bfs()
{
queue<long long> q;
q.push();
while (!q.empty()) {
long long k = q.front();
if (k%n==) {
m = k;
break;
}
q.pop();
q.push(k * );
q.push(k * + );
}
} int main()
{
while (scanf("%lld",&n)&&n) {
bfs();
printf("%lld\n", m);
}
return ;
}
dfs的话,我们就要考虑这个数是不是会超范围,long long是64位的,然后有符号类型的最大十进制数是19位的,所以当我们搜到
18位的时候,我们就可以退出了,因为这个方向搜不到,另外的一个+1方向肯定搜得到。
当我们搜到之后,要把flag标记为1,然后让所有的递归都退出,然后也是每次每个数都向两个方向搜索,不过就是单个数是先搜到
18位数字为顶,然后才返回,在没找到这个数的情况下。
dfs
#include <cstdio>
#include <queue>
using namespace std;
long long n, m, f; void dfs(long long k,int bit)
{
if (f==)
return;
if (k%n==) {
m = k;
f = ;
return;
}
if (bit==)
return;
dfs(k * ,bit+);
dfs(k * + ,bit+);
} int main()
{
while (scanf("%lld",&n)&&n) {
f = ;
dfs(,);
printf("%lld\n", m);
}
return ;
}
POJ-1426-Find the multiply的更多相关文章
- 广搜+打表 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 --- BFS || DFS
POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...
- POJ 1426 Find The Multiple(寻找倍数)
POJ 1426 Find The Multiple(寻找倍数) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Given ...
- 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
http://poj.org/problem?id=1426 一道广搜的题目. 题意就是给你一个n,要你求出n的倍数中,只存在0和1的那个数字 所谓的只存在0和1,那么就是某个数的十倍或者十倍+1,而 ...
- poj 1426 Find The Multiple( bfs )
题目:http://poj.org/problem?id=1426 题意:输入一个数,输出这个数的整数 倍,且只有0和1组成 程序里写错了一个数,结果一直MLE.…… #include <ios ...
- POJ 1426 Find The Multiple(数论——中国同余定理)
题目链接: http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find ...
- 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 nonzer ...
- POJ - 1426 Find The Multiple(搜索+数论)
转载自:優YoU http://user.qzone.qq.com/289065406/blog/1303946967 以下内容属于以上这位dalao http://poj.org/problem? ...
随机推荐
- p标签间距问题
用<p></p>标签写文本时,控制行与行之间的高度最好用line-height,不要用margin或padding: 因为P标签本身就带有一定的上下间距,且自带的间距在模拟 ...
- [题解](折半搜索)luogu_P4799_BZOJ_4800世界冰球锦标赛
抄的题解 以及参考:https://www.cnblogs.com/ZAGER/p/9827160.html 2^40爆搜过不了,考虑折半搜索,难点在于合并左右的答案,因为有可能答案同时载左右两边,我 ...
- Codeforces Round #563 (Div. 2) C. Ehab and a Special Coloring Problem
链接:https://codeforces.com/contest/1174/problem/C 题意: You're given an integer nn. For every integer i ...
- K.河北美食
链接:https://ac.nowcoder.com/acm/contest/903/K 题意: icebound最喜欢吃河北菜,于是他想要大厨做一桌河北菜宴请宾客.icebound购买了一些食材,并 ...
- Codeforces Round #431 (Div. 2) B
Connect the countless points with lines, till we reach the faraway yonder. There are n points on a c ...
- 矩形面积并-扫描线 线段树 离散化 模板-poj1151 hdu1542
今天刚看到这个模板我是懵逼的,这个线段树既没有建树,也没有查询,只有一个update,而且区间成段更新也没有lazy标记....研究了一下午,我突然我发现我以前根本不懂扫描线,之所以没有lazy标记, ...
- Uvalive-4494-(数位dp)
题意:求a->b中的二进制出现过多少个1,很显然的数位dp,对于某一位来说,如果这位是0,那么dp[i]=dp[i-1] 如果这一位是1 那么dp[i]=dp[i-1]+1<<(p ...
- 解决lnmp无法远程登录的bug
使用lnmp一键安装好linux环境后,不能远程连接到所在服务器的mysql,但是通过80端口可以登录phpmyadmin,发现原来是因为lnmp的大多数版本为了安全不禁止远程连接mysql,方法很简 ...
- jdbc查询
import java.util.ArrayList; import java.util.List; import org.springframework.jdbc.core.BeanProperty ...
- NET Core 模块化,多租户框架
NET Core 模块化,多租户框架 Orchard Core Framework:ASP.NET Core 模块化,多租户框架 上一篇编写Orchard Core一分钟搭建ASP.NET Core ...