HDU 4474 Yet Another Multiple Problem

来源:互联网 发布:linux setfacl 编辑:程序博客网 时间:2024/06/05 02:06

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 ≤ 104). 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 37 8 9100 10
 

Sample Output
Case 1: 2345Case 2: -1
 

Source
2012 Asia Chengdu Regional Contest
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  5490 5489 5488 5487 5486 

 


给定一个n,然后给你m个数字(范围0~9),要求找到一个n的最小倍数的数,不能包含这m个数字。

如果可用的数字少的话,直接枚举的话会超时,或者超long long 。

假如C%==0 AC%n也会==0  所以我们从低位开始枚举,从小到大,只要符合(number*10+i)%n==0 就输出,所以我们把可利用的数字进行bfs。我们用一个pre数组记录某个数字的父节点,num数组存要输出的数。


#include<bits/stdc++.h>using namespace std;int pre[1000000];int vis[15];int num[1000000];int n,m;void print(int xx){    if(pre[xx]!=-1)        print(pre[xx]);    printf("%d",num[xx]);}int main(){    int xxx=0;    while(~scanf("%d%d",&n,&m))    {        int i;        int x;        memset(vis,0,sizeof(vis));        memset(pre,-1,sizeof(pre));        memset(num,-1,sizeof(num));        for(i=0; i<m; i++)        {            scanf("%d",&x);            vis[x]=1;        }        printf("Case %d: ",++xxx);        int flag=-1;        int xx;        queue<int>q;        for(i=1; i<10; i++)        {            if(!vis[i])            {                xx=i%n;                if(xx==0)                {                    printf("%d\n",i);                    flag=1;                    break;                }                q.push(xx);                num[xx]=i;            }        }        if(flag==-1)        {            while(!q.empty())            {                int u=q.front();                q.pop();                for(i=0; i<10; i++)                {                    if(!vis[i])                    {                        xx=(u*10+i)%n;                        if(num[xx]==-1)                        {                            num[xx]=i;                            pre[xx]=u;                            q.push(xx);                        }                        if(xx==0)                        {                            flag++;                            break;                        }                    }                }                if(flag!=-1)                    break;            }            if(flag==-1)                puts("-1");            else            {                print(xx);                printf("\n");            }        }    }    return 0;}

0 0