bzoj 4465 游戏中的学问(game)

题目描述

 

 

输入

 

 

输出

 

 

 

样例输入

3 1 1000000009

样例输出

2

提示

 

 


solution

令f[i][j]表示i个人围成j个圈的方案数

f[i][j]=f[i-1][j]*(i-1)+f[i-3][j-1]*C(n-1,2)*2

啥意思呢

可以把一个人塞进前面的圈里(i-1种塞法)

或者新开一个圈(2种)

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n,k,p;
long long f[3002][1002];
int main()
{
    cin>>n>>k>>p;
    f[0][0]=1;
    for(int i=1;i<=n;i++){
        for(int j=1;j*3<=i;j++){
            f[i][j]=f[i][j]+((f[i-1][j]*(i-1))%p);
            f[i][j]%=p;
            if(i>=3){
                f[i][j]=f[i][j]+((f[i-3][j-1]*(i-1))%p*(i-2)%p);
                f[i][j]%=p;
            }
        }
    }
    cout<<f[n][k]<<endl;
    return 0;
}

 

 

posted @ 2018-09-11 20:06  liankewei123456  阅读(144)  评论(0编辑  收藏  举报