http://acm.hdu.edu.cn/showproblem.php?pid=4474
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?
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.
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
Sample Output
Case 1: 2345 Case 2: -1
/**
hdu4474 2012年成都现场赛的一个题目
题目略坑,明明m只有0~9种可能但是m的范围居然给到了10000.。。
题意:给定n,和m个数,求n的最小倍数,使其十进制表示中不含有m中的任何一个。
解题思路:由于要求的数可能很大,因此我们不能暴力枚举。bfs,构造一个数,第一个构造成的数就是答案。 pre[t]记录到达t的前一节点u,num[t]记录形成t的最后一位数字。
**/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int a[20],pre[10010],num[10010];
int n,m;
void print(int n)
{
if(pre[n]!=-1)
print(pre[n]);
printf("%d",num[n]);
}
void bfs()
{
queue<int>q;
for(int i=1; i<10; i++)
{
if(a[i]==0)
{
int t=i%n;
if(t==0)
{
printf("%d",i);
return;
}
q.push(t);
num[t]=i;
}
}
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=0; i<10; i++)
{
if(a[i]==1)
continue;
int t=(u*10+i)%n;
if(t==0)
{
print(u);
printf("%d",i);
return;
}
if(num[t]==-1)
{
q.push(t);
pre[t]=u;
num[t]=i;
}
}
}
printf("-1");
}
int main()
{
int tt=0;
while(~scanf("%d%d",&n,&m))
{
memset(a,0,sizeof(a));
memset(num,-1,sizeof(num));
memset(pre,-1,sizeof(pre));
for(int i=0; i<m; i++)
{
int x;
scanf("%d",&x);
a[x]=1;
}
printf("Case %d: ",++tt);
bfs();
puts("");
}
return 0;
}
/**
2345 3
7 8 9
100 1
0
*/
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?
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.
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
Sample Output
Case 1: 2345 Case 2: -1