ZOJ 1136 Multiple (BFS)
Multiple
Time Limit: 10 Seconds Memory Limit: 32768 KB
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).
The input file 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.
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:
Input
22
3
7
0
1
2
1
1
Output
110
0
题意:给m个数随意拼成一个最小n的倍数。
思路:bfs 输出的时候相当于遍历路径一边,所以有pre.
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 5000 struct Node
{
int num, pre, id, yu;
};
Node node[maxn];
int vis[maxn];
int n, m;
int a[maxn];
void output(int id)
{
if(node[id].pre == -)
return;
output(node[id].pre);
printf("%d", node[id].num);
}
void bfs()
{
memset(vis, , sizeof vis);
node[].id = ;
node[].pre = -;
node[].yu = ;
int cnt = ;
queue<int> q;
q.push();
while(!q.empty())
{
int id = q.front();
q.pop();
for(int i = ; i < m; i++)
{
if(node[id].yu == && a[i] == )
continue;
int yu = (node[id].yu*+ a[i])%n;
if(!vis[yu])
{
if(yu == )
{
output(id);
printf("%d\n", a[i]);
return;
}
vis[yu] = ;
node[cnt].pre = id;
node[cnt].num = a[i];
node[cnt].yu = yu;
node[cnt].id = cnt;
q.push(cnt++);
}
}
}
printf("%d\n", );
}
int main()
{
while(~scanf("%d",&n))
{
scanf("%d", &m);
for(int i = ; i < m; i++) scanf("%d", &a[i]);
sort(a, a + m);
if(n == )
printf("%d\n", );
else
bfs();
}
return ;
}
ZOJ 1136 Multiple (BFS)的更多相关文章
- POJ.1426 Find The Multiple (BFS)
POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...
- POJ 1426 Find The Multiple --- BFS || DFS
POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- SAP BADI的“多次使用”(multiple use)
SAP中的某些BADI是不允许多用(multiple use)的,即不能同时存在多个活动的增强实施类.如下图中的这种,无论为其创建多少个实施类,都只有活动的那一个会被触发: tips : 业务加载项定 ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 【BZOJ5492】[HNOI2019]校园旅行(bfs)
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
随机推荐
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
正好以前项目中碰到这个问题,现在网上偶然又看到这个问题的博文,那就转一下吧. 原文:http://blog.lifw.org/post/46428852 感谢作者 另外补充下:在web Server容 ...
- openldap---ldapsearch使用
ldapsearch 參数表 下表描写叙述能够用于 ldapsearch 的区分大写和小写的參数. 參数 用途 -? 打印关于使用 ldapsearch 的帮助. -a deref 指定别名反向引用. ...
- [Python学习笔记][第四章Python字符串]
2016/1/28学习内容 第四章 Python字符串与正则表达式之字符串 编码规则 UTF-8 以1个字节表示英语字符(兼容ASCII),以3个字节表示中文及其他语言,UTF-8对全世界所有国家需要 ...
- EffectiveC#02--仅在对基类进行强制更新时才使用new修饰符
1.建议避免使用new修饰符来重新定义非虚函数. 非虚方法是静态绑定的,不管哪里的代码也不管在哪里引用, 它总是严格的调用类中所定义的函数.并不会在运行时在 派生类中查找不同的版本. 2.何时使用ne ...
- 模板页 相对路径 JS 加载问题
问题:我在master页面中引入了如下js文件:<script type="text/javascript" src="http://www.cnblogs.com ...
- Ubuntu自定义命令
回到主文件夹 $ cd ~ 建立.bash_aliases $ touch .bash_aliases $ vim .bash_aliases 在此文件中加入一句话: alias cdlauncher ...
- icon数目
[UIApplication sharedApplication].applicationIconBadgeNumber = currentBadgeValue.integerValue;
- poi HSSFRichTextString 对cell中的每一段文字设置字体
HSSFRichTextString ts= new HSSFRichTextString(" 经审核,我司已同意其出库申请.请你部按规定将托管银行承兑汇票办理出库." + &qu ...
- 手算KMP匹配的Next值和Nextval值
文章作者:姜南(Slyar) 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作. KMP 算法我们有写好的函数帮我们计算 Next 数组的值和 Nextval 数组 ...
- uva 230 Borrowers(摘)<vector>"结构体“ 膜拜!
I mean your borrowers of books--those mutilators of collections, spoilers of the symmetry of shelves ...