zoukankan      html  css  js  c++  java
  • Codeforces Round #341 (Div. 2) E. Wet Shark and Blocks

    There are b blocks of digits. Each one consisting of the same n digits, which are given to you in the input. Wet Shark must chooseexactly one digit from each block and concatenate all of those digits together to form one large integer. For example, if he chooses digit1 from the first block and digit 2 from the second block, he gets the integer 12.

    Wet Shark then takes this number modulo x. Please, tell him how many ways he can choose one digit from each block so that he gets exactly k as the final result. As this number may be too large, print it modulo 109 + 7.

    Note, that the number of ways to choose some digit in the block is equal to the number of it's occurrences. For example, there are 3ways to choose digit 5 from block 3 5 6 7 8 9 5 1 1 1 1 5.

    Input

    The first line of the input contains four space-separated integers, nbk and x (2 ≤ n ≤ 50 000, 1 ≤ b ≤ 109, 0 ≤ k < x ≤ 100, x ≥ 2) — the number of digits in one block, the number of blocks, interesting remainder modulo x and modulo x itself.

    The next line contains n space separated integers ai (1 ≤ ai ≤ 9), that give the digits contained in each block.

    Output

    Print the number of ways to pick exactly one digit from each blocks, such that the resulting integer equals k modulo x.

    Sample test(s)
    input
    12 1 5 10
    3 5 6 7 8 9 5 1 1 1 1 5
    
    output
    3
    
    input
    3 2 1 2
    6 2 2
    
    output
    0
    
    input
    3 2 1 2
    3 1 2
    
    output
    6
    

    Note


     题意:给你n个数,这n个数的大小都在1~9之间,有b块集合,每个集合内都有这n个数,你要从每个集合中取出一个数,并把它们依次拼接起来合并成一个大的整数,问最后这个整数%x得到k的方案数有多少。
    思路:我们可以先把1~9在n出现的次数用occ[i]存下来 ,然后用dp[i][j]表示取前i个数,最终模x后为j的方案数,那么容易得到dp[0][0]=1,dp[i][j]=sum{dp[i-1][a]*occ[d] }(其中(a*10+d)%x==j),但因为b太大,所以我们考虑用矩阵快速幂优化.

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    typedef __int64 ll;
    #define inf 99999999
    #define pi acos(-1.0)
    int a[50050],occ[20];
    #define MOD 1000000007
    struct matrix{
        ll n,m,i;
        ll data[105][105];
        void init_danwei(){
            for(i=0;i<n;i++){
                data[i][i]=1;
            }
        }
    };
    
    matrix multi(matrix &a,matrix &b){
        ll i,j,k;
        matrix temp;
        temp.n=a.n;
        temp.m=b.m;
        for(i=0;i<temp.n;i++){
            for(j=0;j<temp.m;j++){
                temp.data[i][j]=0;
            }
        }
        for(i=0;i<a.n;i++){
            for(k=0;k<a.m;k++){
                if(a.data[i][k]>0){
                    for(j=0;j<b.m;j++){
                        temp.data[i][j]=(temp.data[i][j]+(a.data[i][k]*b.data[k][j])%MOD )%MOD;
                    }
                }
            }
        }
        return temp;
    }
    
    matrix fast_mod(matrix &a,ll n){
        matrix ans;
        ans.n=a.n;
        ans.m=a.m;
        memset(ans.data,0,sizeof(ans.data));
        ans.init_danwei();
        while(n>0){
            if(n&1)ans=multi(ans,a);
            a=multi(a,a);
            n>>=1;
        }
        return ans;
    }
    
    int main()
    {
        int n,m,i,j,b,k,x,d;
        while(scanf("%d%d%d%d",&n,&b,&k,&x)!=EOF)
        {
            memset(occ,0,sizeof(occ));
            for(i=1;i<=n;i++){
                scanf("%d",&a[i]);
                occ[a[i] ]++;
            }
            matrix A;
            A.n=1;A.m=x;
            memset(A.data,0,sizeof(A.data));
            A.data[0][0]=1;
    
            matrix B;
            B.n=B.m=x;
            memset(B.data,0,sizeof(B.data));
            for(i=0;i<x;i++){
                for(j=0;j<x;j++){
                    for(d=1;d<=9;d++){
                        if( (i*10+d)%x==j ){
                            B.data[i][j]+=occ[d];  //这里构造矩阵的方法是求出余数为i到余数为j走一步的方案数,那么快速幂k次就是总的方案数了。
                        }
                    }
                }
            }
    
            matrix ant;
            ant=fast_mod(B,b);
            matrix ans;
            ans=multi(A,ant);
            printf("%I64d
    ",ans.data[0][k]);
    
        }
        return 0;
    }
    


  • 相关阅读:
    如何在一个页面后面随机跳转到多个链接地址Math.floor()和Math.random()
    thinkphp中volist标签
    PHP中删除数组空值的方法
    PHP实现四种基本排序算法
    如何解决自动加载与模板中(如Smarty)的自动加载冲突的问题
    GD库常用函数
    内网最小化安装CentOS后,想安装ISO文件中的包怎么办呢?
    Elasticsearch插件安装
    python类的反射使用方法
    python类的继承
  • 原文地址:https://www.cnblogs.com/herumw/p/9464578.html
Copyright © 2011-2022 走看看