Description
有b堆数,每堆数都相同的有n个数a[i],现在从每堆选一个数组成一个b位数,问该b位数模x后为k的方案数
Input
第一行四个整数n,b,k,x,之后n个整数a[i](2<=n<=5e4,1<=b<=1e9,0<=k < x<=100,x>=2,1<=a[i]<=9)
Output
输出方案数
Sample Input
12 1 5 10
3 5 6 7 8 9 5 1 1 1 1 5
Sample Output
3
Solution
假设从第i位取的数放在从低往高第i位,num[i]表示这堆数中i的数量,dp[i][j]表示从前i堆数中选的数组成的i位数模x结果为j的方案数,那么根据从第i堆选取的数有以下转移方程:dp[i][((10k+j)%x]+=dp[i-1][j]*num[k],1<=k<=9,那矩阵快速幂加速下该转移即可
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 111
#define mod 1000000007ll
struct Mat
{
ll mat[maxn][maxn];//矩阵
int row,col;//矩阵行列数
};
Mat mod_mul(Mat a,Mat b,ll p)//矩阵乘法
{
Mat ans;
ans.row=a.row;
ans.col=b.col;
memset(ans.mat,0,sizeof(ans.mat));
for(int i=0;i<ans.row;i++)
for(int k=0;k<a.col;k++)
if(a.mat[i][k])
for(int j=0;j<ans.col;j++)
{
ans.mat[i][j]+=a.mat[i][k]*b.mat[k][j]%p;
ans.mat[i][j]%=p;
}
return ans;
}
Mat mod_pow(Mat a,int k,ll p)//矩阵快速幂
{
Mat ans;
ans.row=a.row;
ans.col=a.col;
for(int i=0;i<a.row;i++)
for(int j=0;j<a.col;j++)
ans.mat[i][j]=(i==j);
while(k)
{
if(k&1)ans=mod_mul(ans,a,p);
a=mod_mul(a,a,p);
k>>=1;
}
return ans;
}
int n,b,k,x,a[maxn],num[maxn];
Mat A,B;
int main()
{
while(~scanf("%d%d%d%d",&n,&b,&k,&x))
{
memset(num,0,sizeof(num));
for(int i=0;i<n;i++)
{
int temp;
scanf("%d",&temp);
num[temp]++;
}
A.col=A.row=x;
memset(A.mat,0,sizeof(A.mat));
for(int i=0;i<x;i++)
for(int j=0;j<10;j++)
A.mat[(i*10+j)%x][i]+=num[j];
B=mod_pow(A,b,mod);
printf("%I64d\n",B.mat[k][0]);
}
return 0;
}