Codeforces 621E Wet Shark and Blocks【Dp+矩阵快速幂】

351 篇文章 2 订阅
25 篇文章 0 订阅

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 choose exactly one digit from each block and concatenate all of those digits together to form one large integer. For example, if he chooses digit 1 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个块,每个块都是n个数,且都一样,现在让你从每个块取一个数,并且按序拼接在一起之后%x==k的方案数。

按序拼接:第一个块取的数是i,第二个块取得数是j,第三个块取的数是k,那么按序拼接在一起得到的数就是ijk.


思路:


1、

统计方案数,考虑dp,不难想到设定dp【i】【j】表示进行到第i个块,所取数据%x==j的方案数。

那么就有:

dp【i】【(j*10+l)%m】+=dp【i-1】【j】*num【l】.表示当前这个块取了数字l拼接到最后的方案数转移情况。

观察到b有1e9辣么大。那么可以使用矩阵快速幂进行优化。


2、矩阵的设定:

对于第一个矩阵中的数据,要根据实际情况决定。



Ac代码:


#include<stdio.h>
#include<string.h>
using namespace std;
#define mod 1000000007
#define ll __int64
typedef struct Matrix
{
    ll mat[102][102];
}matrix;
matrix A,B,tmp;
ll num[15];
ll n,b,x,m;
Matrix matrix_mul(matrix a,matrix b)
{
    matrix c;
    memset(c.mat,0,sizeof(c.mat));
    ll i,j,k;
    for(ll i=0;i<m;i++)
    {
        for(ll j=0;j<m;j++)
        {
            for(ll k=0;k<m;k++)
            {
                c.mat[i][j]=(c.mat[i][j]+(a.mat[i][k]*b.mat[k][j])%mod)%mod;
            }
        }
    }
    return c;
}
Matrix matrix_quick_power(matrix a,ll k)//矩阵快速幂0.0
{
    matrix b;
    memset(b.mat,0,sizeof(b.mat));
    for(ll i=0;i<m;i++)
    b.mat[i][i]=1;//单位矩阵b
    while(k)
    {
        if(k%2==1)
        {
            b=matrix_mul(a,b);
            k-=1;
        }
        else
        {
            a=matrix_mul(a,a);
            k/=2;
        }
    }
    return b;
}
int main()
{
    while(~scanf("%I64d%I64d%I64d%I64d",&n,&b,&x,&m))
    {
        memset(tmp.mat,0,sizeof(tmp.mat));
        memset(A.mat,0,sizeof(A.mat));
        memset(num,0,sizeof(num));
        for(ll i=1;i<=n;i++)
        {
            ll x;scanf("%I64d",&x);
            num[x]++;
        }
        for(ll i=0;i<m;i++)
        {
            for(ll j=0;j<m;j++)
            {
                for(ll l=1;l<=9;l++)
                {
                    ll tmpnum=j*10+l;
                    tmpnum%=m;
                    if(tmpnum==i)
                    {
                        A.mat[i][j]+=num[l];
                    }
                }
            }
        }
        for(ll i=0;i<m;i++)
        {
            for(ll j=1;j<=9;j++)
            {
                if(j%m==i)
                {
                    tmp.mat[i][0]+=num[j];
                }
            }
        }
        B=matrix_quick_power(A,b-1);
        B=matrix_mul(B,tmp);
        printf("%I64d\n",B.mat[x][0]);
    }
}








  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值