poj 1426(同余搜索)
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 26926 | Accepted: 11174 | Special Judge |
Description
Input
Output
Sample Input
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111 题意:找到一个由01组成的串m使得 m%n == 0
题解:同余定理.很强的搜索剪枝技巧。
同余剪枝,如果一个小数的余数和大数的余数相同,假设小点的数 a ,大点的为 b ,a%n==b%n => (a+c)%n == (b+c)%n ,我们假设 a+c是符合条件的,那么,b+c 也是符合条件的,所以我们可以直接不要
b+c 了,这就是一个很巧妙并且很强的剪枝.
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
int n;
bool mod[]; ///这里是最强的剪枝,同余剪枝,如果一个小数的余数和大数的余数相同
///假设小点的数 a ,大点的为 b ,a%n==b%n => (a+c)%n == (b+c)%n ,我们假设 a+c是符合条件的,那么
/// b+c 也是符合条件的,所以我们可以直接不要 b+c 了,这就是一个很巧妙并且很强的剪枝.
struct Node{
int mod;
string ans;
};
string bfs(){
memset(mod,false,sizeof(mod));
queue<Node> q;
Node s;
s.ans = "";
s.mod = %n;
q.push(s);
while(!q.empty()){
Node now = q.front();
q.pop();
if(now.mod==){
return now.ans;
}
Node next;
next.mod = (now.mod*+)%n;
next.ans = now.ans+"";
if(!mod[next.mod]){
mod[next.mod] = true;
q.push(next);
}
next.mod = (now.mod*)%n;
next.ans = now.ans+"";
if(!mod[next.mod]){
mod[next.mod] = true;
q.push(next);
}
}
}
int main(){ while(scanf("%d",&n)!=EOF,n){
cout<<bfs()<<endl;
}
}
poj 1426(同余搜索)的更多相关文章
- 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: 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 (BFS)
POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...
- hdu 1226(同余搜索)
超级密码 Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- DFS/BFS(同余模) POJ 1426 Find The Multiple
题目传送门 /* 题意:找出一个0和1组成的数字能整除n DFS:200的范围内不会爆long long,DFS水过~ */ /************************************ ...
- POJ 1426 Find the Multiple 思路,线性同余,搜索 难度:2
http://poj.org/problem?id=1426 测试了一番,从1-200的所有值都有long long下的解,所以可以直接用long long 存储 从1出发,每次向10*s和10*s+ ...
- POJ - 1426 Find The Multiple(搜索+数论)
转载自:優YoU http://user.qzone.qq.com/289065406/blog/1303946967 以下内容属于以上这位dalao http://poj.org/problem? ...
随机推荐
- Ark组件[转]
Ark组件简介 Ark组件是基于.NET 4.0框架开发的基础组件,封装了一些常用的功能方法,并提供了若干程序开发的基础框架. HttpSession简介 HttpSession是Ark组件中负责HT ...
- BZOJ1999 NOIP2007 洛谷P1099 P2491 SDOI 2011
Description: 设T=(V, E, W) 是一个无圈且连通的无向图(也称为无根树),每条边到有正整数的权,我们称T为树网(treebetwork),其中V,E分别表示结点与边的集合,W表示各 ...
- MyEclipse下项目的包层次结构调整
新电脑安装完MyEclipse,导入项目后发现MyEclipse下项目的包层次结构变成了Flat,平面模式,这种模式感觉特别不好, 不能清晰地显示出项目的包层次结构.这样,显示出的包的结构不够明显,我 ...
- 使用Faster-Rcnn进行目标检测(实践篇)转载
原 文链接:http://blog.csdn.net/gavin__zhou/article/details/52052915 原理 上一篇文章,已经说过了,大家可以参考一下,Faster-Rcnn进 ...
- Python图像处理库(PIL)
官方:(详细)http://pillow.readthedocs.io/en/3.1.x/reference/ImageDraw.html http://pillow.readthedocs.io/e ...
- 如何用好 github 中的 watch、star、fork
http://www.jianshu.com/p/6c366b53ea41 https://www.zhihu.com/question/20431718 在每个 github 项目的右上角,都有三个 ...
- Uva-oj Palindromes 暴力
Palindromes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Statu ...
- mybatis主键返回的实现
向数据库中插入数据时,大多数情况都会使用自增列或者UUID做为主键.主键的值都是插入之前无法知道的,但很多情况下我们在插入数据后需要使用刚刚插入数据的主键,比如向两张关联表A.B中插入数据(A的主键是 ...
- HDU3336 KMP+DP
Count the string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- phantomjs 无法打开https网站
最近一直在用phantomjs 自动登陆并爬取一些数据,突然发现爬取https类型的网站的时候无法正常操作了 困扰了两天的问题在经过google和stackoverflow的一番搜索后发现原来Phan ...