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 ...
随机推荐
- DevExpress控件经验集合
关于GridControl的可以先看这里:http://blog.csdn.net/dong413876225/article/details/8313094 增加新行,我用了AddNewRow,但是 ...
- B3942 Censoring
爆炸入口 有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程. 这道题确乎是个很好的联系kmp ...
- C#声明方法
一.声明方法 方法是类中用于执行计算或其它行为的成员. 方法可以分为: ?静态方法:可以通过类进行访问.?实例方法:可以通过类的对象进行访问. 1. C#方法的声明 声明方法的语法格式如下: 访问修饰 ...
- 在c#中using和new这两个关键字有什么意义?
在c#中using和new这两个关键字有什么意义?答:using 引入名称空间或者使用非托管资源, new 新建实例或者隐藏基类方法
- 基于px2rpx-loader,探讨一下loader的封装思想
本文以px2rpx-loader的源码为学习对象,了解其工作机制以及loader封装的思想. 1.前言 最近在了解mpvue框架的时候,对于其能够实现一套代码兼容web和微信小程序(以下简称小程序)的 ...
- Python 创建项目、应用
1.创建项目 django-admin startproject TestPython 2.创建应用 python3 manage.py startapp books 3.目录讲解 ├── TestP ...
- tree树形
/** * tree * @param menuBeans * @param pid * @return */ public JSON makeTree(List<MenuBean& ...
- yum 仓库配置
[base]name=aliyum basebaseurl=https://mirrors.aliyun.com/centos/6/os/x86_64/ ...
- Docker迁移学习及其他
起因: 有在一台服务器A上通过docker搭建git服务,由于某些原因需要将其迁移到另一台服务器B. 过程: 最终采用方式: 首先通过docker ps(-a) 查看目标容器,然后通过commit命令 ...
- 010---Django的模型层(2)
确定模型关系: ''' Publish ---- Book 多对一:一个出版社对应多本书,在多的那张表添加关联字段 Book ---- Author 多对多:一个书对应多个作者,多个作者对应一本书 会 ...