Dream_Chaser队训练赛第一场 K题

Dream_Chaser队训练赛第一场 K题

题目来自2012成都区域赛

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

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

 

 

转载于:https://www.cnblogs.com/--560/p/4543051.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值