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 ...
随机推荐
- [转]xe6 android 使用距离传感器(Proximiry)
The first step it's a run sample from RAD Studio that named SensorInfo on your device. On the tab Bi ...
- 【转】快速开发移动医疗App!开源框架mHealthDroid
原文地址:http://www.csdn.net/article/2014-12-12/2823096-mHealhDroid mHealthDroid是一款开源的移动框架,主要用于帮助开发者快速而又 ...
- 【转】android 手势识别和VelocityTracker
参考地址: http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1020/448.html http://www.jcodecraeer.co ...
- WinForm中TabControl的使用
TabControl和TabPage之间有一个默认颜色的边框,很难去除,需要重写TabControl控件重绘区域 public class FullTabControl : TabControl { ...
- vue生态圈
本文来自网易云社区 作者:刘凌阳 前言 公司社区上关于Vue的文章挺少的(少的可怜),不禁为Vue愤愤不平,此文应运而生. 但笔者水平有限,也写不了什么特别高深的东西,只能简单介绍下Vue生态圈,如有 ...
- 三、Node.js-HelloWorld案例
之前我们编写的JavaScript代码都是在浏览器中运行的,因此,我们可以直接在浏览器中敲代码,然后直接运行. 在Node,我们编写的JavaScript代码将不能在浏览器环境中执行了,而是在Node ...
- [Algorithm]排序
一.排序算法 1.插入排序 1) 直接插入排序:(插入类) 1 void InsertSort( ElemType R[], int n ) 2 { 3 for ( int i = 2; i < ...
- 移动端复制粘贴clipboardjs
官方网址: https://clipboardjs.com/ 不能一打开就直接触发复制粘贴: 参考博客: http://blog.csdn.net/twoByte/article/details/52 ...
- php代码审计4审计代码执行漏洞
代码执行漏洞代码执行漏洞是指应用程序本身过滤不严,用户可以通过请求将代码注入到应用中执行,当应用在调用一些能将字符串转化成代码的函数(如php中的eval)时,没有考虑到用户是否能控制这个字符串,造成 ...
- Nginx 进行性能配置
总所周知,网络上我们购买的服务器的性能各不相同,如果采用 Nginx 的默认配置的话,无法将服务器的全部性能优势发挥出来,我们应该选择适合自己需求的配置. 当我们默认安装后 Nginx 后,我们便得到 ...