POJ 1465 Multiple
来源:互联网 发布:国家软件资格考试认证 编辑:程序博客网 时间:2024/06/02 22:45
Multiple
Time Limit: 1000MS Memory Limit: 32768KTotal Submissions: 6410 Accepted: 1386
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.
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:
An example of input and output:
Sample Input
223701211
Sample Output
1100
#include <iostream>#include <stdio.h>#include <string>#include <cstring>#include <queue>#include <stack>#include <cmath>#include <algorithm>#define N 5020using namespace std;int n,m;int a[N];int f;struct node{ int digit;//数值 int yu;//余数 int pre;//用于递归输入结束判断 int id;//下标}que[N];bool b[N];void output(int id)//可能会出现大数所以递归输出{ if(que[id].pre==-1) return; output(que[id].pre); printf("%d",que[id].digit);}void bfs(){ f=0; memset(b,0,n); int cnt=1,i,yu; int id; que[0].pre=-1; que[0].yu=0; que[0].id=0; queue<int> Q; Q.push(0); while(!Q.empty()) { id=Q.front(); Q.pop(); for(i=0;i<m;i++) { if(que[id].yu==0 &&a[i]==0)continue; yu=(que[id].yu*10+a[i])%n; if(!b[yu]) { if(yu==0) { output(id); printf("%d\n",a[i]); f=1; return; } b[yu]=1; que[cnt].digit=a[i]; que[cnt].pre=id; que[cnt].yu=yu; que[cnt].id=cnt; Q.push(cnt++); } } }}int main(){ while(~scanf("%d",&n)) { scanf("%d",&m); for(int i=0;i<m;i++) scanf("%d",&a[i]); sort(a,a+m); if(n==0) { printf("0\n"); continue; } bfs(); if(!f) printf("0\n"); } return 0;}
0 0
- POJ 1465 Multiple
- POJ 1465 Multiple BFS
- POJ-1465-Multiple
- poj 1465 Multiple (BFS)
- POJ 1465 Multiple
- POJ 1465 Multiple
- POJ-1465 Multiple
- poj 1465 Multiple
- POJ 1465 Multiple
- POJ 1465 Multiple
- POJ-1465 Multiple
- poj 1465 Multiple
- Chapter05-Multiple(POJ 1465)
- POJ 1465 Multiple BFS + 余数判重
- poj 1465 Multiple (bfs+取余判重)
- POJ 1465 Multiple (广搜,哈希判重)
- POJ:1465 Multiple(bfs+剪枝)
- POJ 1465 Multiple(BFS+同余剪枝)
- UVA - 514 Rails(栈)
- js 判断多组时间段是否交叉重复
- event.srcElement与event.target
- JAVA类self study note
- 【java编程思想--学习笔记(二)】访问控制-Java访问权限修饰词
- POJ 1465 Multiple
- velocity模板常用语法
- HTTP长连接与短连接
- linux2.6.xx内核代码分析
- Objective-C的内省方法
- 自定义菜单的创建及菜单事件响应
- C++ 析构函数
- [Android UI]ActionBar随ScorllView上下拖动而透明度渐变效果
- 你真的懂javascript的计时器吗?