题意:给一些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 + 同余剪枝的更多相关文章

  1. HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )

    没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib&g ...

  2. (简单) 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 ...

  3. NOIP 模拟 玩积木 - 迭代加深搜索 / bfs+hash+玄学剪枝

    题目大意: 有一堆积木,0号节点每次可以和其上方,下方,左上,右下的其中一个交换,问至少需要多少次达到目标状态,若步数超过20,输出too difficult 目标状态: 0 1 1 2 2 2 3 ...

  4. HDU 1104 Remainder(BFS 同余定理)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1104 在做这道题目一定要对同余定理有足够的了解,所以对这道题目对同余定理进行总结 首先要明白计算机里的 ...

  5. spoj Goblin Wars(简单bfs)

    J - Goblin Wars Time Limit:432MS    Memory Limit:1572864KB    64bit IO Format:%lld & %llu Submit ...

  6. JZYZOJ1442 [noip2013]华容道 bfs 最短路 剪枝

    http://172.20.6.3/Problem_Show.asp?id=1442 想到最短路的简直神了,如果我写我大概只能写一个30分的bfs. 从数据范围可以看出思路是bfs剪枝,但这里的剪枝是 ...

  7. hdu 1689 Alien’s Necklace (bfs层次图剪枝)

    Alien's Necklace Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  8. hdu 4400 离散化+二分+BFS(暴搜剪枝还超时的时候可以借鉴一下)

    Mines Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  9. ACM/ICPC 之 BFS范例(ZOJ2913-ZOJ1136(POJ1465))

    通过几道经典BFS例题阐述BFS思路 ZOJ2913-Bus Pass 题意:找一个center区域,使得center到所有公交线路最短,有等距的center则输出id最小的. 题解:经典的BFS,由 ...

随机推荐

  1. java 24点算法实现

    最近闲来无事,突然怀念起小时候和堂兄表姐们经常玩24点游戏,于是就琢磨着是不是开发一个安卓手机版本.然后上网上一搜,发现已经被别人给开发烂了啊.不过这只能说明这个小游戏要想赚广告费很难了,但是拿来锻炼 ...

  2. android开发 ,对接支付宝,服务器(PHP)校验失败

    已备忘记,资料链接: http://my.oschina.net/u/256646/blog/174222 注意: 里面有一个设计到支付宝公钥的地方: 注 意这个是2048位的公钥应该是9行或者10行 ...

  3. timersmanager 解析

    最近在看crtmp源代码,看到timersmanager 模块时感觉很难理解,花了不少时间反复思考该模块 的逻辑,现在思考的结果记录下来,方便以后查阅. 构造函数中将处理时间方法传进来,将_lastT ...

  4. adb出现unkown host advices 错误

    今日在Windows DOS窗口中输入adb命令,如adb devices,adb shell等后,会出现如下错误: adb server is out of date.  killing... AD ...

  5. 《JavaScript高级程序设计》第5章 引用类型

    5.2.2 转换方法 所有对象都有toString()和valueOf()方法调用数组的toString()方法,会返回一个字符串,由数组中的每个项通过逗号连接而成调用valueOf()还是返回数组 ...

  6. hibernate---ID生成策略

    5.1.4.1. Generator 可选的<generator>子元素是一个Java类的名字, 用来为该持久化类的实例生成唯一的标识.如果这个生成器实例需要某些配置值或者初始化参数, 用 ...

  7. matrix_last_acm_5

    password 123 A http://acm.hust.edu.cn/vjudge/contest/view.action?cid=97960#problem/A 题意:给国王生日可能区间[a, ...

  8. poj 3254

    状态压缩 dp dp[i][j] 为第 i 行状态为 j 的总数 #include <cstdio> #include <cstdlib> #include <cmath ...

  9. eclipse 自动 注释

    在使用Eclipse 编写Java代码时,自动生成的注释信息都是按照预先设置好的格式生成的. 修改作者.日期注释格式:打开Windows->Preferences->Java->Co ...

  10. xxx.properties获取方法

    1.手动获取加载 Locale locale = Locale.getDefault(); ResourceBundle localResource = ResourceBundle.getBundl ...