[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. 【洛谷 P2762】 太空飞行计划问题(最大权闭合图)

    题目链接 最大权闭合图模型,参考 具体做法是从源点向每个实验连一条流量为这个实验的报酬的边,从每个实验向这个实验需要的所有器材各连一条流量为\(INF\)的边,再从每个器材向汇点连一条流量为这个器材的 ...

  2. HDU 1002 A + B Problem II (大数加法)

    题目链接 Problem Description I have a very simple problem for you. Given two integers A and B, your job ...

  3. bzoj 3028 母函数

    首先我们可以求出来所有食物的母函数: 汉堡:f(x)=1/(1-x^2). 可乐:f(x)=1+x. 鸡腿:f(x)=1+x+x^2. 蜜桃多:f(x)=x/(1-x^2). 鸡块:f(x)=1/(1 ...

  4. 项目记录 -- zpool set

    zfs set <property=value> <filesystem|volume|snapshot> root@UA4300D-spa:~/hanhuakai/pro_0 ...

  5. 在AndroidStudio中导入开源库 或者jar

    方法一: 先点击Androidstudio中的Project Structure,如图 图1 到如下界面 图2 然后点击+号 图3 选择Library dependency 图4 输入你要的jar包, ...

  6. 【nginx】nginx的安装及测试

    nginx中文文档:http://www.nginx.cn/doc/index.html 1.到官网下载nginx的压缩包: https://nginx.org/   2.解压到相应的目录,比如我是e ...

  7. 【1】记一次破解wifi

    当然,使用的依旧是aircrack套件,这次依旧是跑字典,今天,捉到了另一个实验室icephone的wpa握手包,我猜测实验室的wifi一般都跟自己的名字有关,icephone刚好是8位字母,于是我就 ...

  8. 【Python项目】使用Face++的人脸识别detect API进行本地图片情绪识别并存入excel

    准备工作 首先,需要在Face++的主页注册一个账号,在控制台去获取API Key和API Secret. 然后在本地文件夹准备好要进行情绪识别的图片/相片. 代码 介绍下所使用的第三方库 ——url ...

  9. 【Android framework】am命令启动Activity流程

    源码基于Android 4.4.   am start -W -n com.dfp.test/.TEstActivity -W:等目标Activity启动后才返回 -n:用于设置Intent的Comp ...

  10. vue轮播,不是只有左右切换的,还有只切换src的

    在项目中,初次接触vue,看了轮播插件vue-swiper等,好多都是左右切换的.个人强迫症比较严重,就要单页切换样式,就手写了一个. 功能:自动轮播,上一页下一页,点击小圆点切换大图.基本轮播要求的 ...