2015
07-16

# Yet Another Multiple Problem

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?

There are several test cases.
For each test case, there are two lines. The first line contains two integers n and m (1 ≤ n ≤ 104). The second line contains m decimal digits separated by spaces.
Input is terminated by EOF.

There are several test cases.
For each test case, there are two lines. The first line contains two integers n and m (1 ≤ n ≤ 104). The second line contains m decimal digits separated by spaces.
Input is terminated by EOF.

2345 3
7 8 9
100 1
0

Case 1: 2345
Case 2: -1

#include <iostream>
#include <cstdio>
#include <queue>
#include <string>

#define N 10005
#define ll __int64
#define ss(a) scanf("%d",&a)
#define cl(a) memset(a,0,sizeof(a))

using namespace std;

int a[12],f[N];
queue<pair<string,int> > rec;

string find_ans(int n,int m)
{
while (!rec.empty()) rec.pop();
pair<string,int>init;
init.first="";init.second=0;
rec.push(init);
int i;
while (!rec.empty())
{
pair<string,int> curr=rec.front();
for (i=0;i<10;i++)
{
if (curr.first.length()==0&&i==0) continue;
if (a[i]) continue;
char ch='0'+i;
string ss=curr.first+ch;
int x=(curr.second*10+i)%n;
if (!f[x])
{
if (x==0) return ss;
pair<string,int>u;
u.first=ss;u.second=x;
rec.push(u);
f[x]=1;
}
}
rec.pop();
}
return "-1";
}

int main()
{
int i,n,m,x,cas=0;
while (ss(n)!=EOF)
{
ss(m);
cl(a);cl(f);
for (i=1;i<=m;i++)
{
ss(x);a[x]=1;
}
printf("Case %d: ",++cas);
cout<<find_ans(n,m)<<endl;
}
return 0;
}