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

  1. POJ.1426 Find The Multiple (BFS)

    POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...

  2. POJ 1426 Find The Multiple --- BFS || DFS

    POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...

  3. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  4. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  5. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  6. SAP BADI的“多次使用”(multiple use)

    SAP中的某些BADI是不允许多用(multiple use)的,即不能同时存在多个活动的增强实施类.如下图中的这种,无论为其创建多少个实施类,都只有活动的那一个会被触发: tips : 业务加载项定 ...

  7. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  8. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  9. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

随机推荐

  1. Linux中使用mysqldump对MySQL数据库进行定时备份

    #!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH expo ...

  2. 利用内存结构及多线程优化多图片下载(IOS篇)

    利用内存结构及多线程优化多图片下载(IOS篇) 前言 下载地址, 后续发布, 请继续关注本blog 在IOS中,我们常常遇到多图片下载的问题.最简单的解决方案是直接利用别人写好的框架.但是这如同练武, ...

  3. 【HDU1514】Stars(树状数组)

    绝对大坑.千万记住树状数组0好下标位置是虚拟节点.详见大白书P195.其实肉眼看也能得出,在add(有的也叫update)的点修改操作中如果传入0就会死循环.最后TLE.所以下标+1解决问题.上代码! ...

  4. C++小知识之wsprintf使用

    在C语言中格式化字符串可以使用printf,但是在WINDOWS编程设计中却行不通了,但是却有变通的方法,那就是用 wsprintf这个函数.它的格式如下: int wsprintf (    LPT ...

  5. iTunes 重新提交代码步骤

    1.选择View Details 2.右侧Links-Binary Details选项 3.Reject This Binary

  6. java框架BeanUtils及路径问题练习

    内省----->一个变态的反射    BeanUtils主要解决 的问题: 把对象的属性数据封装 到对象中.  使从文件中读取的数据往对象中赋值更加简单:   BeanUtils的好处:  1. ...

  7. 机器设备(dfs)

    机器设备 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Alpha 公司设计出一种节能的机器设备.它的内部结构是由 N 个齿轮组成.整个机器设备有 一个驱动齿轮,当 ...

  8. Go语言程序的状态监控

    Go是很实在的编程语言,从一开始就提供了很详细的运行状态信息.产品上线后的调优和排查疑难杂症都得靠这些状态信息.这边总结一些我们项目里用到的状态监控手段. pprof Go自带了一个pprof工具,这 ...

  9. thinkphp框架的路径问题 - 总结

    thinkphp框架的路径问题 - 总结 (2011-06-21 11:01:28) 转载▼ 标签: thinkphp 框架 路径 杂谈 分类: Php TP中有不少路径的便捷使用方法,比如模板中使用 ...

  10. 微信网页授权获取code链接

    本公众号授权 "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&r ...