zoukankan      html  css  js  c++  java
  • Yet Another Multiple Problem HDU 4474 BFS

    Yet Another Multiple Problem

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


    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
     
    Recommend
    liuyiding
     
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    #include<string>
    #include<map>
    #include<stack>
    #include<set>
    #include<iostream>
    #include<vector>
    #include<queue>
    //ios_base::sync_with_stdio(false);
    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    
    using namespace std;
    #define sz(v) ((int)(v).size())
    #define rep(i, a, b) for (int i = (a); i < (b); ++i)
    #define repf(i, a, b) for (int i = (a); i <= (b); ++i)
    #define repd(i, a, b) for (int i = (a); i >= (b); --i)
    #define clr(x) memset(x,0,sizeof(x))
    #define clrs( x , y ) memset(x,y,sizeof(x))
    #define out(x) printf(#x" %d
    ", x)
    #define sqr(x) ((x) * (x))
    typedef long long LL;
    
    const int INF = 1000000000;
    const double eps = 1e-8;
    const int maxn = 30000;
    
    int sgn(const double &x) {  return (x > eps) - (x < -eps); }
    
    int n,m;
    int cant[maxn];
    int vis[maxn];
    struct Node
    {
        int mod;
        int pre;
        int val;
        int id;
    }node[maxn];
    
    void print_ans(int i)
    {
        if(node[i].pre != -1)
            print_ans(node[i].pre);
        //cout<<i<<endl;
        printf("%d",node[i].val);
    }
    void bfs()
    {
        clr(vis);
        queue<Node> q;
        int cnt = 0;
        rep(i,1,10)
        {
            if(!cant[i] && !vis[i%n])
            {
                vis[i%n] = 1;
                node[cnt].mod = i%n;
                node[cnt].pre = -1; 
                node[cnt].val = i;
                node[cnt].id = cnt;
                q.push(node[cnt]);
                cnt++;
            }
        }
        while(!q.empty())
        {
            Node temp = q.front();
            q.pop();
            if(temp.mod == 0)
            {
                print_ans(temp.id);
                return ;
            }
            
            int ret;
            rep(i,0,10)
            {
                if(cant[i])
                    continue;
                ret  = temp.mod*10 + i;
                int a = ret%n;
                if(!vis[a])
                {
                    vis[a] = 1;
                    Node t;
                    t.mod = a;
                    t.val = i;
                    t.pre = temp.id;
                    t.id = cnt;
                    node[cnt] = t;
                    cnt++;
                    q.push(t);
                }
            }
        }
        printf("-1");
    }
    int main() 
    {
        //freopen("in.txt","r",stdin);
        int k = 1;
        while(scanf("%d%d",&n,&m) == 2)
        {
            clr(cant);
            rep(i,0,m)
            {
                int a;
                scanf("%d",&a);
                cant[a] = 1;
            }
            printf("Case %d: ",k++);
            bfs();
            cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    1.计算机初识
    re模块前瞻后顾 快速
    getattr 对类使用
    sklearn iris快速
    numpy c_
    sorted函数 字典按值倒序 实现
    logging快速入门
    configparser快速应用
    reduce 和 map 函数
    一个简单的类继承
  • 原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3367003.html
Copyright © 2011-2022 走看看