Yet Another Multiple Problem
Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4057 Accepted Submission(s): 947
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
Source
Recommend
看的别人代码,,,,很经典啊,,,收藏一下
ac代码
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
int v[10100],fin[10100],pre[10100],ans;
char s[10010];
int n,m;
int bfs()
{
int i,j;
queue<int>q;
q.push(0);
while(!q.empty())
{
int cur;
cur=q.front();
q.pop();
for(i=0;i<=9;i++)
{
if(fin[i]||i==0&&cur==0)
continue;
int mod=(cur*10+i)%n;
if(!v[mod])
{
v[mod]=1;
s[mod]=i+'0';
pre[mod]=cur;
q.push(mod);
if(mod==0)
{
// ans=cur*10+i;
return 1;
}
}
}
}
return 0;
}
void print()
{
string ans;
int p=0;
while(p!=0||ans.empty())
{
ans+=s[p];
p=pre[p];
}
reverse(ans.begin(),ans.end());
puts(ans.c_str());
}
int main()
{
//int n,m;
int c=0;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(v,0,sizeof(v));
memset(fin,0,sizeof(fin));
while(m--)
{
int num;
scanf("%d",&num);
fin[num]=1;
}
printf("Case %d: ",++c);
if(bfs())
{
print();
// printf("%d\n",ans);
}
else
printf("-1\n");
}
}