题意
t组数据,问当0≤i≤n,i<j≤m中,有几组\(C_{i}^{j}\)能整除k
题解
依旧找规律,发现是\(\mathbb{YANGHITRIANGLE}\)
调试记录
杨辉三角要加前缀和,不然会TLE
#include <cstdio>
#include <algorithm>
#define maxn 2005
using namespace std;
int T, k, c[maxn][maxn], sum[maxn][maxn];
void Install();
int main(){
scanf("%d%d", &T, &k);
Install();
while (T--){
int n, m;
scanf("%d%d", &n, &m);
m = min(m, n);
printf("%d\n", sum[n][m]);
}
return 0;
}
void Install(){
c[1][1] = 1;
for (int i = 0; i <= 2000; i++) c[i][0] = 1;
for (int i = 2; i <= 2000; i++){
for (int j = 1; j <= i; j++){
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % k;
}
}
for (int i = 2; i <= 2000; i++){
for (int j = 1; j <= i; j++){
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
if (c[i][j] == 0) sum[i][j] += 1;
}
sum[i][i + 1] = sum[i][i];
}
}