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 ...
随机推荐
- C# while语句
一.C# while语句 while语句是用于重复执行程序代码的语句. 语法格式如下: while(boolean-expression){ embedded-statement} 当boole ...
- [转]C++ explicit的作用
explicit作用: 在C++中,explicit关键字用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显示的方式进行类型转换. explicit使用注意事项: * e ...
- 使用CSS隐藏HTML元素的四种常用方法
CSS隐藏HTML元素的四种常用方法 1.opacity:设置opacity: 0可以使一个元素变得完全透明. 设置的透明度会被子元素继承,而且无法取消. 通常可以使用opacity属性来制作元素的淡 ...
- html ajax请求 php 下拉 加载更多数据 (也可点击按钮加载更多)
<input type="hidden" class="total_num" id="total" value="{$tot ...
- C语言数组篇(二)指针数组和数组指针
数组指针 和 指针数组 这两个名词可以说是经常搞混了 数组指针--> 数组的指针 就是前面讲的 指向数组a的指针p; 指针数组--&g ...
- 将WebAPI发布到本地服务器与远程服务器
一.本地服务器 参考链接:https://blog.csdn.net/shuai_wy/article/details/69221858 1.要实现 IIS Express域名.IP的访问,关键在于需 ...
- POJ:3126-Prime Path
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- protobuf-2.5.0的下载与安装
1.下载 Hadoop使用protocol buffer进行通信,需要下载和安装protobuf-2.5.0.tar.gz.由于现在protobuf-2.5.0.tar.gz已经无法在官网https: ...
- PHP.24-TP框架商城应用实例-后台1-添加商品功能、钩子函数、在线编辑器、过滤XSS、上传图片并生成缩略图
添加商品功能 1.创建商品控制器[C] /www.test.com/shop/Admin/Controller/GoodsController.class.php <?php namespace ...
- 【转帖】置高并发jdbc连接池
简单的MySQL连接池 <Resource type="javax.sql.DataSource" name="jdbc/TestDB" factory= ...