SPOJ 370 Ones and zeros BFS + 同余剪枝
题意:给一些n,求出最小的只包含0,1的n的倍数
设两数a, b满足: a < b 并且a % n = b % n。
如果 ( a * 10^x + c ) % n = z , 根据同余定理,( b * 10^x + c ) % n 也等于 z。
b的情况其实与a相同,如果a不符合条件,那么b一定不符合条件。
因此我们在搜索时,从1开始,每次往后添加0或1,如果得到的数与之前得到的某数同余,则扔掉,否则放入队列继续搜索。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue> using namespace std; const int MAXN = ; struct node
{
int val;
int pre;
}; int n;
int ans[MAXN];
node D[MAXN]; void solved()
{
memset( D, -, sizeof(D) );
queue<int> Q;
Q.push();
D[].val = ;
D[].pre = -; while ( !Q.empty() )
{
int cur = Q.front();
Q.pop();
if ( cur == )
break;
//printf( "%d\n", cur ); bool find = false;
for ( int i = ; i < ; ++i )
{
int tp = ( cur * + i ) % n;
if ( D[tp].val == - )
{
D[tp].val = i;
D[tp].pre = cur;
Q.push( tp );
}
if ( tp == )
{
find = true;
break;
}
}
if ( find ) break;
}
return;
} bool GetStart()
{
char str[];
sprintf( str, "%d", n );
int len = strlen(str);
for ( int i = ; i < len; ++i )
if ( str[i] != '' && str[i] != '' )
return false;
return true;
} int main()
{
int T;
scanf( "%d", &T );
while ( T-- )
{
scanf( "%d", &n );
if ( GetStart() ) printf( "%d\n", n );
else
{
solved();
int cnt = ;
for ( int i = ; i != -; i = D[i].pre )
ans[cnt++] = D[i].val;
for ( int i = cnt - ; i >= ; --i )
printf( "%d", ans[i] );
puts("");
}
}
return ;
}
SPOJ 370 Ones and zeros BFS + 同余剪枝的更多相关文章
- HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )
没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib&g ...
- (简单) POJ 1426 Find The Multiple,BFS+同余。
Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...
- NOIP 模拟 玩积木 - 迭代加深搜索 / bfs+hash+玄学剪枝
题目大意: 有一堆积木,0号节点每次可以和其上方,下方,左上,右下的其中一个交换,问至少需要多少次达到目标状态,若步数超过20,输出too difficult 目标状态: 0 1 1 2 2 2 3 ...
- HDU 1104 Remainder(BFS 同余定理)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1104 在做这道题目一定要对同余定理有足够的了解,所以对这道题目对同余定理进行总结 首先要明白计算机里的 ...
- spoj Goblin Wars(简单bfs)
J - Goblin Wars Time Limit:432MS Memory Limit:1572864KB 64bit IO Format:%lld & %llu Submit ...
- JZYZOJ1442 [noip2013]华容道 bfs 最短路 剪枝
http://172.20.6.3/Problem_Show.asp?id=1442 想到最短路的简直神了,如果我写我大概只能写一个30分的bfs. 从数据范围可以看出思路是bfs剪枝,但这里的剪枝是 ...
- hdu 1689 Alien’s Necklace (bfs层次图剪枝)
Alien's Necklace Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)
Mines Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- ACM/ICPC 之 BFS范例(ZOJ2913-ZOJ1136(POJ1465))
通过几道经典BFS例题阐述BFS思路 ZOJ2913-Bus Pass 题意:找一个center区域,使得center到所有公交线路最短,有等距的center则输出id最小的. 题解:经典的BFS,由 ...
随机推荐
- hibernate.cfg.xml 配置(摘录)
配置文件中映射元素详解 对象关系的映射是用一个XML文档来说明的.映射文档可以使用工具来生成,如XDoclet,Middlegen和AndroMDA等.下面从一个映射的例子开始讲解映射元素,映射文件的 ...
- C#制作高仿360安全卫士窗体<一>
开始写这一系列博客之前先要向大家说声抱歉,放肆雷特建立很久却很少有更新.当然博客人气也不旺,大部分都是看的人多评论收藏的人少.一直想要改变这种状态,正好赶上了最近工作上做了一个高仿360安全卫士窗体. ...
- AxMicrosoft.Office.Interop.Owc11.AxSpreadsheet控件在C#中的引用
这几天要是用AxMicrosoft.Office.Interop.Owc11.AxSpreadsheet控件做查询,发现一系列问题,一点点记录下来吧,以备后查: 第一.相关属性:http://www. ...
- 01.JSP基础语法
本章主要讲解Java Web与JSP的入门内容,适合有JSP或Java Web基础的读者学习. 1.Web应用与web.xml文件 (1)Java Web应用程序的结构 Java We ...
- poj 1325 Machine Schedule 最小点覆盖
题目链接:http://poj.org/problem?id=1325 As we all know, machine scheduling is a very classical problem i ...
- C#中实现VB中的CreateObject方法
经常看到有些VB的例子中直接用个CreateObject就可调用系统功能(大多是COM对象),像用户设定,网络设定等等.虽然C#中可以通过使用VB的命名空间的方法来调用CreateObject函数,但 ...
- BZOJ 2038
基础不牢:补莫队算法: 莫队算法入门题: 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 26 ...
- javascript中关于坐标 大小 的描述
window对象 有效桌面的大小,除去桌面下面的任务栏的高度 window.screen.availHeight : window.screen.availWidth : 浏览器窗口的左上角相对于 ...
- Sqli-labs less 23
第二部分/page-2 Advanced injection Less-23 Sql语句为$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1 ...
- Ambient Occlusion
一般在光照模型中,ambient light的计算方法为:A = l * m,其中l表示表面接收到的来自光源的ambient light的总量,而m表示表面接收到ambient light后,反射和吸 ...