You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}