求组合数(笔记)

cpp 复制代码
//组合数2,取值在1e5
//Cab = a! / (a - b)! * b!
#include<iostream>
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;
}
相关推荐
萌动的小火苗16 小时前
嵌入式开发中的栈与队列:任务调度为什么依赖数据结构
数据结构·c++·单片机·嵌入式硬件
叩码以求索16 小时前
统计按位或能得到最大值的子集数目(一)
数据结构·算法
tachibana217 小时前
hot100 数组中的第K个最大元素(215)
java·数据结构·算法·leetcode
txzrxz17 小时前
单调队列讲解
数据结构·c++·算法·单调队列
WWTYYDS_66617 小时前
JsonCpp超详细使用教程
c++
不会就选b18 小时前
算法日常・每日刷题--<快排>4
算法
Keven_1118 小时前
算法札记:树状数组的用途
数据结构·算法
Joey_friends18 小时前
指纹authenticate流程图
android·java·c++
用户6774371758118 小时前
C++函数参数传递方式详解:string、string&、const string、const string&该怎么选?
算法
小小晓.18 小时前
C++小白记:C风格字符串和数组用法
c语言·开发语言·c++