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相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
随机推荐
- Angular学习笔记(2)——TODO小应用
Angular学习笔记(2)--TODO小应用 1. 写在前面 之前我们跑了Angular的Hello World,你是不是对它有点感觉了呢?这一篇将结合一个TODO程序来继续学习Angular的用法 ...
- python json基础学习01
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import json #全称(javascript object ...
- array模块
array模块定义了一种序列数据结构,看起来和list很相似,但是所有成员必须是相同基本类型. 2.1 array-固定类型数据序列 array作用是高效管理固定类型数值数据的序列. 2.2.1 初始 ...
- 2014 BDTC 參会有感
中国大数据技术大会(Big Data Technology Conference,BDTC)是眼下国内最具影响.规模最大的大数据领域的技术盛会. 大会的前身是Hadoop中国云计算大会(Hadoop ...
- css学习之color: window和color: currentColor
一.易被忽略的属性 color: currentColor color: window 看完之后感觉眼前一亮,有的我之前根本没有用过,甚至都不知道有color: currentColor这么个东西 ...
- Jquery on() 动态绑定事件
之前就一直受这个问题的困扰,在jQuery1.7版本之后添加了on方法,之前就了解过,其优越性高于live(),bind(),delegate()等方法,在此之前项目中想用这个来测试结果发现,居然动态 ...
- (转)ASP.Net 学习路线
入门篇 1.学习面向对象(OOP)的编程思想 许多高级语言都是面向对象的编程,.NET也不例外.如果您第一次接触面向对象的编程,就必须理解类.对象.字段.属性.方法和事件.封装.继承和多态性.重载.重 ...
- django防止表单数据重复提交
思路: 在Asp.net中存在Page.IsPostback的方法,所以对django中表单提交数据的重复提交的数据采用相似方法实现,即在页面第一次访问时,即访问方法为GET方法在view中 ...
- Xcode6和Xcode5获取app名字
1.在Xcode5下,获取程序名字(app name)的方法为: NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionar ...
- “有箭头的视图”,即程序的Storyboard Entry Point。
设置方法很简单:打开StoryBoard文件,选中要设置为第一视图的ViewController,在右边工具栏勾选Is Initial View Controller就好了,此时你会看到ViewCon ...