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. hibernate的orphanRemoval

    在@OneToMany与@OneToOne中使用orphanRemoval = true时候 改动保存时候setXXX org.springframework.orm.hibernate3.Hiber ...

  2. yii性能调节

    网络应用程序的性能受很多因素的影响.数据库存取,文件系统操作,网络带宽等都是潜在的影响因素. Yii 已在各个方面减少框架带来的性能影响.但是在用户的应用中仍有很多地方可以被改善来提高性能. 1. 开 ...

  3. VC++.Net CAD编程架构

    1.每个对应的菜单项的图形抽象的, 图形抽象基类, 取决于改变来自子(如矩形.椭圆形) 2.在Doc对象管理列表管理,图形对象,当图形需要重绘或序列存储,通过遍历该列表的对象可以是 3. 每个类的职责 ...

  4. Visual Studio 单元测试之四---Generic测试

    原文:Visual Studio 单元测试之四---Generic测试 这里的Generic我觉得理解为外部测试更合适.因为在这种测试模式下Visual Studio只是启动一个外部的程序,然后通过返 ...

  5. 版本控制工具Vault v7.0更新内容曝光【慧都独家】

    SourceGear公司原定于2013年第三季度发布Vault 7.0版本.近日SourceGear就新版本的更新内容就行了发布,相信Vault 7.0版本也会于不久后面世. 那么Vault 7.0版 ...

  6. android 背景图片的设置

    在java文件中对控件设置背景图片 layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.bgimage)) 在设置中, ...

  7. 如何在局域网安装Redmine(转贴)

    如何在局域网安装Redmine(转贴) 分类: Redmine2009-06-01 10:31 1740人阅读 评论(0) 收藏 举报 phpmyadmin项目管理railssubversion数据库 ...

  8. Xamarin.Android

    Xamarin.Android之使用百度地图起始篇 一.前言 如今跨平台开发层出不穷,而对于.NET而言时下最流行的当然还是Xamarin,不仅仅能够让我们在熟悉的Vs下利用C#开发,在对原生态类库的 ...

  9. open打开窗口并且获得打开窗口的窗口对象

    //主窗体 <script language="javascript" type="text/javascript"> function opens ...

  10. CString转换为const char*

    CString str=_T("这是我的测试程序.");// 先得到要转换为字符的长度const size_t strsize=(str.GetLength()+1)*2; // ...