首页 > 代码库 > HDOJ 4474 Yet Another Multiple Problem

HDOJ 4474 Yet Another Multiple Problem


BFS.....

Yet Another Multiple Problem

Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3307    Accepted Submission(s): 806


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 3 7 8 9 100 1 0
 

Sample Output
Case 1: 2345 Case 2: -1
 

Source
2012 Asia Chengdu Regional Contest
 


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <string>

using namespace std;

string num[10]={"0","1","2","3","4","5","6","7","8","9"};

int n,m;
bool ex[10];

struct SU
{
	int yu;
	string num;
};
bool vis[200000];

int main()
{
	int cas=1;
	while(scanf("%d",&n)!=EOF&&n)
	{
		scanf("%d",&m);
		for(int i=0;i<10;i++) ex[i]=true;
		for(int i=0;i<m;i++)
		{
			int a; scanf("%d",&a); ex[a]=false;
		}
		printf("Case %d: ",cas++);
		queue<SU> q;
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=9;i++)	
		{
			if(ex[i]==true) 
			{
				q.push((SU){i%n,num[i]});
				vis[i*10]=true;
			}
		}
		bool flag=false;
		while(!q.empty())
		{
			SU u=q.front(); q.pop();
			if(u.yu==0)
			{
				cout<<u.num<<endl;
				flag=true;
				break;
			}
			for(int i=0;i<10;i++)
			{
				if(ex[i]==false) continue;
				int newyu=(u.yu*10+i)%n;	
				if(vis[newyu*10+i]==true) continue;
				vis[newyu*10+i]=true;
				q.push((SU){newyu,u.num+num[i]});
			}
		}
		if(flag==false) puts("-1");
	}
	return 0;
}



HDOJ 4474 Yet Another Multiple Problem