HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )
没什么巧办法,直接搜就行。
用余数作为每个节点的哈希值。
#include <cstdio>
#include <cstring>
#include <cstdlib> const int MAXN = ; struct node
{
int mod;
int fa;
int digit;
node() {}
node( int mod, int fa, int dig ):mod(mod), fa(fa), digit(dig) { }
}; int N;
bool ok[];
bool vis[MAXN];
node Q[MAXN << ]; void findFa( int u )
{
if ( u == - ) return;
findFa( Q[u].fa );
printf( "%d", Q[u].digit );
} int BFS()
{
int head = ;
int tail = ;
memset( vis, false, sizeof(vis) ); for ( int i = ; i < ; ++i )
{
if ( !ok[i] ) continue;
int hashh = i % N;
if ( vis[hashh] ) continue;
vis[hashh] = true;
Q[tail] = node( hashh, -, i );
if ( hashh == ) return tail;
++tail;
} while ( head < tail )
{
node cur = Q[head];
for ( int i = ; i < ; ++i )
{
if ( !ok[i] ) continue;
int hashh = ( cur.mod * + i ) % N; if ( vis[hashh] ) continue;
vis[hashh] = true;
Q[tail] = node( hashh, head, i );
if ( hashh == ) return tail;
++tail;
}
++head;
}
return -;
} int main()
{
int cas = ;
while ( scanf( "%d", &N ) == )
{
memset( ok, true, sizeof(ok) );
int n;
scanf( "%d", &n );
for ( int i = ; i < n; ++i )
{
int digit;
scanf( "%d", &digit );
ok[digit] = false;
} printf( "Case %d: ", ++cas );
int ans = BFS();
if ( ans != - ) findFa( ans );
else printf("-1"); puts("");
}
return ;
}
HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )的更多相关文章
- HDU 4474 Yet Another Multiple Problem BFS
题意:求m的倍数中不包含一些数码的最小倍数数码是多少.比如15 ,不包含0 1 3,答案是45. BFS过程:用b[]记录可用的数码.设一棵树,树根为-1.树根的孩子是所有可用的数码,孩子的孩子也是 ...
- HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】
Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- hdu 4474 Yet Another Multiple Problem
题意: 找到一个n的倍数,这个数不能含有m个后续数字中的任何一个 题解: #include<stdio.h> #include<string.h> #include<qu ...
- Yet Another Multiple Problem(bfs好题)
Yet Another Multiple Problem Time Limit : 40000/20000ms (Java/Other) Memory Limit : 65536/65536K ( ...
- (简单) 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 ...
- hdu 4474 大整数取模+bfs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4474 (a*10+b)%c = ((a%c)*10+b%c)%c; 然后从高位开始枚举能填的数字填充, ...
- hdu 1689 Alien’s Necklace (bfs层次图剪枝)
Alien's Necklace Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- SPOJ 370 Ones and zeros BFS + 同余剪枝
题意:给一些n,求出最小的只包含0,1的n的倍数 设两数a, b满足: a < b 并且a % n = b % n. 如果 ( a * 10^x + c ) % n = z , 根据同余定理,( ...
- HDU-4471 Yet Another Multiple Problem (BFS+路径还原)
Problem Description There are tons of problems about integer multiples. Despite the fact that the to ...
随机推荐
- clear:both;和overflow:hidden;的应用理解。
摘自cbwcwy 前辈: clear是子模块之间限定的,如下:<div id="a"> <div id="1"></div& ...
- ImportError: No module named images
[问题] 在使用学习wxPython时,一个Dem抱有如题所示错误 [解决] images 只不过是wxpython自带demo中的一个文件 体验wxpython IN action的时候Import ...
- java设计模式——迭代器模式
一. 定义与类型 定义:提供一种方法,顺序访问一个集合对象中的各个元素,而又不暴露该对象的内部表示 类型:行为型. 二. 使用场景 (1) 访问一个集合对象的内容而无需暴露它的内部表示 (2) 为遍 ...
- 【php】关于trim,rtrim,ltrim,substr 的字符串切割导致 json,_encode无法 识别数据的问题
示例 <?php $a = rtrim('南宁 .',' .'); echo $a; //输出 南�� echo json_encode($a); //输出空白 $b = ['name'=> ...
- 改进的平台设备驱动——dev和drv完全分离
这是平台设备: 1 #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> # ...
- C语言进阶—— 逻辑运算符分析15
印象中的逻辑运算符: ---学生:老师,在我的印象中,逻辑运算符用在条件判断的时候,真挺简单的,还有必要深究吗? ---老师:逻辑运算符确实在条件判断的时候用的比较多,但是并不能说简单... 请思考下 ...
- 笔记-select,poll,epoll
笔记-select,poll,epoll 1. I/O多路复用 I/O多路复用是指:通过一种机制或一个进程,可以监视多个文件描述符,一旦描述符就绪(写或读),能够通知程序进行相应的读写操作. ...
- PHP代码审计3-SQL注入,CSRF,动态函数执行与匿名函数执行,unserialize 反序列化漏洞,变量覆盖,文件管理,文件上传
SQL注入 审计语句 [输入参数] SELECT,DELETE,UPDATE,INSERT 防御 转义: 1.开启gpc:判断解析用户提示的数据 2.mysql_real_escape_string( ...
- Java-数据结构之二叉树练习
本来这个随笔应该在4月17号就应该发出来的.不巧的是,那天晚上收到了offer,然后接下去两天为入职到处跑,20号入职后一直忙,直到最近几天才有时间看看书.然而20多天前就看完的了二叉树,然后17号那 ...
- sql里的多行多列转一行多列小技巧
---恢复内容开始--- [ 今天下午接受了一个紧急小任务,是将一组比赛记录统计出来,将象棋游戏玩家的两条记录在一行里面显示,进数据库看之后是首先想到的是行转列,但是一开始就觉得不对,后来写到一半确实 ...