Yet Another Multiple Problem

Description

There are tons of problems about integer multiples. Despite the fact that the topic is not original, the content is highly challenging. That’s why we call it “Yet Another Multiple Problem”.
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

There are several test cases.

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

For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) while Y is the minimal multiple satisfying the above-mentioned conditions or “-1” (without quotation marks) in case there does not exist such a multiple.
 

Sample Input

2345 3
7 8 9
100 1
0
题意,就是给一个数,求出这个数的最小倍数,且不包含,要排除的数字!其实,这题看起来挺简单的,我也刚开始,
用高精度,一个一个乘,但发现行不通,因为,我们不知道,在什么时候,才能停下来,我们再仔细想想,n是很小的数的,我们可不可以把所有的模n的值都取到,这样的话,我们就可以,用一个bfs,枚举出所有的情况,这样,我们就可以从小到大的搜出来,因为我们一直都是从小取到大,这样的话,我们最先搜到的一定是最小值!我们就用取的模判重,如果在以来已经求到了这个模,那么我们也没必要再加长了啊,因为,在以前,都可以取到了,那么,为什么还要加长呢?而且这两个值是等价的!这样,我们就可以很快的搜到要求的值!
 

Sample Output

Case 1: 2345
Case 2: -1
  1. #include <iostream>
  2. #include <queue>
  3. #include <string>
  4. #include <string.h>
  5. #include <stdio.h>
  6. using namespace std;
  7. struct node{
  8. int x;
  9. string re;
  10. };
  11. queue<node> q;
  12. int numcan[12],visit[10050],n;
  13. char str[2];
  14. int bfs()
  15. {
  16. node temp,ptop;
  17. int i,modi;
  18. str[1]='\0';
  19. memset(visit,0,sizeof(visit));
  20. while(!q.empty())
  21. {
  22. q.pop();
  23. }
  24.  
  25. for(i=1;i<=9;i++)//第一位不为0
  26. {
  27. if(numcan[i])
  28. {
  29. modi=i%n;
  30.  
  31. temp.x=modi;
  32. str[0]=i+'0';
  33.  
  34. temp.re=str;
  35.  
  36. visit[modi]=1;
  37. if(modi==0)
  38. {
  39. cout<<temp.re<<endl;
  40. return 1;
  41. }
  42. q.push(temp);
  43. }
  44. }
  45. while(!q.empty())
  46. {
  47. ptop=q.front();
  48. q.pop();
  49. for(i=0;i<=9;i++)
  50. {
  51.  
  52. if(numcan[i])
  53. {
  54. modi=(ptop.x*10+i)%n;
  55. if(modi==0)
  56. {
  57. cout<<ptop.re<<i<<endl;
  58. return 1;
  59. }
  60. if(!visit[modi])
  61. {
  62. visit[modi]=1;
  63. str[0]=i+'0';
  64. temp.re=ptop.re+str;
  65. temp.x=modi;
  66. q.push(temp);
  67.  
  68. }
  69. }
  70. }
  71.  
  72. }
  73. printf("-1\n");
  74. return -1;
  75. }
  76. int main()
  77. {
  78. int m,tcase,i,num;
  79. tcase=1;
  80. while(scanf("%d%d",&n,&m)!=EOF)
  81. {
  82. for(i=0;i<10;i++)
  83. {
  84. numcan[i]=1;
  85. }
  86. for(i=0;i<m;i++)
  87. {
  88. scanf("%d",&num);
  89. numcan[num]=0;
  90. }
  91. printf("Case %d: ",tcase++);
  92. bfs();
  93. }
  94. return 0;
  95. }

hdu4474 Yet Another Multiple Problem的更多相关文章

  1. 2012Chhengdu K - Yet Another Multiple Problem

    K - Yet Another Multiple Problem Time Limit:20000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  2. Yet Another Multiple Problem(bfs好题)

    Yet Another Multiple Problem Time Limit : 40000/20000ms (Java/Other)   Memory Limit : 65536/65536K ( ...

  3. HDU-4471 Yet Another Multiple Problem (BFS+路径还原)

    Problem Description There are tons of problems about integer multiples. Despite the fact that the to ...

  4. 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 ...

  5. hdu 4474 Yet Another Multiple Problem

    题意: 找到一个n的倍数,这个数不能含有m个后续数字中的任何一个 题解: #include<stdio.h> #include<string.h> #include<qu ...

  6. HDU4474_Yet Another Multiple Problem

    题意很简单,要你用一些数字,组成一个数的倍数,且那个数最小. 比赛的时候没能做出来,深坑啊. 其实我只想说我以前就做过这种类型的题目了,诶. 题目的解法是数位宽搜. 首先把可用的数位提取出来,从小到大 ...

  7. HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )

    没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib&g ...

  8. HDU 4474 Yet Another Multiple Problem BFS

    题意:求m的倍数中不包含一些数码的最小倍数数码是多少.比如15 ,不包含0  1 3,答案是45. BFS过程:用b[]记录可用的数码.设一棵树,树根为-1.树根的孩子是所有可用的数码,孩子的孩子也是 ...

  9. K - Least Common Multiple

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Descr ...

随机推荐

  1. 新手sqlserver数据库dba需要注意的小细节

    前言:任何的优化和修改都是以业务情况为前提,可能有的写的有误或者不准确的地方,欢迎各位来拍砖. 1.在创建db的时候自增长建议设置成按MB(M)增长,步长根据业务量来设置,一般情况建议设置100-20 ...

  2. java中Integer包装类的具体解说(java二进制操作,全部进制转换)

    程序猿都非常懒,你懂的! 今天为大家分享的是Integer这个包装类.在现实开发中,我们往往须要操作Integer,或者各种进制的转换等等.我今天就为大家具体解说一下Integer的使用吧.看代码: ...

  3. oracle 数据库技术支持生命周期表

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl4b3Jh/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  4. 通过Web Api 和 Angular.js 构建单页面的web 程序

    通过Web Api 和 Angular.js 构建单页面的web 程序 在传统的web 应用程序中,浏览器端通过向服务器端发送请求,然后服务器端根据这个请求发送HTML到浏览器,这个响应将会影响整个的 ...

  5. linux下C语言中的flock函数使用方法 .

    表头文件  #include<sys/file.h> 定义函数  int flock(int fd,int operation); 函数说明  flock()会依參数operation所指 ...

  6. jmeter java请求

    demo下载地址http://yun.baidu.com/share/link?shareid=4277735898&uk=925574576 1.引用jmeter的jar包 到jmeter的 ...

  7. C# 以嵌入到窗体的方式打开外部exe

    using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using S ...

  8. [译]Java中的继承 VS 组合

    (文章翻译自Inheritance vs. Composition in Java) 这篇文章阐述了Java中继承和组合的概念.它首先给出了一个继承的例子然后指出怎么通过组合来提高继承的设计.最后总结 ...

  9. leetcode[73] Set Matrix Zeroes 将矩阵置零

    给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...

  10. kvm与qemu

    载请注明出处: http://www.openext.org/2014/04/kvmqemu/ http://blog.csdn.net/muge0913/article/details/245577 ...