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. 首次将项目从svn下载到eclipse

    1.点击 File --> Import,进入导入项目窗口 2.选择从SVN检出项目,点击Next 3.选择创建新的资源库位置,点击Next 4.在URL处输入SVN项目远程地址,点击Next ...

  2. HDU 1850 Being a Good Boy in Spring Festival 在春节做乖孩子(Nim博弈,微变形)

    题意: 思路: 如果全部扑克牌数目异或的结果ans为0,则必输,输出0.否则,必须要给对方一个P状态,可以对所有扑克堆进行逐个排查,将ans^a[i]就可以得到除了a[i]之外其他扑克数的异或结果tm ...

  3. python爬虫之路——初识数据库存储

    非关系型数据库:MongoDB.关系型数据库:MySQL 关系型和非关系型的区别: 安装: 使用: 应用场景: mongoDB是一种非关系型数据库,分为四大类:键值存储数据库,列存储数据库,文档型数据 ...

  4. Ajax经典的面试题

    1.什么是AJAX,为什么要使用Ajax(请谈一下你对Ajax的认识)什么是ajax:AJAX是“Asynchronous JavaScript and XML”的缩写.他是指一种创建交互式网页应用的 ...

  5. 使用JS的画布制作一个瞄准镜

    <canvas width="600" height="500" id="myCanvas"></canvas> & ...

  6. 广播监听USB插入与拔出

    package com.joy.usbbroadcastreceiver; import android.content.BroadcastReceiver; import android.conte ...

  7. MySQL内置函数:IP地址点分式与数字转换函数(INET_ATON/INET_NTOA)

    前后转换,相比代码内部在进行移位简单太多了 SELECT INET_ATON('209.207.224.40'); SELECT INET_NTOA('578950');

  8. webgis技术在智慧城市综合治理(9+X)网格化社会管理平台(综治平台)的应用研究

    综治中心9+X网格化社会管理平台 为落实中央关于加强创新社会治理的要求,适应国家治理体系和治理能力现代化要求,以基层党组织为核心,以整合资源.理顺关系.健全机制.发挥作用为目标,规范街道.社区综治中心 ...

  9. java String中的replace(oldChar,newChar) replace(CharSequence target,CharSequence replacement) replaceAll replaceFirst 面试题:输入英文语句,单词首字符大写后输出 char String int 相互转换

    package com.swift; import java.util.Scanner; public class FirstChat_ToCaps_Test { public static void ...

  10. django logging日志优先级

    原创博文 转载请注明出处! 参考官方文档:https://docs.djangoproject.com/en/2.1/topics/logging/ Loggers¶ A logger is the ...