组合数问题

[Time Gate]

https://www.luogu.org/problem/P2822

【解题思路】

前缀和+递推打表

【code】

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 using namespace std;
 5 const int N=2000;
 6 int i,j,t,k,n,m,sum[N+5][N+5];
 7 int a[N+5][N+5]; 
 8 unsigned long long ans;
 9 int main(){
10     scanf("%d%d",&t,&k);
11     a[1][0]=1;        
12     for (i=1;i<N;i++){
13         a[i][0]=a[i][i]=1;
14         for (j=1;j<i;j++)
15         a[i][j]=(a[i-1][j-1]+a[i-1][j])%k;
16     }
17     for (i=1;i<=N;i++){
18         for(j=1;j<=i;j++){
19             sum[i][j]=sum[i][j-1];
20             if(a[i][j]==0)sum[i][j]++;
21         }
22     }
23     while(t--){
24         scanf("%d%d",&n,&m);
25         ans=0;
26         for(i=1;i<=n;i++){
27             if(i<m)j=i;
28             else j=m;
29             ans+=sum[i][j];    
30         }
31         printf("%lld\n",ans);
32     }
33     return 0;
34 }

 

posted @ 2019-08-20 12:56  GTR_PaulFrank  阅读(103)  评论(0编辑  收藏  举报