zoukankan      html  css  js  c++  java
  • hdu 4474 大整数取模+bfs

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4474

    (a*10+b)%c = ((a%c)*10+b%c)%c;

    然后从高位开始枚举能填的数字填充,只是注意最高位(第一位)不能为0。

    代码:

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<string>
    #include<queue>
    using namespace std;
    
    struct Node
    {
        string s;
        int mod;
        Node(string s="",int mod=0): s(s),mod(mod) {}
    };
    
    bool can[15];
    bool vis[10050];
    string ans;
    
    bool bfs(int n)
    {
        queue<Node> Q;
    
        string temp = "";
        Q.push(Node(temp,0));
    
        while(!Q.empty())
        {
            Node node = Q.front();
            Q.pop();
    
            if(node.mod == 0 && node.s != "" && node.s!="0")
            {
                ans = node.s;
                return true;
            }
    
            for(int i=0; i<=9; i++)
            {
                if(!can[i] ) continue;
                if(i == 0 && node.s == "") continue;
                int mod = (node.mod*10 + i)%n;
                if(vis[mod]) continue;
    
                temp = node.s + char(i + '0');
                vis[mod] = true;
    
                Q.push(Node(temp,mod));
            }
        }
        return false;
    }
    
    int main()
    {
        //freopen("E:\acm\input.txt","r",stdin);
    
        int n,m;
        int T = 0;
        while(cin>>n>>m)
        {
            for(int i=0; i<=9; i++) can[i] = true;
            for(int i=0; i<m; i++)
            {
                int a;
                scanf("%d",&a);
                can[a] = false;
            }
            ans = "";
            memset(vis,0,sizeof(vis));
            printf("Case %d: ",++T);
    
            if(!bfs(n))
                printf("-1
    ");
            else
                cout<<ans<<endl;
        }
    }
    View Code
  • 相关阅读:
    Paths on a Grid
    Three Kingdoms(优先队列+bfs)
    Factstone Benchmark(数学)
    C. Searching for Graph(cf)
    B. Trees in a Row(cf)
    String Successor(模拟)
    乘积最大的分解(数学)
    Kindergarten Election
    In 7-bit
    Friends
  • 原文地址:https://www.cnblogs.com/acmdeweilai/p/3352709.html
Copyright © 2011-2022 走看看