【jzoj5215】【BZOJ4870】【Shoi2017】【GDOI2018模拟7.9】【组合数问题】【矩阵快速幂】

题目大意

【jzoj5215】【BZOJ4870】【Shoi2017】【GDOI2018模拟7.9】【组合数问题】【矩阵快速幂】_第1张图片

解题思路

暴力60分没毛病。r

code

#include
#include
#include
#include
#define LF double
#define LL long long
#define ULL unsigned LL
#define fo(i,j,k) for(LL i=j;i<=k;i++)
#define fd(i,j,k) for(LL i=j;i>=k;i--)
#define fr(i,j) for(LL i=begin[j];i;i=next[i])
using namespace std;
LL max(LL x,LL y){return (x>y)?x:y;}
LL min(LL x,LL y){return (xconst mk=50+9,inf=1e18+7;
LL n,p,K,r,ans[mk][mk],mat[mk][mk],tmp[mk][mk];
void multansmat(){
    fo(i,0,K-1)fo(j,0,K-1)tmp[i][j]=0;
    fo(i,0,K-1)fo(j,0,K-1)fo(k,0,K-1)tmp[i][k]=(tmp[i][k]+ans[i][j]*mat[j][k])%p;
    fo(i,0,K-1)fo(j,0,K-1)ans[i][j]=tmp[i][j];
}
void multmatmat(){
    fo(i,0,K-1)fo(j,0,K-1)tmp[i][j]=0;
    fo(i,0,K-1)fo(j,0,K-1)fo(k,0,K-1)tmp[i][k]=(tmp[i][k]+mat[i][j]*mat[j][k])%p;
    fo(i,0,K-1)fo(j,0,K-1)mat[i][j]=tmp[i][j];
}
int main(){
    freopen("d.in","r",stdin);
    freopen("d.out","w",stdout);
    scanf("%lld%lld%lld%lld",&n,&p,&K,&r);
    ans[0][0]=1;
    fo(i,0,K-1)mat[i][i]++,mat[i][(i+1==K)?0:i+1]++;
    LL nk=n*K;
    while(nk){
        if(nk&1)multansmat();
        multmatmat();
        nk>>=1;
    }
    printf("%lld",ans[0][r]);
    return 0;
}

你可能感兴趣的:(动态规划,bzoj,jzoj,矩阵乘法)