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;
}
时间: 2024-10-15 18:28:35

HDOJ 4474 Yet Another Multiple Problem的相关文章

HDU 4474 Yet Another Multiple Problem

Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4734    Accepted Submission(s): 1021 Problem Description There are tons of problems about integer multiples. Despit

HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】

Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3407    Accepted Submission(s): 825 Problem Description There are tons of problems about integer multiples. Despit

【暴力+BFS】HDU 4474 Yet Another Multiple Problem

通道:http://acm.hdu.edu.cn/showproblem.php?pid=4474 题意:给出n和m个数位,求一个数X,这个数是n的最小倍数且他的每一位都不含有m个数位中的任意一个. 思路:反过来想,其实就是有非M的元素组成一个数,这个数是N的倍数.如果存在解,那么他的第一位便是非M组合中的任意一位,然后除N后,他的余数便是X-a*n,那么下一位除N的就是(X-a*n)*10+(枚举的下一个非M元素),要求最小这个数,那么BFS跑最短就可以了. 代码:https://github

[bfs+余数判重+路径记录] hdu 4474 Yet Another Multiple Problem

题意: 给一个n和m个数字(一位数) 求最小的n的倍数不含有这m个数字,不存在输出-1 思路: 首先有可能这个数超long long 所以无法暴力解决 所以这题应该是一个bfs 为什么能用余数判重呢 对于当前的余数进到队列里,一定是这个余数对应数的最小值 接下来再怎么添加到满足条件的后续东西应该是一样的 所以就可以余数判重了,类似数位dp的记录方式 然后再加上一个路径记录就好了 代码: #include"cstdlib" #include"cstdio" #incl

2012Chhengdu K - Yet Another Multiple Problem

K - Yet Another Multiple Problem Time Limit:20000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4474 Appoint description:  System Crawler  (2014-10-16) Description There are tons of problems about integer mul

HDOJ 4974 A simple water problem

A simple water problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 173    Accepted Submission(s): 112 Problem Description Dragon is watching competitions on TV. Every competition is held be

Yet Another Multiple Problem(bfs好题)

Yet Another Multiple Problem Time Limit : 40000/20000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) Total Submission(s) : 2   Accepted Submission(s) : 1 Problem Description There are tons of problems about integer multiples. Despite the f

水题 HDOJ 4716 A Computer Graphics Problem

题目传送门 1 /* 2 水题:看见x是十的倍数就简单了 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <string> 9 #include <cmath> 10 using namespace std; 11 12 const int MAXN = 1e4 + 10; 13

hdoj 5349 MZL&#39;s simple problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5349 1 #include<stdio.h> 2 int main(){ 3 int cnt; 4 int max; 5 int N; 6 int ch; 7 while(~scanf("%d",&N)){ 8 cnt = 0; 9 while(N--){ 10 scanf("%d",&ch); 11 if(ch==1){ 12 scanf(