HDU 4474 Yet Another Multiple Problem - BenFromHRBUST

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

--------------------------------------------------------------分割线-------------------------------------------------------------

题意:

给定一个10^4的数n,最小的满足是n的倍数且不包含给的的m个数字中的任意一个。

思想

BFS从小到大搜索所有不包含m个数字的可能的数。由于余数等价的数在本题内等价,故可以用vis记录已经访问过的余数值。本题的答案可能较大,所以采用string储存数值,并且写了一个string的大数取余。

坑点

如果0不在题目中给出的m个数中,要注意第一位数不能是为0。

还有我自己的坑点。。。对于一个queue Q,我直接尝试Q.push(""+‘0’)。我崩溃了一晚上,还好想到了这个错误。

---------------------------------------------------分割线------------------------------------------------------------------------

代码

#include <bits/stdc++.h>
#define __bug(x) cout<<x<<endl;

using namespace std;

const int N=1e5+5;

int use[15];

int mod(string s,int md)
{
    int ret=0;
    for (int i=0; i<(int)s.length(); i++)
    {
        ret=(ret*10+s[i]-'0')%md;
    }
    return ret;
}

int main()
{
    int cas=1;
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0; i<10; i++)
        {
            use[i]=1;
        }
        for(int i=1; i<=m; i++)
        {
            int x;
            scanf("%d",&x);
            use[x]--;
        }
        queue<string> Q;
        vector<char> G;
        map<int,int> M;
        for(char i='0'; i<='9'; i++)
        {
            if(use[i-'0']==1)
            {
                G.push_back(i);
                string tmp="";
                if(i!='0')Q.push(tmp+i);
            }
        }
        string ans="-1";
        while(!Q.empty())
        {
            string now=Q.front();
//            cout<<now<<endl;
            int modd=mod(now,n);
            if(modd==0)
            {
                ans=now;
                break;
            }
            if(M[modd]==1)
            {
                Q.pop();
                continue;
            }
            M[modd]=1;
            for(int i=0; i<(int)G.size(); i++)
            {
                Q.push(now+G[i]);
            }
            Q.pop();
        }
        printf("Case %d: ",cas++);
        cout<<ans<<endl;
    }
    return 0;
}

over 第一篇博客纪念~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值