当前位置:网站首页>数学知识:求组合数 II—求组合数
数学知识:求组合数 II—求组合数
2022-06-29 16:08:00 【奋斗吧!骚年!】
题目:AcWing 886. 求组合数 II
给定 n 组询问,每组询问给定两个整数 a,b,请你输出 Cbamod(109+7) 的值。
输入格式
第一行包含整数 n。
接下来 n 行,每行包含一组 a 和 b。
输出格式
共 n 行,每行输出一个询问的解。
数据范围
1≤n≤10000,
1≤b≤a≤105
输入样例:
3
3 1
5 3
2 2
输出样例:
3
10
1
题目分析:
该题是预处理阶乘
因为组合可以换成阶乘的形式
但是除法应该换成逆元,所以需要用到快速幂的知识求逆元。
最后fact[i]表示i的阶乘,infact[i]表示i的逆元
Cab=fact[a] * infact[a-b] * infact[b]
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 100010,mod=1e9+7;
int fact[N],infact[N];
int qmi(int a, int k, int p)
{
int res = 1;
while(k)
{
if(k&1)res=(LL)res*a%p;
a=(LL)a*a%p;
k>>=1;
}
return res;
}
int main()
{
fact[0]=infact[0]=1;
for(int i=1;i<N;i++) // 预处理
{
fact[i]=(LL)fact[i-1]*i%mod; // 阶乘
infact[i]=(LL)infact[i-1]*qmi(i,mod-2,mod)%mod; // 阶乘的逆元
}
int n;
cin>>n;
while(n--)
{
int a,b;
cin>>a>>b;
cout<<(LL)fact[a]*infact[b]%mod*infact[a-b]%mod<<endl;
}
return 0;
}
边栏推荐
- Mysql database Basics: introduction to data types
- BS-GX-018 基于SSM实现在校学生考试系统
- After studying this series of notes about software testing, it is a "bonus" to enter the factory
- 『计组』CPU 如何区分指令和数据
- Notice on organizing the declaration of Nanjing innovative products (the first batch) in 2022
- Differences between virtual hosts, WordPress hosts and virtual hosts
- CompletableFuture的入门
- Sophon base 3.1 launches mlops function to provide wings for enterprise AI capability operation
- Sophon KG升级3.1:打破数据间壁垒,解放企业生产力
- 【Try to Hack】XML
猜你喜欢

DAP large screen theme development description

南京大学:新时代数字化人才培养方案探讨

Cv5200 ad hoc network remote WiFi module, UAV wireless image transmission application, HD low delay scheme

Selenium 凭什么成为 Web 自动化测试的首选?(内附源码)

The latest agenda of dtcc2022 China database technology conference was released

加速智能驾驶项目落地?你还缺一套真值测评系统

破解湖+仓混合架构顽疾,星环科技推出自主可控云原生湖仓一体平台

毕业生迷茫,中年人焦虑,职场路怎么越走越宽?

面试官:说一下MySQL事务隔离级别?
![leetcode:42. Rain water [double hands are elegant]](/img/8c/41f4a9c7176ff47327e79b688e2c55.png)
leetcode:42. Rain water [double hands are elegant]
随机推荐
加速智能驾驶项目落地?你还缺一套真值测评系统
leetcode:139. Word splitting [DFS + memory]
DAP large screen theme development description
时间格式化 GTM转北京时间
Locust performance pressure test tool
如何在 WordPress 中创建联系表格?
事件相关电位ERP的皮层溯源分析
我想网上注册股票开户,如何操作?另外,手机开户安全么?
挖财学堂证券开户安全嘛?
Sophon autocv: help AI industrial production and realize visual intelligent perception
Which version of JVM is the fastest?
Selenium 凭什么成为 Web 自动化测试的首选?(内附源码)
Science: the interrelated causes and consequences of sleep in the brain
实践 | 脚本错误量极致优化-让脚本错误一目了然
CV5200自组网远程WiFi模组,无人机无线图传应用,高清低时延方案
After 3 years of testing experience, do you know the state transition method for use case design?
Tutorial | fNIRS data processing toolkit homer2 download and installation
技术分享| 融合调度中的广播功能设计
locust性能压测工具
Apache atlas breakpoint view