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,由 ...
随机推荐
- Socket 一对多通信
服务器(TCPServer.java): package visec; import java.net.*; import java.io.*; public class TCPServer{ pub ...
- Temporary-Post-Used-For-Style-Detection-Title-1901742601
Temporary-Post-Used-For-Style-Detection-Content-1901742601
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树
题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...
- Mybatis错误调试(二)
错误日志信息: Caused by: java.sql.BatchUpdateException: ORA-00911: 无效字符 at oracle.jdbc.driver.OraclePrepa ...
- Java中HashMap排序
注: 转载于 http://www.cnblogs.com/xingyun/archive/2012/12/09/2809962.html package com.holdobject; import ...
- 【POJ】【2601】Simple calculations
推公式/二分法 好题! 题解:http://blog.csdn.net/zck921031/article/details/7690288 这题明显是一个方程组……可以推公式推出来…… 然而这太繁琐了 ...
- GS LiveMgr心跳管理类
struct LiveMgr { private: int m_nCount; ///< 管理数量 std::vector<int> m_vecChannels; ///< 所 ...
- IP分片浅析
在TCP/IP分层中,数据链路层用MTU(Maximum Transmission Unit,最大传输单元)来限制所能传输的数据包大小,MTU是指一次传送的数据最大长度,不包括数据链路层数据帧的帧头, ...
- C++中static的全部作用
要理解static,就必须要先理解另一个与之相对的关键字,很多人可能都还不知道有这个关键字,那就是auto,其实我们通常声明的不用static修饰的变量,都是auto的,因为它是默认的,就象short ...
- 【QT】找茬外挂制作
找茬外挂制作 找茬游戏大家肯定都很熟悉吧,两张类似的图片,找里面的不同.在下眼神不大好,经常瞪图片半天也找不到区别.于是乎决定做个辅助工具来解放一下自己的双眼. 一.使用工具 Qt:主要是用来做界面的 ...