hdu4474 Yet Another Multiple Problem
Description
In this problem, you’re asked to solve the following question: Given a positive integer n and m decimal digits, what is the minimal positive multiple of n whose decimal notation does not contain any of the given digits?
Input
For each test case, there are two lines. The first line contains two integers n and m (1 ≤ n ≤ 10
4). The second line contains m decimal digits separated by spaces.
Input is terminated by EOF.
Output
Sample Input
7 8 9
100 1
0
Sample Output
Case 2: -1
- #include <iostream>
- #include <queue>
- #include <string>
- #include <string.h>
- #include <stdio.h>
- using namespace std;
- struct node{
- int x;
- string re;
- };
- queue<node> q;
- int numcan[12],visit[10050],n;
- char str[2];
- int bfs()
- {
- node temp,ptop;
- int i,modi;
- str[1]='\0';
- memset(visit,0,sizeof(visit));
- while(!q.empty())
- {
- q.pop();
- }
- for(i=1;i<=9;i++)//第一位不为0
- {
- if(numcan[i])
- {
- modi=i%n;
- temp.x=modi;
- str[0]=i+'0';
- temp.re=str;
- visit[modi]=1;
- if(modi==0)
- {
- cout<<temp.re<<endl;
- return 1;
- }
- q.push(temp);
- }
- }
- while(!q.empty())
- {
- ptop=q.front();
- q.pop();
- for(i=0;i<=9;i++)
- {
- if(numcan[i])
- {
- modi=(ptop.x*10+i)%n;
- if(modi==0)
- {
- cout<<ptop.re<<i<<endl;
- return 1;
- }
- if(!visit[modi])
- {
- visit[modi]=1;
- str[0]=i+'0';
- temp.re=ptop.re+str;
- temp.x=modi;
- q.push(temp);
- }
- }
- }
- }
- printf("-1\n");
- return -1;
- }
- int main()
- {
- int m,tcase,i,num;
- tcase=1;
- while(scanf("%d%d",&n,&m)!=EOF)
- {
- for(i=0;i<10;i++)
- {
- numcan[i]=1;
- }
- for(i=0;i<m;i++)
- {
- scanf("%d",&num);
- numcan[num]=0;
- }
- printf("Case %d: ",tcase++);
- bfs();
- }
- return 0;
- }
hdu4474 Yet Another Multiple Problem的更多相关文章
- 2012Chhengdu K - Yet Another Multiple Problem
K - Yet Another Multiple Problem Time Limit:20000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- Yet Another Multiple Problem(bfs好题)
Yet Another Multiple Problem Time Limit : 40000/20000ms (Java/Other) Memory Limit : 65536/65536K ( ...
- HDU-4471 Yet Another Multiple Problem (BFS+路径还原)
Problem Description There are tons of problems about integer multiples. Despite the fact that the to ...
- HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】
Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- hdu 4474 Yet Another Multiple Problem
题意: 找到一个n的倍数,这个数不能含有m个后续数字中的任何一个 题解: #include<stdio.h> #include<string.h> #include<qu ...
- HDU4474_Yet Another Multiple Problem
题意很简单,要你用一些数字,组成一个数的倍数,且那个数最小. 比赛的时候没能做出来,深坑啊. 其实我只想说我以前就做过这种类型的题目了,诶. 题目的解法是数位宽搜. 首先把可用的数位提取出来,从小到大 ...
- HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )
没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib&g ...
- HDU 4474 Yet Another Multiple Problem BFS
题意:求m的倍数中不包含一些数码的最小倍数数码是多少.比如15 ,不包含0 1 3,答案是45. BFS过程:用b[]记录可用的数码.设一棵树,树根为-1.树根的孩子是所有可用的数码,孩子的孩子也是 ...
- K - Least Common Multiple
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descr ...
随机推荐
- 新手sqlserver数据库dba需要注意的小细节
前言:任何的优化和修改都是以业务情况为前提,可能有的写的有误或者不准确的地方,欢迎各位来拍砖. 1.在创建db的时候自增长建议设置成按MB(M)增长,步长根据业务量来设置,一般情况建议设置100-20 ...
- java中Integer包装类的具体解说(java二进制操作,全部进制转换)
程序猿都非常懒,你懂的! 今天为大家分享的是Integer这个包装类.在现实开发中,我们往往须要操作Integer,或者各种进制的转换等等.我今天就为大家具体解说一下Integer的使用吧.看代码: ...
- oracle 数据库技术支持生命周期表
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl4b3Jh/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
- 通过Web Api 和 Angular.js 构建单页面的web 程序
通过Web Api 和 Angular.js 构建单页面的web 程序 在传统的web 应用程序中,浏览器端通过向服务器端发送请求,然后服务器端根据这个请求发送HTML到浏览器,这个响应将会影响整个的 ...
- linux下C语言中的flock函数使用方法 .
表头文件 #include<sys/file.h> 定义函数 int flock(int fd,int operation); 函数说明 flock()会依參数operation所指 ...
- jmeter java请求
demo下载地址http://yun.baidu.com/share/link?shareid=4277735898&uk=925574576 1.引用jmeter的jar包 到jmeter的 ...
- C# 以嵌入到窗体的方式打开外部exe
using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using S ...
- [译]Java中的继承 VS 组合
(文章翻译自Inheritance vs. Composition in Java) 这篇文章阐述了Java中继承和组合的概念.它首先给出了一个继承的例子然后指出怎么通过组合来提高继承的设计.最后总结 ...
- leetcode[73] Set Matrix Zeroes 将矩阵置零
给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...
- kvm与qemu
载请注明出处: http://www.openext.org/2014/04/kvmqemu/ http://blog.csdn.net/muge0913/article/details/245577 ...