hdu4474(BFS)

Yet Another Multiple Problem

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


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
 

Source
2012 Asia Chengdu Regional Contest
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6253  6252  6251  6250  6249 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define N 10005
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
typedef long long ll;

/*
给一个数n,求n的最小的倍数x,使得x中不包含给定的数字
例如2345 给定数字为7 8 9
最小的倍数x=2345;
1 Q;
    Q.push(0);
    int cur;//cur为当前检验的数字
    while(!Q.empty())
    {
        cur=Q.front();
        Q.pop();
        for(int i=0;i<10;i++)
        {
            if(del[i]==1||cur==0&&i==0)//如果是不允许使用的数字或者当前余数和数字都为0,继续
                continue;
            int yu=(cur*10+i)%n;//当前数字mod n之后的余数
            if(vis[yu])//已经到达过,不保存
                continue;
            text[yu]='0'+i;//记录到达新的余数时的数字i
            vis[yu]=true;
            pre[yu]=cur;//记录到达这个余数的前面那个余数
            Q.push(yu);
            if(yu==0)//若此时余数为0,说明获得答案,返回true
                return true;
        }
    }
    return false;
}

void print()
{
    string ans;
    int p=0;
    while(p!=0||ans.empty())//如果p不为0或者ans为空
    {
        ans+=text[p];//添加数字
        p=pre[p];//寻找前缀
    }
    reverse(ans.begin(),ans.end());//反转
    puts(ans.c_str());
}
int main()
{
	int m=0,cas=0;
	while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        memset(del,0,sizeof(del));
        while(m--)
        {
            int k;
            scanf("%d",&k);
            del[k]=1;//设置这个数字不能出现
        }
        printf("Case %d: ",++cas);
        if(!bfs())
            printf("-1\n");
        else print();
    }
	return 0;
}















你可能感兴趣的:(hdu4474(BFS))