hdu 4474 搜索bfs

来源:互联网 发布:怎样查网卡mac地址 编辑:程序博客网 时间:2024/05/22 10:25

Yet Another Multiple Problem

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


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 37 8 9100 10

Sample Output
Case 1: 2345Case 2: -1

Source
2012 Asia Chengdu Regional Contest 

/*题意:    给一个n(n<=10000), m个数字(长度为1),问最小的数x(x%n=0) 不包含这m个数。思路:  直接求,没想出解法.  对于一个数 x%n = m, 则 x` = x*10+i , 有 m` = (m*10+i)%n  我们可以利用 除了m个数字外的数来构造这个 X.  因为需要最小的, 则其长度与字典序排列皆最小. 通过BFS进行搜索. 每一个模n的余数只取第一次出现的,因为之后再出现的. 只可能更长或者更大. 必定不是最优解.  搜索节点,保存三个信息: 当前x的最后一位, 当前x%n的余数, 当前节点的父亲节点.  结果输出利用记忆父亲节点 ,然后递归输出即可.*/#include<stdio.h>#include<queue>#include<string.h>#define N 10005using namespace std;int b[N],pre[N],mark[N],n;void print(int k){    if(pre[k])        print(pre[k]);    printf("%d",b[k]);}void bfs(){    queue<int>q;    int i,r,x,y;    for(i=1; i<10; i++)    {        if(!mark[i])        {            r=i%n;            if(r==0)            {                printf("%d\n",i);                return;            }            else if(pre[r]==-1)            {                pre[r]=0;//pre指向前驱,此时无前驱                q.push(r);//根节点保存的mod值                b[r]=i;//保存i,用于输出            }        }    }    while(!q.empty())    {        x=q.front();        q.pop();        for(i=0; i<10; i++)        {            if(!mark[i])            {                y=x*10+i;                r=y%n;                if(r==0)                {                    print(x);                    printf("%d\n",i);                    return;                }//表示没有r这个mod没有访问过,则把它加入队列                if(pre[r]==-1)                {                    q.push(r);                    b[r]=i;                    pre[r]=x;                }            }        }    }    printf("-1\n");}int main(){    int m,i,a,cnt=1;    while(scanf("%d%d",&n,&m)!=EOF)    {        memset(mark,0,sizeof(mark));        memset(pre,-1,sizeof(pre));        for(i=1; i<=m; i++)        {            scanf("%d",&a);            mark[a]=1;        }        printf("Case %d: ",cnt++);        bfs();    }    return 0;}


0 0