zoukankan      html  css  js  c++  java
  • 【矩阵乘法优化dp】[Codeforces 621E] Wet Shark and Blocks

    http://codeforces.com/problemset/problem/621/E

    E. Wet Shark and Blocks

    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    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 3 ways 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, n, b, k 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.

    Examples

    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

    In the second sample possible integers are 22, 26, 62 and 66. None of them gives the remainder 1 modulo 2.

    In the third sample integers 11, 13, 21, 23, 31 and 33 have remainder 1 modulo 2. There is exactly one way to obtain each of these integers, so the total answer is 6.

    【题目大意】

    n个数,每个数可重复多次选取,选b个数组成一个b位数,求这个数mod k=x的方案数,答案模10^9+7

    题解

    dp[i][j]表示前i个数 mod x=j的方案数,cnt[i]表示i出现的次数,显然dp[i][(i*10+j)%x]=dp[i-1][j]*cnt[j];

    矩乘优化即可。

    #include<iostream>
    #include<cstdlib>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #define mod 1000000007
    using namespace std;
    
    int n,b,x,k;
    int num[15];
    struct matrix
    {
        long long a[105][105];
        int n,m;
    }ans,temp;
    matrix mult(matrix,matrix);
    matrix pow(matrix,int);
    
    matrix pow(matrix a,int k)
    {
        int i,j;
        matrix c;
        memset(c.a,(long long)0,sizeof(c.a));
        for(i=0;i<=x-1;i++) c.a[i][i]=(long long)1;
        while(k)
        {
            if(k&1) c=mult(a,c);
            a=mult(a,a);
            k>>=1;
        }
        return c;
    }
    
    matrix mult(matrix a,matrix b)
    {
        int i,j,k;
        matrix c;
        memset(c.a,(long long)0,sizeof(c.a));
        for(i=0;i<=x-1;i++)
            for(j=0;j<=x-1;j++)
                for(k=0;k<=x-1;k++)
                    c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j])%mod;
        return c;
    }
    
    int main()
    {
        int i,j,t;
        scanf("%d%d%d%d",&n,&b,&k,&x);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&t);
            num[t]++;
        }
        for(i=0;i<=x-1;i++)
            for(j=0;j<=9;j++)
                 temp.a[i][(i*10+j)%x]=(temp.a[i][(i*10+j)%x]+(long long)num[j])%mod;
        ans=pow(temp,b);
        printf("%lld",ans.a[0][k]);
        return 0;
    }
  • 相关阅读:
    Android用户界面UI组件--AdapterView及其子类(五) Spinner和SpinnerAdapter
    Android用户界面UI组件--AdapterView及其子类(四) GridView
    Android用户界面UI组件--AdapterView及其子类(三) ExpandableListView
    Android用户界面 UI组件--AdapterView及其子类(二) AdapterViewAnimator及其子类
    Navigation Drawer介绍
    Android用户界面 UI组件--AdapterView及其子类(一) ListView及各种Adapter详解
    draw9patch超详细教程
    主线程中有多个handler的情况
    Python服务器开发二:Python网络基础
    shell脚本实现无密码交互的SSH自动登陆
  • 原文地址:https://www.cnblogs.com/yljiang/p/5815893.html
Copyright © 2011-2022 走看看