HDU 4474 Yet Another Multiple Problem


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
 

Sample Output
  
  
Case 1: 2345 Case 2: -1
 

Source
 

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值