Dream_Chaser队训练赛第一场 K题

Dream_Chaser队训练赛第一场 K题

题目来自2012成都区域赛

K - Yet Another Multiple Problem
Time Limit:20000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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
 
题意:找出不含给定数字的n的倍数。
思路:bfs+剪枝。利用同余模定理,如果A%n==B%n,则A和B只搜一个,就不用重复搜了,因为在A和B后添加尾数后的效果是一样的,比如添加尾数C,AC%n=(A*10+C)%n=(A%n)*(10%n)+C%n, BC%n=(B%n)*(10%n)+C%n,显然相等。所以从小到大不断对余数添加合法的尾数取余,利用余数判重即可,利用pre数组回溯路径来输出每一位。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<math.h>
#include<cctype>

using namespace std;

typedef long long ll;
const int maxn=1000100;
const int INF=(1<<29);
const double EPS=0.0000000001;
const double Pi=acos(-1.0);

ll n,m;
bool a[20];
int pre[maxn];
int num[maxn];
int tag=1;

void bfs()
{
    memset(pre,-1,sizeof(pre));
    memset(num,-1,sizeof(num));
    queue<int> q;
    for(int i=1;i<=9;i++){
        if(!a[i]){
            if(i%n==0){
                printf("%d\n",i);
                return;
            }
            q.push(i);
            pre[i%n]=-1;
            num[i%n]=i;
        }
    }
    while(!q.empty()){
        int u=q.front();q.pop();
        if(u==0){
            stack<int> s;
            for(int i=u;i!=-1;i=pre[i]){
                s.push(num[i]);
            }
            while(!s.empty()){
                printf("%d",s.top());
                s.pop();
            }
            printf("\n");
            return;
        }
        for(int i=0;i<=9;i++){
            if(!a[i]){
                int v=(u*10+i)%n;
                if(num[v]==-1){
                    q.push(v);
                    pre[v]=u;
                    num[v]=i;
                }
            }
        }
    }
    printf("-1\n");
}

int main()
{
    while(cin>>n>>m){
        memset(a,0,sizeof(a));
        while(m--){
            int t;
            scanf("%d",&t);
            a[t]=1;
        }
        printf("Case %d: ",tag++);
        bfs();
    }
    return 0;
}
View Code

 

 
posted @ 2015-06-01 00:40  __560  阅读(206)  评论(0编辑  收藏  举报