[poj1465]Multiple
Time Limit: 1000MS   Memory Limit: 32768K
Total Submissions: 7731   Accepted: 1723

Description

a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the smallest strictly positive multiple of N that has no other digits besides X1,X2..XM (if such a multiple exists).

Input

The input has several data sets separated by an empty line, each data set having the following format:

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

For each data set, the program should write to standard output on a single line the multiple, if such a multiple exists, and 0 otherwise.

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的更多相关文章

  1. poj 1465 Multiple(bfs+余数判重)

    题意:给出m个数字,要求组合成能够被n整除的最小十进制数. 分析:用到了余数判重,在这里我详细的解释了.其它就没有什么了. #include<cstdio> #include<cma ...

  2. hdu 1044(bfs+dfs+剪枝)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  3. hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)

    题目传送门 题目大意:给出n座城市,每个城市都有一个0到9的val,城市的编号是从0到n-1,从i位置出发,只能走到(i*i+1)%n这个位置,从任意起点开始,每走一步都会得到一个数字,走n-1步,会 ...

  4. HDU1664 BFS + 数论 + 剪枝

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1664 , 一道比较蛋疼的搜索题. 这道题有很多坑点,一点处理不好就要TLE. 题意很简单,就是找到一个 ...

  5. hdu 1226 超级密码(bfs+余数判重)

    题意:略过 分析:用m个数字组成一个能够被n整除的c进制数的最小值,实际上本题的关键就在于这个最小值上.  首先确定我们的思路是从小到大寻找.先查看一位数,即查看着m个数字是否能被n整除:若不能,就查 ...

  6. UVA - 11882 Biggest Number(dfs+bfs+强剪枝)

    题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...

  7. hdu1664 bfs+余数判重

    input n 不超过50个例子,n==0结束输入 Sample Input 7 15 16 101 0 output 最少个不同数字的n的倍数的x,若不同数字个数一样,输出最小的x Sample O ...

  8. soj1091 指环王 bfs+hash+剪枝

    原题链接http://acm.scu.edu.cn/soj/problem.action?id=1091 这题的主要解法就是搜索,我用的是bfs,用map将二维数组处理成字符串作为主键,到达当前状态的 ...

  9. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

随机推荐

  1. jQuery右侧悬浮楼层滚动 电梯菜单

    http://www.kaiu.net/effectCon.aspx?id=2198 <!doctype html> <html> <head> <meta ...

  2. ie8下a标签中的图片出现边框

    1.ie8下a标签中的图片出现边框 <a href="#"><img src="horse.jpg"></a> 效果如图所示 ...

  3. document.onclick在ios上不触发的解决方法与touchstart点击穿透处理

    document.onclick = function (e) { var e = e ? e : window.event; var tar = e.srcElement || e.target; ...

  4. linux编程之文件操作

    在linux下用文件描述符来表示设备文件盒普通文件,文件描述符是一个整型的数据,所有对文件的操作都是通过文件描述符来实现的. 文件描述符是文件系统中连接用户空间和内核空间的枢纽,当我们打开一个或者创建 ...

  5. monkey测试===修改adb的默认端口

    最近电脑上由于公司系统的原因,adb的端口被占用了,但是占用端口的进程是必须启动的,不能被杀死,在网上找了很多办法,大家都是说杀死占用端口的进程.这个方法并不适用我,所以在此给大家一个新的方法.新建一 ...

  6. Linux内核通知链分析【转】

    转自:http://www.cnblogs.com/jason-lu/articles/2807758.html Linux内核通知链分析 1. 引言 Linux是单内核架构(monolithic k ...

  7. 004 ConcurrentHashMap原理

    下面这部分内容转载自: http://www.haogongju.net/art/2350374 JDK5中添加了新的concurrent包,相对同步容器而言,并发容器通过一些机制改进了并发性能.因为 ...

  8. C 实现有追求的线程池 探究

    引言 线程池很普通的老话题,讨论的很多.深入的不多,也就那些基础库中才能见到这种精妙完备的技巧.而本文随大流 想深入简述一种高效控制性强的一种线程池实现. 先引入一个概念, 惊群. 简单举个例子. 春 ...

  9. 自动安装jar包到本地仓库

    参考博客:http://blog.csdn.net/m0_37797991/article/details/73394873

  10. lr_Vugen界面图