码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ4870:[SHOI2017]组合数问题——题解

时间:2018-02-21 22:20:15      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:str   lan   tps   ble   图片   target   style   def   就是   

http://www.lydsy.com/JudgeOnline/problem.php?id=4870

https://www.luogu.org/problemnew/show/P3746

技术分享图片

 

看网上一群人说“傻逼题”,我感觉我傻逼了。

首先我们把式子转换一下变成求有nk件物品,我取的物品数%k==r的方案数有多少。

显然f[i][j]=f[i-1][j]+f[i-1][j-1]。

但就没人教一下f[i][j]=f[i-1][j]+f[i-1][j-1]如何矩乘吗……

那我就引洛谷的题解了:

可以加速的原理,其实就是杨辉三角是一个一维递推,并且可以将递推描述为:复制矩阵到一个新矩阵,然后矩阵右移一格,加到新矩阵中。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
ll n,p,K,r;
struct node{
    ll g[51][51];
    node(){
        memset(g,0,sizeof(g));
    }
    friend node operator *(const node &x,const node &y){
        node z;
        for(int i=0;i<K;i++)
            for(int j=0;j<K;j++)
                for(int k=0;k<K;k++)
                    z.g[i][k]=(z.g[i][k]+x.g[i][j]*y.g[j][k]%p)%p;
        return z;
    }
}f,t,res;
int main(){
    cin>>n>>p>>K>>r;
    t.g[0][0]=1;
    for(int i=0;i<K;i++){
        f.g[(i-1+K)%K][i]++;
        f.g[i][i]++;
        res.g[i][i]=1;
    }
    n*=K;
    while(n){
        if(n&1)res=res*f;
        f=f*f;n>>=1;
    }
    printf("%lld\n",(t*res).g[0][r]);
    return 0;
}

+++++++++++++++++++++++++++++++++++++++++++

 +本文作者:luyouqi233。               +

 +欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+

+++++++++++++++++++++++++++++++++++++++++++

BZOJ4870:[SHOI2017]组合数问题——题解

标签:str   lan   tps   ble   图片   target   style   def   就是   

原文地址:https://www.cnblogs.com/luyouqi233/p/8457445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!