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. Android Google Map API使用的八个步骤

    本系列教程将分为两部分,第一部分是指导用户使用Mapview控件进行编程,其中包括了如何获得Google Map API,如何使用该API进行简单的开发,如何获得用户当前所在的位置.第二部分则包括如何 ...

  2. [LoadRunner]录制启动时报“The JVM could not be started……”错误解决方案

    在LR准备点击录制java over http协议时,程序报如下错误: 报错提示是设置的JVM值设置问题,导致不能启动. 解决方案一 点击F4快捷按钮,会弹出以下界面,在选中的位置选择对应的java路 ...

  3. MySQL查询优化方法总结

    1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉 ...

  4. JSON 序列化格式

    一.C#处理简单json数据json数据: 复制代码代码如下: {"result":"0","res_info":"ok" ...

  5. JavaSe-算数运算符

    算数运算符包括:+.-.*./.%.++.-- 代码: package com.java.chap02; public class Demo07 { public static void main(S ...

  6. mdns小结

    mdns的功能和普通DNS很类似,即提供主机名到IP地址的解析服务.   mdns一些基本特性: 1,mdns主要为小型私有网络(不存在DNS)提供名称解析. 2,mdns使用多播(Multicast ...

  7. [CV笔记]图像特征提取三大法宝:HOG特征,LBP特征,Haar特征

    (一)HOG特征 1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子.它通过计算和 ...

  8. opencv中mat的type

    type表示了矩阵中元素的类型以及矩阵的通道个数,它是一系列的预定义的常量,其命名规则为CV_(位数)+(数据类型)+(通道数),由type()返回,但是返回值是int型,不是OpenCV预定义的宏( ...

  9. 多线程编程之pthread线程深入理解

    不同的平台和操作系统上 进程和线程的实现机制不完全一致  但是一般来说线程栈都是独立的 只要得到地址就可以相互访问       Pthread是 POSIX threads 的简称,是POSIX的线程 ...

  10. LINQ中AsEnumerable与AsQueryable的区别

    AsEnumerable将一个序列向上转换为一个IEnumerable, 强制将Enumerable类下面的查询操作符绑定到后续的子查询当中:AsQueryable将一个序列向下转换为一个IQuery ...