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
#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的更多相关文章

  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. Ninject.Extensions.

    最近在使用IoC进行一个较复杂的项目进行架构,在IoC的选择上让我很是纠结.首先我不喜欢大量的配置文件进行配置,那简直是噩梦,比学习一门编程语言还痛苦.我喜欢前一段时间看EF的CodeFirst的那种 ...

  2. 经常使用Javascript CDN 对照

    [前言] 请参阅某种网上文章: http://c7sky.com/javascript-libraries-cdn.html 本文加入的各个cdn对一些库的更新情况.以及响应时间. [更新] 发表文章 ...

  3. Dev环境中的集成测试用例执行时上下文环境检查(实战)

    Dev环境中的集成测试用例执行时上下文环境检查(实战) Microsoft.NET 解决方案,项目开发必知必会. 从这篇文章开始我将分享一系列我认为在实际工作中很有必要的一些.NET项目开发的核心技术 ...

  4. jQuery百叶窗图片滑块

    超酷的jQuery百叶窗图片滑块实现教程   今天我们要来分享一款基于jQuery的百叶窗焦点图插件,也可以说是图片滑块插件.这种jQuery焦点图插件的应用非常广泛,在早些年前,我们需要用flash ...

  5. EF的四种开发模式

    EF提供了四种开发模式,具体如下:(转载)Code First(New DataBase) :在代码中定义类和映射关系并通过model生成数据库,使用迁移技术更新数据库.Code First(Exis ...

  6. UrlRouting的理解

    UrlRouting的理解 文章内容 根据对Http Runtime和Http Pipeline的分析,我们知道一个ASP.NET应用程序可以有多个HttpModuel,但是只能有一个HttpHand ...

  7. ASP.NET MVC5 视图预编译

    ASP.NET MVC5 视图预编译 ASP.NET MVC5 视图预编译   关于Razor视图及引擎原理,就不多说了. 这里通俗或者不恰当地说,cshtml视图文件是先编译成dll文件,再通过视图 ...

  8. 测试驱动 ASP.NET MVC Type Aliase

    Type Aliase 去掉Scala的糖衣(4) -- Type Aliase 我的新博客地址:http://cuipengfei.me/blog/2013/12/23/desugar-scala- ...

  9. twobin博客样式

    twobin博客样式—“蓝白之风”   自暑假以来,囫囵吞枣一般蒙头栽入前端自学中,且不说是否窥探其道,却不自觉中提高了对网页版面设计的要求,乃至挑剔.一个设计清爽美观的网页能让读者心旷神怡,甚至没有 ...

  10. probuf了解

    人们一直在强调,同 XML 相比, Protobuf 的主要优点在于性能高.它以高效的二进制方式存储,比 XML 小 3 到 10 倍,快 20 到 100 倍. 对于这些 “小 3 到 10 倍”, ...