zoukankan      html  css  js  c++  java
  • 【BZOJ4870】组合数问题 [矩阵乘法][DP]

    组合数问题

    Time Limit: 10 Sec  Memory Limit: 512 MB
    [Submit][Status][Discuss]

    Description

      

    Input

      第一行有四个整数 n, p, k, r,所有整数含义见问题描述。

    Output

      一行一个整数代表答案。

    Sample Input

      2 10007 2 0

    Sample Output

      8

    HINT

      1 ≤ n ≤ 10^9, 0 ≤ r < k ≤ 50, 2 ≤ p ≤ 2^30 − 1

    Solution

      首先,不难发现,题目的本质是:从n*k个中选模k等于r个的方案数,那么轻易地写出了暴力DPf[i][j]=f[i-1][j]+f[i-1][(j-1+k)%k]

      然后套个矩阵乘法优化一下即可。

    Code

    #include<iostream>  
    #include<string>  
    #include<algorithm>  
    #include<cstdio>  
    #include<cstring>  
    #include<cstdlib>  
    #include<cmath>
    using namespace std;
    typedef long long s64;
    
    const int ONE = 55;
    
    int n,MOD,num,r;
    
    inline s64 get()
    {
            s64 res=1,Q=1;  char c;
            while( (c=getchar())<48 || c>57)
            if(c=='-')Q=-1;
            if(Q) res=c-48; 
            while((c=getchar())>=48 && c<=57) 
            res=res*10+c-48;
            return res*Q; 
    }
    
    struct Matrix
    {
            s64 v[ONE][ONE];
            friend Matrix operator *(Matrix a,Matrix b)
            {
                Matrix record;
                for(int i=0;i<num;i++)
                for(int j=0;j<num;j++)
                {
                    record.v[i][j]=0;
                    for(int k=0;k<num;k++)
                    record.v[i][j] = (s64)(record.v[i][j] + a.v[i][k]*b.v[k][j] % MOD) % MOD;
                }
                return record;
            }
    };
    Matrix B,Ans;
    
    Matrix Quickpow(Matrix a,s64 b)
    {
            Matrix res;
            for(int i=0;i<num;i++) res.v[i][i] = 1;
            while(b)
            {
                if(b&1) res = res*a;
                a = a*a;
                b>>=1;
            }
            return res;
    }
    
    int main()
    {
            n=get();    MOD=get();    num=get();    r=get();
            for(int i=0;i<num;i++)
            {
                B.v[i][i]++;
                B.v[((i-1)%num+num)%num][i]++;
            }
            
            Ans = Quickpow(B, (s64)n*num);
            
            cout<<Ans.v[0][r];
            
    }
    View Code
  • 相关阅读:
    ubuntu install ssh server
    blug聚会&&小资早餐
    virtual box share folder usage
    关于xrdp的安装设置
    使用scp传送文件
    firefox插件集锦
    原来ubuntu早有关机功能
    blug聚会&&小资早餐
    加域工具
    ubuntu安装virtual box在命令行
  • 原文地址:https://www.cnblogs.com/BearChild/p/6824207.html
Copyright © 2011-2022 走看看