现在的位置: 首页 > 综合 > 正文

hdu4474 Yet Another Multiple Problem

2013年09月13日 ⁄ 综合 ⁄ 共 2014字 ⁄ 字号 评论关闭
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?
 

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
题意,就是给一个数,求出这个数的最小倍数,且不包含,要排除的数字!其实,这题看起来挺简单的,我也刚开始,
用高精度,一个一个乘,但发现行不通,因为,我们不知道,在什么时候,才能停下来,我们再仔细想想,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;
}

抱歉!评论已关闭.