您的位置:首页 > 其它

HDU 4474 Yet Another Multiple Problem

2019-07-22 23:07 106 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_43383246/article/details/96910688

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 第一篇博客纪念~

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: