codeforces 621 E. Wet Shark and Blocks(dp+矩阵快速幂)

Description

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

题目大意

n位数,有b组,从每一组中选取一位数,依次组成一个b位数,计算有多少种选法可以满足结果%x=k.

解题思路

dp[i][j]表示从余数i到余数j的方法数,则在每一次的选取中dp[i][(i*10+j)%X]=dp[i][(i*10+j)%X]+num[j],num[j]记录数j 在一组中出现的次数;由于需要选取b 次且b太大,所以用矩阵快速幂来代替。

代码实现

#include<bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
typedef long long ll;
const int maxn = 107;
const int mod = 1e9+7;
struct matrix
{
    ll m[maxn][maxn];
}ans;
int X,num[10];
matrix multi(matrix a,matrix b)
{
    matrix c;
    for(int i=0;i<X;i++)
    {
        for(int j=0;j<X;j++)
        {
            c.m[i][j]=0;
            for(int k=0;k<X;k++)
                c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j]%mod)%mod;
        }
    }
    return c;
}
matrix quick_pow(matrix x,int t)
{
    matrix y;
    for(int i=0;i<X;i++) y.m[i][i]=1;
    while(t)
    {
        if(t&1)
            y=multi(x,y);
        x=multi(x,x);
        t>>=1;
    }
    return y;
}
int main()
{
    IO;
    int n,b,k,t;
    cin>>n>>b>>k>>X;(i*10+j)%X
    for(int i=0;i<n;i++)
    {
        cin>>t;
        num[t]++;
    }
    for(int i=0;i<X;i++)
    {
        for(int j=0;j<=9;j++)
        {
            ans.m[i][(i*10+j)%X]=(ans.m[i][(i*10+j)%X]+num[j])%mod;
        }
    }
    ans=quick_pow(ans,b); 
    cout<<ans.m[0][k]<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值