求组合数(笔记)

求组合数(笔记)_第1张图片

求组合数(笔记)_第2张图片 

 求组合数(笔记)_第3张图片

//组合数2,取值在1e5
//Cab = a! / (a - b)! * b!
#include
using namespace std;
using ll = long long;
const ll N = 1e4 + 9, mod = 1e9 + 7;
ll fact[N], infact[N];//阶乘,逆元阶乘

ll qmi(ll a, ll k, ll p)//逆元模板
{
	ll res = 1;

	while (k)
	{
		if (k & 1) res = res * a % p;
		a = a * a % p;
		k >>= 1;
	}
	return res;
}

int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	fact[0] = infact[0] = 1;
    //初始化数组
	for (ll i = 1; i < N; ++i)
	{
		fact[i] = fact[i - 1] * i % mod;
		infact[i] = infact[i - 1] * qmi(i, mod - 2, mod) % mod;
	}

	int n; cin >> n;

	while (n--)
	{
		int a, b; cin >> a >> b;
        //多取模的原因是防止超出long long的数据范围
		cout << fact[a] * infact[a - b] % mod * infact[b] % mod << '\n';
	}
	return 0;
}

 

 

你可能感兴趣的:(笔记,c++,算法)