【BFS】【余数剪枝】Multiple
| Time Limit: 1000MS | Memory Limit: 32768K | |
| Total Submissions: 7731 | Accepted: 1723 |
Description
Input
On the first line - the number N
On the second line - the number M
On the following M lines - the digits X1,X2..XM.
Output
An example of input and output:
Sample Input
22
3
7
0
1 2
1
1
Sample Output
110
0
Source
题目大意:给你一个在0至4999之间的数N,在给你M个数,输出这些数能组成的最小的N的倍数,没有输出0
试题分析:本体盲目搜是不可取的,因为我们根本就不知道要搜到哪里(对于DFS),每个数的数量可以取无限多个……
那么对于BFS来说数量还是比较多,但BFS擅长解决一些没有边界的问题嘛,所以用BFS解决此题
那么如何剪枝呢?
对于一个数(AX+Y)%X与(BX+Y)%X的余数是一样的,至于取谁,小的那个(也就是先搜到的那个)是我们要的,大的可以直接剪掉,所以只需要开一个数组Hash一下就好了……
注意特判N=0的情况!!!
代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
//#include<cmath> using namespace std;
const int INF = 9999999;
#define LL long long inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
int N,M;
int a[5101];
int l=1,r=1;
bool flag[5101];
bool fla=false;
struct data{
int ga,last,ans;
}Que[5101];
void print(data k){
if(k.ga!=-1){
print(Que[k.ga]);
printf("%d",k.ans);
}
}
void BFS(){//a当前数字
Que[1].last=0;
Que[1].ans=0;
Que[1].ga=-1;
int l1,p;
while(l<=r){
l1=Que[l].last;
for(int i=1;i<=M;i++){
p=(l1*10+a[i])%N;
if(!flag[p]&&(Que[l].ga!=-1||a[i]>0)){
flag[p]=true;
Que[++r].ans=a[i];
Que[r].ga=l;
Que[r].last=p;
if(p==0){
data s=Que[r];
print(s);
printf("\n");
fla=true;
return ;
}
}
}
l++;
}
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
while(scanf("%d",&N)!=EOF){
M=read();
for(int i=1;i<=M;i++) a[i]=read();
sort(a+1,a+M+1);
if(N==0){
puts("0");
continue;
}
memset(flag,false,sizeof(flag));
l=r=1;
fla=false;
BFS();
if(!fla){
puts("0");
}
}
return 0;
}
【BFS】【余数剪枝】Multiple的更多相关文章
- poj 1465 Multiple(bfs+余数判重)
题意:给出m个数字,要求组合成能够被n整除的最小十进制数. 分析:用到了余数判重,在这里我详细的解释了.其它就没有什么了. #include<cstdio> #include<cma ...
- hdu 1044(bfs+dfs+剪枝)
Collect More Jewels Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)
题目传送门 题目大意:给出n座城市,每个城市都有一个0到9的val,城市的编号是从0到n-1,从i位置出发,只能走到(i*i+1)%n这个位置,从任意起点开始,每走一步都会得到一个数字,走n-1步,会 ...
- HDU1664 BFS + 数论 + 剪枝
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1664 , 一道比较蛋疼的搜索题. 这道题有很多坑点,一点处理不好就要TLE. 题意很简单,就是找到一个 ...
- hdu 1226 超级密码(bfs+余数判重)
题意:略过 分析:用m个数字组成一个能够被n整除的c进制数的最小值,实际上本题的关键就在于这个最小值上. 首先确定我们的思路是从小到大寻找.先查看一位数,即查看着m个数字是否能被n整除:若不能,就查 ...
- UVA - 11882 Biggest Number(dfs+bfs+强剪枝)
题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...
- hdu1664 bfs+余数判重
input n 不超过50个例子,n==0结束输入 Sample Input 7 15 16 101 0 output 最少个不同数字的n的倍数的x,若不同数字个数一样,输出最小的x Sample O ...
- soj1091 指环王 bfs+hash+剪枝
原题链接http://acm.scu.edu.cn/soj/problem.action?id=1091 这题的主要解法就是搜索,我用的是bfs,用map将二维数组处理成字符串作为主键,到达当前状态的 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
随机推荐
- python学习笔记(一)之为什么学习python
python的特点: 跨平台 实现同一个功能是Java代码的1/5 python应用范围: 操作系统 web 3D动画 企业应用 云计算 如何学习python? 学习语法 验证例子 学会总结 课外实践
- 剖析 golang 的25个关键字
剖析 golang 的25个关键字 基本在所有语言当中,关键字都是不允许用于自定义的,在Golang中有25个关键字,图示如下: 下面我们逐个解析这25个关键字. var && con ...
- WordPress在nginx服务器伪静态
server { listen 80; root /var/www/xxx; server_name www.xxx.com; access_log /var/log/www/xxx.log main ...
- OOM有哪些情况,SOF有哪些情况
OOM 1.全称为OutOfMemoryError异常,如果虚拟机在扩展栈时无法申请足够的内存空间,抛出它: 2.Java heap异常:java.lang.OutOfMemoryError:Java ...
- CentOS7手动编译安装内核4.11.7
1. 进入/usr/src/目录 cd /usr/src 2. 下载内核源码,网址:https://www.kernel.org wget https://cdn.kernel.org/pub/lin ...
- nvidia tk1使用记录--基本环境搭建
前言 项目最开始是在X86+Nvidia(ubuntu+opencv+cuda)平台上实现,达到了期望性能,最近考虑将其移植到嵌入式平台,特别是最近nvidia出了tegra X1,基于和我们使用的g ...
- sicily 数据结构 1014. Translation
Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...
- BZOJ 4516: [Sdoi2016]生成魔咒——后缀数组、并查集
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4516 题意 一开始串为空,每次往串后面加一个字符,求本质不同的子串的个数,可以离线.即长度为 ...
- 2015多校第9场 HDU 5405 Sometimes Naive 树链剖分
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5405 题意: 给你一棵n个节点的树,有点权. 要求支持两种操作: 操作1:更改某个节点的 ...
- caffe solver.prototxt 生成
from caffe.proto import caffe_pb2 s = caffe_pb2.SolverParameter() path='/home/xxx/data/' solver_file ...