【矩阵快速幂】code forces 631E

【矩阵快速幂】code forces 631E

【题目链接】


题目内容

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的数组([1,9]),取b个数可重复的数连起来,比如取1和2会得到12,取2和2会得到22。求取得的数连起来对x取模得到k的种类数。取位置不同但值相同的数视为不同种类。
首先,记录一下1,2,3……出现的次数,记为num[i];
接下来一个一个数取,用dp[i][j]表示取好第i个数后%x==j的种类数,比如第i个数取k,对答案的贡献是dp[i+1][ (j*10+k) % x]=dp[i][j] * num[k];
dp[0][0]初始化为1;
不考虑复杂度的话就是

for(i=0;i<b;i++)
    for(j=0;j<x;j++)
        for(k=0;k<x;k++)
            dp[i+1][(j*10+k)%x]=dp[i][j]*num[k];

用矩阵快速幂来优化。
矩阵大概长
[ dp[0][0],dp[0][1],dp[0][2],……dp[0][x] ]*[某矩阵的(b)次幂] = [ dp[n][0],dp[n][1],dp[n][2],……dp[n][x] ]
这样。


AC代码

#include <bits/stdc++.h>
#define LL long long
using namespace std;

const int maxn=3007;
const int INF=0x3f3f3f3f;
const LL mod=1e9+7;
const int maxarr=100;///矩阵最大大小

LL m;///矩阵实际大小
LL num[107];
struct node
{
    LL arr[maxarr][maxarr];
};
void init(node &a)
{
    for(LL i=0;i<m;i++)
        for(LL j=0;j<m;j++)
            a.arr[i][j]=0;
}
node MatrixMultiplication(node &a,node &b)
{
    LL i,j,k;
    node c;
    init(c);
    for(i=0;i<m;i++)
        for(j=0;j<m;j++)
            for(k=0;k<m;k++)
                c.arr[i][j]=(c.arr[i][j]+a.arr[i][k]*b.arr[k][j])%mod;
    return c;
}
node fast_power(node a,LL n)
{
    node b;
    init(b);
    for(int i=0;i<maxarr;i++)
        b.arr[i][i]=1;
    while(n>0)
    {
        if(n&1==1) b=MatrixMultiplication(a,b);
        a=MatrixMultiplication(a,a);
        n>>=1;
    }
    return b;
}

int main()
{
    LL n,B,K,x,y,i,j,k;
    scanf("%lld%lld%lld%lld",&n,&B,&K,&x);
    m=x;
    for(i=1;i<=n;i++)
    {
        scanf("%lld",&y);
        num[y%x]++;
    }
    node a,b;
    init(a);
    for(j=0;j<x;j++)
    {
        for(k=0;k<x;k++)
        {
            a.arr[j][(j*10+k)%x]+=num[k];
        }
    }
    b=fast_power(a,B);
    init(a);
    a.arr[0][0]=1;
    a=MatrixMultiplication(a,b);
    printf("%lld\n",a.arr[0][K]);
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值