【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 ...
随机推荐
- MySQL 基于 GTID 主从架构添加新 Slave 的过程
内容全部来自: How to create/restore a slave using GTID replication in MySQL 5.6 需求说明 需求: 对于已经存在的 MySQL 主从架 ...
- 取石子游戏 HDU2516(斐波那契博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2516 题目: Problem Description 1堆石子有n个,两人轮流取.先取者第1次可以取任 ...
- 分类算法:决策树(C4.5)(转)
C4.5是机器学习算法中的另一个分类决策树算法,它是基于ID3算法进行改进后的一种重要算法,相比于ID3算法,改进有如下几个要点: 1)用信息增益率来选择属性.ID3选择属性用的是子树的信息增益,这里 ...
- js原生读取json
function showJson(){ var test; if(window.XMLHttpRequest){ test = new XMLHttpRequest(); }else if(wind ...
- UNIX v6
UNIX v6 http://download.csdn.net/download/u013896535/9106775 https://github.com/chromium/mini_chromi ...
- Cent os FTP配置
原文:http://www.aicoffees.com/itshare/412261137.html
- 使用 Visual Studio 部署 .NET Core 应用 ——.Net Core 部署到Ubuntu 16.04
.Net Core 部署到Ubuntu 16.04 中的步骤 1.安装工具 1.apache 2..Net Core(dotnet-sdk-2.0) 3.Supervisor(进程管理工具,目的是服务 ...
- javascript 实现图片拖动
javascript实现图片拖动效果并不难,主要的思路如下 1:给图片绑定监听鼠标按下对象,设置拖动属性为true 2:鼠标抬起:拖动属性为false 鼠标移动:改变坐标即可,新坐标=图片原始坐标+鼠 ...
- Math.random易于记忆理解
产生随机数 Math.random*(Max-Min)+Min
- LeetCode解题报告——Convert Sorted List to Binary Search Tree & Populating Next Right Pointers in Each Node & Word Ladder
1. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ...