P2822 组合数问题
作者:互联网
题面:https://www.luogu.org/problem/P2822
本题直接将c[i][j]%k=1的(i,j)用前缀和数组记录下来,然后求前缀和即可.
注意:ans[i][j]=ans[i-1][j]+ans[i][j-1]-ans[i-1][j-1]
Code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<ctime>
using namespace std;
const int N=2010,M=2005;
long long t,k,n,m,i,j,f[N][N],a[N][N],ans[N][N];
int main(){
cin>>t>>k;
for(i=0;i<=M;i++){
a[i][0]=1;
a[i][i]=1;
}
for(i=1;i<=M;i++){
for(j=1;j<i;j++){
a[i][j]=(a[i-1][j-1]+a[i-1][j])%k;
}
}
for(i=1;i<=M;i++){
for(j=1;j<=M;j++){
ans[i][j]=ans[i-1][j]+ans[i][j-1]-ans[i-1][j-1];
if(a[i][j]==0&&j<=i){
ans[i][j]+=1;
}
}
}
while(t--){
cin>>n>>m;
cout<<ans[n][m]<<endl;
}
return 0;
}
标签:前缀,组合,int,long,P2822,问题,ans,include 来源: https://www.cnblogs.com/ukcxrtjr/p/11521805.html