TOJ 3031 Multiple
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
组队训练赛的一题,智商捉鸡。压根没头绪。看了别人的思路说是用广搜分别枚举每一位然后除以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的更多相关文章
- TOJ3031: Multiple bfs
3031: Multiple Time Limit(Common/Java):2000MS/6000MS Memory Limit:65536KByte Total Submit: 60 ...
- 最小生成树 TOJ 4117 Happy tree friends
链接http://acm.tju.edu.cn/toj/showp4117.html 4117. Happy tree friends Time Limit: 1.0 Seconds Memo ...
- TOJ 4119 Split Equally
描述 Two companies cooperatively develop a project, but they don’t like working with one another. In o ...
- 种类并查集,TOJ(1706)
题目链接:http://acm.tju.edu.cn/toj/showp1706.html 很类似Poj的一道帮派的问题,记得找到的可疑的关系,不要将集合刷新就可以了. 1706. A Bug's ...
- 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 ... 这个错误是因为有两个相 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- [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 ...
- SharePoint "System.Data.SqlClient.SqlException (0x80131904): Parameter '@someColumn' was supplied multiple times.“
最近在处理SharePoint Office365的相关开发的时候发现了这样一个奇怪的现象: 无法通过API更新Editor field,只要已更新就会throw Exception,由于是Offic ...
- 2012Chhengdu K - Yet Another Multiple Problem
K - Yet Another Multiple Problem Time Limit:20000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
随机推荐
- datatables表格行内编辑的实现
Datatables是一款jquery表格插件,它是一个高度灵活的工具,灵活就意味着很多功能需要自己去实现,比如说行内编辑功能. Datatables自己是没有行内编辑功能的,最简单的是通过modal ...
- C# socket请求的名称有效 但是找不到请求的类型的数据
程序以前在xp下运行一直良好,但将安装在win7下面却出现"请求的名称有效 但是找不到请求的类型的数据"错误,程序底层通信是基于socket,时间久了就会出现系统黑屏死机,但并不知 ...
- DOM--sql server
public List<LianHeData> select(int ID) { List<LianHeData> list = new List<LianHeData& ...
- .Net Mvc 四种过滤器
一.授权过滤器:AuthorizationFilters 二.动作过滤:ActionFilters 三.响应过滤:ResultFilters 四.异常过滤:ExceptionFilters ===== ...
- WinForm中使用自定义Tooltip控件
private ToolTip tooltipCtr; 构造函数中: 隐藏默认的Tooltip:this.ShowCellToolTips = false; this.tooltipCtr = new ...
- Kotlin if else判断
Kotlin的if相对与java,有着较为灵活的用法. if是用来判断. if在Kotlin里面可以作为表达式来使用. 如果熟悉C java C#等 A>B:A?B这个判断应该是很熟悉,而Kot ...
- 玩转php缓存memcache
记录 一.本地安装 链接:https://www.cnblogs.com/jkko123/p/6294669.html 二.linux安装
- 一个div添加多个背景图片
效果图如下:
- PHP获取对象的纯数字属性
php的对象属性 我们知道获取php的对象属性用箭头: echo $obj->name; 如果属性名是一个变量,那么可以用: $var = 'name'; echo $obj->$var; ...
- 5.EM