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 ...
随机推荐
- 关于对SwfUpload的改造
Swfupload 在普通上传下,对于IE chrome firefox等有很好的兼容性. 但一旦与其他控件组合,很容易出现无法上传,帮顶事件丢失的情况. 例如Layer与Swfupload,上传一个 ...
- WPF中在摄像头视频上叠加控件的解决方案
一.视频呈现 前段时间,在一个wpf的项目中需要实时显示ip摄像头,对此的解决方案想必大家都应该知道很多.在winform中,我们可以将一个控件(一般用panel或者pictruebox)的句柄丢给摄 ...
- HtmlAgilityPack 使用
或.无属性.属性个数.属性值: var preceding_siblings = node.SelectNodes("preceding-sibling::input| preceding- ...
- C# WinForm Webbrowser 强制所有网页链接在同一页面打开或者在TabControl中弹出新窗口(续)
上面那个文写的如同粑粑一样 效果图 Winfrom 中添加这个类就好了 using System; using System.Collections.Generic; using System.Com ...
- 钩子(hook)编程
一.钩子介绍 1.1钩子的实现机制 钩子英文名叫Hook,是一种截获windows系统中某应用程序或者所有进程的消息的一种技术.下图是windows应用程序传递消息的过程: 如在键盘中按下一键,操作系 ...
- Balance(Stack)
栈的运用 mooc视频连接 #include <iostream> using namespace std; ]; ; void Push(char c) { ) { Top = ; S[ ...
- Linux解压工具
指令 yum install lrzsz
- scrapy 调试功能
在使用 scrapy 来爬取网页的时候,我们难免会使用到调试功能,下面介绍两种调试方法: 1.终端使用 scrapy shell exampleurl exampleurl 为你要爬取网站的 url ...
- vue可视化图表 基于Echarts封装好的v-charts简介
**vue可视化图表 基于Echarts封装好的v-charts** 近期公司又一个新的需求,要做一个订单和销售额统计的项目,需要用到可视化图表来更直观的展示数据.首先我想到的是Echarts,众所周 ...
- RPC 定义 和 原理
一.RPC 1. RPC是什么 RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议. ...