3031: Multiple

Time Limit(Common/Java):2000MS/6000MS     Memory Limit:65536KByte
Total Submit:
60
          
Accepted:10

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

Southeastern Europe 2000

这个题目可能没那么爽啊,就是给你m个数字,然后用这些数字就构造n的倍数

首先肯定贪心取啊,然后就是大数取余,所以这个时候同余的,然后bfs就完了

#include<stdio.h>
#include<string.h>
#include<algorithm>
const int N=;
struct T
{
int res,pre,digit;
} q[N],t;
int n,m,a[];
bool vis[N];
void print (int x)
{
if(q[x].pre==-)return;
print(q[x].pre),printf("%d",q[x].digit);
}
int bfs()
{
q[]= {,-,},memset(vis,,sizeof vis);
int tot=,lst=;
while(tot<lst)
{
t=q[tot];
for(int i=,t1; i<m; i++)
{
t1=(t.res*+a[i])%n;
if(!a[i]&&t.pre==-)continue;
if(!vis[t1])vis[t1]=,q[lst++]= {t1,tot,a[i]};
if(!t1)
{
print(lst-),printf("\n");
return ;
}
}
tot++;
}
return ;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=; i<m; i++)scanf("%d",&a[i]);
if(!n)printf("0\n");
else
{
std::sort(a,a+m);
if(!bfs())printf("0\n");
}
}
}

TOJ3031: Multiple bfs的更多相关文章

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

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

  2. POJ1426Find The Multiple[BFS]

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27433   Accepted: 114 ...

  3. ZOJ 1136 Multiple (BFS)

    Multiple Time Limit: 10 Seconds      Memory Limit: 32768 KB a program that, given a natural number N ...

  4. POJ 1465 Multiple (BFS,同余定理)

    id=1465">http://poj.org/problem?id=1465 Multiple Time Limit: 1000MS   Memory Limit: 32768K T ...

  5. poj 1426 Find The Multiple (bfs 搜索)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18012   Accepted: 729 ...

  6. POJ1426 Find The Multiple —— BFS

    题目链接:http://poj.org/problem?id=1426 Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Tota ...

  7. POJ 1426 Find The Multiple BFS

    没什么好说的 从1开始进行广搜,因为只能包涵0和1,所以下一次需要搜索的值为next=now*10 和 next=now*10+1,每次判断一下就可以了,但是我一直不太明白我的代码为什么C++提交会错 ...

  8. poj1426 - Find The Multiple [bfs 记录路径]

    传送门 转:http://blog.csdn.net/wangjian8006/article/details/7460523 (比较好的记录路径方案) #include<iostream> ...

  9. poj 1426 Find The Multiple ( BFS+同余模定理)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18390   Accepted: 744 ...

随机推荐

  1. Viewcontroller基类

    #import <UIKit/UIKit.h> #import "YQZMutableArray.h" @interface YQZViewController : U ...

  2. xcode在代码中查找中文

    总是忘记xcode中查找中文,这次记下来,以后就不会忘记了,哈哈 请看下图: 切换到查找,点击find后面的text,选择Regular Expression,然后输入 1. 查找非ascii的字符 ...

  3. jenkins只能同时构建2个Job怎么办?

    在jenkins 构建任务时,同时只能构建2个,如果两个没有job没有结束,构建第3个就会不执行: 提示: pending—Waiting for next available executor on ...

  4. Python 之excle的读写

    一.读取Excel 注:要先安装xlrd 代码如下: #-*- coding: utf8 -*-import xlrd   #引入读excle的类#fname = "reflect.xls& ...

  5. (四)SpringMVC之使用cookie实现记住密码的功能

    注意:因为实现记住密码的功能需要用到json,所以需要加上这条语句: <script type="text/javascript" src="scripts/jqu ...

  6. SAP Cloud for Customer客户主数据的地图集成

    点击这个按钮可以通过地图的方式查看C4C客户在地图上的地理位置: 只需要在这个客户的地址栏里维护上天府软件园的经度和维度: 就能够在C4C的客户列表页面里显示该客户在地图上的位置: 要获取更多Jerr ...

  7. 2017年团体程序设计天梯赛 - 大区赛 L3-3

    题意:有向图找哈密顿回路 比赛的时候剪枝只剪了vis 状压没剪对 反而只拿17分... 比赛结束后还去看了一发这个NP问题的QB(快速回溯法...但是对于本题好像大材小用...) 上网看了一个神犇的写 ...

  8. Classes and metaclasses

    http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html Obje ...

  9. eclipse报错GC overhead limit exceed,卡顿

    在使用Eclipse的Build Project功能时,提示以下错误: An internal error occurred during: “Build Project”. GC overhead ...

  10. springboot超详细笔记

    一.Spring Boot 入门 1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,m ...