P2822 组合数问题

嘁垃圾题目连接

递推加动规,就酱紫

#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<string>
#include<cmath>
#include<ctime>
#include<vector>
#include<bitset>
#include<memory>
#include<utility>
#include<cstdio>
#include<sstream>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

int n,m,k,t;
int a[2005][2005],f[2005][2005];

int ff(){
	a[1][1]=a[1][0]=1;
    for(int i=2;i<=2000;i++){
    	a[i][0]=1;
        for(int j=1;j<=i;j++){
            a[i][j]=(a[i-1][j]+a[i-1][j-1])%k;
        }
    }
    for(int i=2;i<=2000;i++){
        for(int j=1;j<=i;j++){
            f[i][j]=f[i-1][j]+f[i][j-1]-f[i-1][j-1];
            if(a[i][j]==0){
			    f[i][j]+=1;
			}
        }
        f[i][i+1]=f[i][i];
    }
}

int main(){
    //freopen("problem.in","r",stdin);
    //freopen("problem.out","w",stdout);
    scanf("%d%d",&t,&k);
    ff();
    while(t--){
        scanf("%d%d",&n,&m);
        if(m>n){
        	m=n;
        }
        printf("%d\n",f[n][m]);
    }
    return 0;
}

  嗯好的就这样

posted @ 2019-08-15 17:13  喵呜,颜儿ღ  阅读(81)  评论(0编辑  收藏  举报