本文主要是介绍hdu4474 Yet Another Multiple Problem,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Yet Another Multiple 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题意,就是给一个数,求出这个数的最小倍数,且不包含,要排除的数字!其实,这题看起来挺简单的,我也刚开始,用高精度,一个一个乘,但发现行不通,因为,我们不知道,在什么时候,才能停下来,我们再仔细想想,n是很小的数的,我们可不可以把所有的模n的值都取到,这样的话,我们就可以,用一个bfs,枚举出所有的情况,这样,我们就可以从小到大的搜出来,因为我们一直都是从小取到大,这样的话,我们最先搜到的一定是最小值!我们就用取的模判重,如果在以来已经求到了这个模,那么我们也没必要再加长了啊,因为,在以前,都可以取到了,那么,为什么还要加长呢?而且这两个值是等价的!这样,我们就可以很快的搜到要求的值!
Sample Output
Case 1: 2345 Case 2: -1#include <iostream> #include <queue> #include <string> #include <string.h> #include <stdio.h> using namespace std; struct node{int x; string re; }; queue<node> q; int numcan[12],visit[10050],n; char str[2]; int bfs() {node temp,ptop;int i,modi;str[1]='\0';memset(visit,0,sizeof(visit));while(!q.empty()){q.pop();}for(i=1;i<=9;i++)//第一位不为0{if(numcan[i]){modi=i%n;temp.x=modi;str[0]=i+'0';temp.re=str;visit[modi]=1;if(modi==0){cout<<temp.re<<endl;return 1;}q.push(temp);}}while(!q.empty()){ptop=q.front();q.pop();for(i=0;i<=9;i++){if(numcan[i]){modi=(ptop.x*10+i)%n;if(modi==0){cout<<ptop.re<<i<<endl;return 1;}if(!visit[modi]){visit[modi]=1;str[0]=i+'0';temp.re=ptop.re+str;temp.x=modi;q.push(temp);}}}}printf("-1\n");return -1; } int main() {int m,tcase,i,num;tcase=1;while(scanf("%d%d",&n,&m)!=EOF){for(i=0;i<10;i++){numcan[i]=1;}for(i=0;i<m;i++){scanf("%d",&num);numcan[num]=0;}printf("Case %d: ",tcase++);bfs();}return 0; }
这篇关于hdu4474 Yet Another Multiple Problem的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!