Files
python/TangDou/AcWing/Combinations/885.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

35 lines
1.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 2010;
const int p = 1e9 + 7;
int c[N][N];
void init() {
/*
功能:第0列初始化为1,现实意义:C(n,0)=1
理解:从n个元素中,每次取出0个元素,也就是每次都不取出元素,有几种取法?
很明显只有1种取法,就是什么都不取这样一种情况。类似:C(n,0)=C(n,n)=1
这是递归的base case,如果没有这个1打底,以后再怎么加法原理都是白费
*/
for (int i = 1; i < N; i++) c[i][0] = 1, c[i][i] = 1; // base case
for (int i = 1; i < N; i++)
for (int j = 1; j < i; j++)
// 递归式最重要!这是根据数学公式得到的递推式
// 从递推式由右向左看过去,需要由小到大填充i,j,并且,i>j
c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % p;
}
int n;
int main() {
init();
cin >> n;
while (n--) {
int a, b;
cin >> a >> b;
cout << c[a][b] << endl;
}
return 0;
}