题意
原题链接
给定\(n,m,k\),对于所有的\(0\le i \le n,0 \le j \le min\{i,m\}\),有多少对\((i,j)\)满足\(k|(^i_j)\)
sol
在解决组合数问题时,若遇到\(n,m\le2000\)的情况,可以使用递推法(杨辉三角)来进行\(O(n^2)\)的预处理,再\(O(1)\)直接调用
递推法求组合数
\[(^n_m)=(^{n-1}_m)+(^{n-1}_{m-1}) \]证明:
\[(^{n-1}_m)+(^{n-1}_{m-1}) = \frac{(n-1)!}{m!(n-m-1)!} + \frac{(n-1)!}{(m-1)!(n-m)!} = \frac{n!}{m!(n-m)!} = (^n_m)\]对于本题,可以在预处理的同时维护二维前缀和,在每次查询时直接查表即可
代码
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 2005;
int n, m, k;
int c[N][N];
int s[N][N];
void init(){
c[0][0] = 1;
for (int i = 1; i <= 2000; i ++ ){
c[i][0] = 1;
for (int j = 1; j <= 2000; j ++ ){
if (j <= i){
c[i][j] = (c[i - 1][j] % k + c[i - 1][j - 1] % k) % k;
if (c[i][j] == 0) s[i][j] ++;
}
s[i][j] = s[i][j] + s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
}
}
}
int main(){
int T;
scanf("%d%d", &T, &k);
init();
while (T -- ){
scanf("%d%d", &n, &m);
long long cnt = 0;
for (int i = 1; i <= n; i ++ ){
cnt += s[i][min(i, m)] - s[i - 1][min(i, m)];
}
printf("%lld\n", cnt);
}
return 0;
}
标签:NOIP2016,le,frac,组合,int,lnsyoj166,luoguP2822,include
From: https://www.cnblogs.com/XiaoJuRuoUP/p/18251236/lnsyoj166_luoguP2822