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 ...
随机推荐
- Unable to launch the Java Virtual Machine
看看国内的回答,http://zhidao.baidu.com/question/119993351.html 再看看国外的,http://www.mkyong.com/oracle/oracle-s ...
- 使用筛法在 O(logN) 的时间内查询多组数的素数因子
Prime Factorization using Sieve O(log n) for multiple queries 使用筛法在 O(logN) 的时间内查询多组数的素数因子 前言 通常, 我们 ...
- JQuery 解决按钮上的倒计时问题
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- LeetCode426.Convert Binary Search Tree to Sorted Doubly Linked List
题目 Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right point ...
- zabbix proxy安装配置
1.下载软件zabbix-2.2.1.tar.gz 1.1解压 tar xvf zabbix-2.2.1.tar.gz 1.2编译安装 cd zabbix-2.2.1./configure --pre ...
- ethereum(以太坊)(十一)--字节数组(二)
pragma solidity ^0.4.0; contract test { uint [5] T =[1,2,3,4,5] ;//固定长度的数组:可修改数组内值大小,不支持push,不可更改长度 ...
- 以CentOS为操作系统的vps或服务器安装lnmp运行环境的方法
安装步骤: 1.使用putty或类似的SSH工具登陆VPS或服务器: 登陆后运行:screen -S lnmp 如果提示screen: command not found 命令不存在可以执行:yum ...
- python——集合
在python中,字典的亲戚就是集合,集合就是无映射关系的字典,花括号并不是字典的特权.如下面程序所示: >>> num = {} >>> type(num) &l ...
- Java语言基础---变量与数据类型
变量的作用域 java用一对大括号“{}”作为语句块的范围,称为作用域.作用域中的变量不能重复定义:离开作用域,变量所分配的内存空间将被JVM所收回. 基本数据类型的包装类 java为基础数据类型提供 ...
- Eclipse Java 构建路径 ---Eclipse教程第13课
Eclipse Java 构建路径 设置 Java 构建路径 Java构建路径用于在编译Java项目时找到依赖的类,包括以下几项: 源码包 项目相关的 jar 包及类文件 项目引用的的类库 我们可以通 ...