HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】

Yet Another Multiple Problem

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

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

Sample Output

Case 1: 2345 Case 2: -1

Source

2012 Asia Chengdu Regional Contest

Recommend

liuyiding

题意转自:http://blog.csdn.net/yang_7_46/article/details/12356587

题意:0-9这十个数字里面的若干个数字组合出一个数,使这个数是N的倍数,求最小的这个这样的数,不存在的话输出-1。

按照数的位数BFS,从小向大枚举就可以保证构造出来的数是递增的,如果不加判断就直接搜索的话,复杂度非常高。因此需要剪枝。

优化方法:如果一个数%N==0,那么这个数就是N的倍数。在没有找到的前提下,如果A%N==B%N,而且A<B,那么其实我们就可以取A而不取B,因为如果在A末尾增加C可以使得AC%N==0,那么BC%N也等于0,易得:如果A和B追加数之后%N==0,那么最优条件下追加的数肯定相同。

因此我们只需要维护组合出来的数%N的值即可,如果在搜索的途中出现了相同的%N值,就可以直接忽略了,因为肯定没有前面的优秀。

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<string>
//#include<pair>

#define N  10005
#define M 10005
#define mod 10000007
//#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b)

using namespace std;

int n,ans;
int m;
int vis[15];
int pre[N];
int val[N];

void ini()
{
    int x;
    ans=-1;
    memset(vis,0,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    while(m--){
        scanf("%d",&x);
        vis[x]=1;
    }
}

void out(int x)
{
    if(pre[x]){
        out(pre[x]);
    }
    printf("%d",val[x]);
}

void solve()
{
    int i;
    queue<int> q;
    for(i=1;i<=9;i++){
        if(vis[i]==1) continue;
        if(i>=n && i%n==0){
            printf("%d\n",i);
            return;
        }
        if(pre[i%n]!=-1) continue;
        pre[i%n]=0;
        val[i%n]=i;
        q.push(i);
    }
    while(q.size()>=1)
    {
        int te=q.front();
        int nx;
        q.pop();
        for(i=0;i<=9;i++){
            if(vis[i]==1) continue;
            nx=(te*10+i)%n;
            if(nx%n==0){
                out(te);
                printf("%d\n",i);
                return;
            }
            if(pre[nx]!=-1) continue;
            pre[nx]=te;
            val[nx]=i;
            q.push(nx);
        }
    }
    printf("-1\n");
    return;
}

int main()
{
    int cnt=1;
   // freopen("data.in","r",stdin);
    //freopen("data.out","w",stdout);
   // scanf("%d",&T);
   // for(int ccnt=1;ccnt<=T;ccnt++)
   // while(T--)
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        //if(n==0 && k==0 ) break;
        //printf("Case %d: ",ccnt);
        printf("Case %d: ",cnt);
        ini();
        solve();

        cnt++;
//        out();
    }

    return 0;
}
时间: 2024-08-04 22:12:33

HDU 4474 Yet Another Multiple Problem【2012成都regional K题】 【BFS+一个判断技巧】的相关文章

HDU 4474 Yet Another Multiple Problem

Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4734    Accepted Submission(s): 1021 Problem Description There are tons of problems about integer multiples. Despit

【暴力+BFS】HDU 4474 Yet Another Multiple Problem

通道:http://acm.hdu.edu.cn/showproblem.php?pid=4474 题意:给出n和m个数位,求一个数X,这个数是n的最小倍数且他的每一位都不含有m个数位中的任意一个. 思路:反过来想,其实就是有非M的元素组成一个数,这个数是N的倍数.如果存在解,那么他的第一位便是非M组合中的任意一位,然后除N后,他的余数便是X-a*n,那么下一位除N的就是(X-a*n)*10+(枚举的下一个非M元素),要求最小这个数,那么BFS跑最短就可以了. 代码:https://github

[bfs+余数判重+路径记录] hdu 4474 Yet Another Multiple Problem

题意: 给一个n和m个数字(一位数) 求最小的n的倍数不含有这m个数字,不存在输出-1 思路: 首先有可能这个数超long long 所以无法暴力解决 所以这题应该是一个bfs 为什么能用余数判重呢 对于当前的余数进到队列里,一定是这个余数对应数的最小值 接下来再怎么添加到满足条件的后续东西应该是一样的 所以就可以余数判重了,类似数位dp的记录方式 然后再加上一个路径记录就好了 代码: #include"cstdlib" #include"cstdio" #incl

HDOJ 4474 Yet Another Multiple Problem

BFS..... Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3307    Accepted Submission(s): 806 Problem Description There are tons of problems about integer multiple

hdu 2680 Choose the best route 大年三十的首A 赤裸裸的Dijkstra 做这题需要一个小技巧

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8063    Accepted Submission(s): 2655 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

hdu 4463 第37届ACM/ICPC杭州赛区K题 最小生成树

题意:给坐标系上的一些点,其中有两个点已经连了一条边,求最小生成树的值 将已连接的两点权值置为0,这样一定能加入最小生成树里 最后的结果加上这两点的距离即为所求 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map>

HDU 4474(神奇的BFS+强剪枝)

 HDU - 4474 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 hi

hdu 4465 Candy 2012 成都现场赛

1 /** 2 对于大数的很好的应用,,缩小放大,,保持精度 3 **/ 4 #include <iostream> 5 #include <cmath> 6 #include <algorithm> 7 #include <cstdio> 8 using namespace std; 9 10 int main() 11 { 12 double n,p; 13 int cnt =1; 14 while(cin>>n>>p){ 15

2012Chhengdu K - Yet Another Multiple Problem

K - Yet Another Multiple Problem Time Limit:20000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4474 Appoint description:  System Crawler  (2014-10-16) Description There are tons of problems about integer mul