AcWing 885. 求组合数 I

题目叙述:

输入格式

第一行包含整数 n。
接下来 n行,每行包含一组 a和 b。

输出格式

共n行,每行输出一个询问的解。

数据范围

1≤n≤10000,1≤b≤a≤2000

输入样例:

3
3 1
5 3
2 2

输出样例:

3
10
1

思路讲解:

看数据范围——a,b均在2000以内,就算挨个枚举a,b的值,也就四百万种情况,因此直接枚举——使用组合数的递推式直接枚举

本质上是杨辉三角

#include<iostream>
using namespace std;
const int N = 2005;
const int mod = 1e9 + 7;
int c[N][N];
//本质上杨辉三角
int main()
{
   for (int i = 0; i < N; i++)
   	for (int j = 0; j <= i; j++)
   		if (!j) c[i][j] = 1;
   		else c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod;
   int n; cin >> n;
   while (n--) {
   	int a, b;
   	cin >> a >> b;
   	cout << c[a][b] << endl;
   }
   return 0;
}
posted @ 2024-07-24 14:35  Tomorrowland_D  阅读(2)  评论(0编辑  收藏  举报