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

组队训练赛的一题,智商捉鸡。压根没头绪。看了别人的思路说是用广搜分别枚举每一位然后除以n是否模得0。

还有过程中可以对余数重复剪掉。

代码重新写了一遍。

 #include <stdio.h>
#include <queue>
#include <iostream>
using namespace std; int n,m;
int M[];
bool visited[]; struct Node{
int digit;
int pre;
int mod;
int cnt;
}nod[]; int bfs(){
queue<int> Q;
memset(visited,,sizeof(visited));
int cur=;
nod[cur].digit=;
nod[cur].pre=-;
nod[cur].mod=;
nod[cur].cnt=cur;
Q.push(cur++);
while( !Q.empty() ){
int now=Q.front();
int now_mod;
Q.pop();
for(int i=; i<m; i++){
if(nod[now].mod== && M[i]==)continue;
now_mod=(nod[now].mod*+M[i])%n;
if( !visited[now_mod] ){
visited[now_mod]=;
if(now_mod==){
int r[];
int index=;
r[index++]=M[i];
while( nod[now].pre!=- ){
r[index++]=nod[now].digit;
now=nod[now].pre;
}
for(int i=index-; i>=; i--){
printf("%d",r[i]);
}
printf("\n");
return ;
}else{
nod[cur].digit=M[i];
nod[cur].pre=nod[now].cnt;
nod[cur].mod=now_mod;
nod[cur].cnt=cur;
Q.push(cur++);
}
}
}
}
return ;
} int main(int argc, char *argv[])
{
while( scanf("%d",&n)!=EOF ){
scanf("%d",&m);
for(int i=; i<m; i++){
scanf("%d",&M[i]);
}
sort(M,M+m);
if( n== || !bfs() ){
puts("");
}
}
return ;
}

TOJ 3031 Multiple的更多相关文章

  1. TOJ3031: Multiple bfs

    3031: Multiple Time Limit(Common/Java):2000MS/6000MS     Memory Limit:65536KByte Total Submit: 60   ...

  2. 最小生成树 TOJ 4117 Happy tree friends

    链接http://acm.tju.edu.cn/toj/showp4117.html 4117.   Happy tree friends Time Limit: 1.0 Seconds   Memo ...

  3. TOJ 4119 Split Equally

    描述 Two companies cooperatively develop a project, but they don’t like working with one another. In o ...

  4. 种类并查集,TOJ(1706)

    题目链接:http://acm.tju.edu.cn/toj/showp1706.html 很类似Poj的一道帮派的问题,记得找到的可疑的关系,不要将集合刷新就可以了. 1706.   A Bug's ...

  5. Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...

    Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...

  6. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  7. [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  8. SharePoint "System.Data.SqlClient.SqlException (0x80131904): Parameter '@someColumn' was supplied multiple times.“

    最近在处理SharePoint Office365的相关开发的时候发现了这样一个奇怪的现象: 无法通过API更新Editor field,只要已更新就会throw Exception,由于是Offic ...

  9. 2012Chhengdu K - Yet Another Multiple Problem

    K - Yet Another Multiple Problem Time Limit:20000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

随机推荐

  1. 【SQL】- 基础知识梳理(二) - SQL简介

    一.引言 在梳理这些知识之前,说实话,如果有人问我SQL是什么?我可能会回答就是“INSERT,DELETE,UPDATE,SELECT”语句呗,还能是啥. 二.SQL概念 SQL是什么? SQL是S ...

  2. WinForm中自定义搜索框(水印、清空按钮、加载中图标)

    public partial class CustomSearchBar : TextBox { private readonly Label lblwaterText = new Label(); ...

  3. 【[APIO/CTSC2007]动物园】状压DP

    题目测评:https://www.luogu.org/problemnew/show/P3622 题目描述 新建的圆形动物园是亚太地区的骄傲.圆形动物园坐落于太平洋的一个小岛上,包含一大圈围栏,每个围 ...

  4. 《C#多线程编程实战》2.8 Barrier

    不得不说,C#的同步线程的机制是真的多. 各式各样.几乎各种场景下都有可以使用的同步机制. 今天说的,就是比较有意思了. 等待的机制很简单,单纯的等待. 使用的方法我就等. 等待的东西或者内容则是你自 ...

  5. NSURLSession 网络请求

    1.NSURLSession 在 iOS9.0 之后,以前使用的 NSURLConnection 过期,苹果推荐使用 NSURLSession 来替换 NSURLConnection 完成网路请求相关 ...

  6. Java 根据Date计算年龄

  7. javascript 深拷贝对象

    深拷贝polesResult,拷贝后sourceCopy成为与polesResult完全独立的对象 var sourceCopy=[].concat(JSON.parse(JSON.stringify ...

  8. 流水的算法,铁打的损失函数/MLE

    机器学习算法可以说是不少的,如果死记硬背的话,只能当时记得推导过程和步骤,过一段时间就又想不起来了,只能依稀记得一些影子.所以,应该找到算法的一些通用的方法来理解算法的思路以及推导过程. 我认为,最大 ...

  9. 跟我一起读postgresql源码(四)——Planer(查询规划模块)(上)

    时间一晃周末就过完了,时间过得太快,不由得让人倍加珍惜.时间真是不够用哈~ 好的不废话,这次我们开始看查询规划模块的源码吧. 查询规划部分的在整个查询处理模块应该是在一个非常重要的地位上,这一步直接决 ...

  10. angluarJs与后台交互get

    <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...