版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_36797646/article/details/86012038
Solution
题目求的实际上是在
个物品中,选
个(
)个物品的方案数,而
又很小,
很大,所以直接矩阵乘法就行了。
要注意
的时候,一开始转移矩阵是直接赋值,但因为
的存在,要
。
Code
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pa pair<int,int>
const int Maxk=52;
const int inf=2147483647;
int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
return x*f;
}
int n,p,K,r;
struct Matrix{int v[Maxk][Maxk];}one,st,M;
Matrix operator*(Matrix a,Matrix b)
{
Matrix c;memset(c.v,0,sizeof(c.v));
for(int i=0;i<K;i++)
for(int j=0;j<K;j++)
for(int k=0;k<K;k++)
c.v[i][j]=(c.v[i][j]+(LL)a.v[i][k]*b.v[k][j]%p)%p;
return c;
}
Matrix operator^(Matrix x,LL y)
{
Matrix re=one;
while(y)
{
if(y&1)re=re*x;
x=x*x;y>>=1;
}
return re;
}
int main()
{
n=read(),p=read(),K=read(),r=read();
for(int i=0;i<K;i++)one.v[i][i]=1,M.v[i][i]++,M.v[i][(i+1)%K]++;
st.v[0][0]=1;
st=st*(M^((LL)n*K));
printf("%d",st.v[0][r]);
}