求组合数(笔记)

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;
}
相关推荐
星恒随风12 小时前
从0实现C++ AI大模型接入SDK(三):环境搭建与ChatSDK快速上手——实现我们的第一个AI聊天程序
linux·开发语言·c++·人工智能·笔记
风落青芜12 小时前
排序算法(二):选择排序、堆排序与冒泡排序
c语言·开发语言·数据结构·算法·排序算法
Navigator_Z13 小时前
LeetCode //C - 1319. Number of Operations to Make Network Connected
c语言·算法·leetcode
h_a_o777oah13 小时前
【数据结构】FHQ Treap:无旋转的平衡 BST
数据结构·c++·算法·acm·平衡树·treap·fhq treap
撸码猿13 小时前
Ladybird 进入 alpha:从零写的浏览器引擎现在能下载了
c++·rust·开源·浏览器·ladybird
草履虫AI课14 小时前
强化学习从零到一 07|重要性采样:借别人的数据,算自己的期望
算法·强化学习
AAA修煤气灶刘哥14 小时前
明明输入了完整报错,知识库怎么净给我相似答案?
人工智能·后端·算法
ShineWinsu15 小时前
对于Qt:重写事件步骤的解析
开发语言·c++·qt·客户端·事件·鼠标事件·步骤
凌冰_16 小时前
数据结构 - 渐进符号和递归的复杂度(二)
数据结构·算法·软件工程
爱和冰阔落16 小时前
【Linux网络】HTTP 服务器怎么返回网页和图片?从 404、301 到 Content-Type 与静态资源完整实战
linux·c++·http·网络编程·web服务器