当前位置:网站首页>数学知识:求组合数 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;
}
边栏推荐
- Selenium 凭什么成为 Web 自动化测试的首选?(内附源码)
- CompletableFuture的入门
- apache atlas断点查看
- Cv5200 ad hoc network remote WiFi module, UAV wireless image transmission application, HD low delay scheme
- How do I create a contact form in WordPress?
- GNN笔记:消息传播模型
- Notice on organizing the declaration of Nanjing innovative products (the first batch) in 2022
- [try to hack] upload labs (temporarily write to 12)
- Interviewer: tell me about the MySQL transaction isolation level?
- [proteus simulation] 8-bit nixie tube dynamic scanning display change data
猜你喜欢

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

面试官:说一下MySQL事务隔离级别?

隐私计算助力数据的安全流通与共享

BS-GX-017基于SSM实现的在线考试管理系统

Summary of common MySQL statements and commands

MySQL常用语句和命令汇总

BOE: with the arrival of the peak season in the second half of the year, the promotion and the release of new products, the demand is expected to improve

技术分享| 融合调度中的广播功能设计
![leetcode:232. Realize queue with stack [two stacks, one auxiliary and one simulated queue]](/img/be/844772e761c0ea6002c25483be93c0.png)
leetcode:232. Realize queue with stack [two stacks, one auxiliary and one simulated queue]

Top the list for 10 consecutive years? What is the "most common" programming language for programmers?
随机推荐
Tool chain empowers hundreds of companies, horizon opens the "Matthew effect" of mass production of intelligent driving
The understanding of industrial Internet is directly related to how we view it
MATLAB输出格式控制 %d,%f,%c,%s的用法
Key wizard play monster learning - multi window and multi thread background judgment of character, pet blood volume and pet happiness
破解湖+仓混合架构顽疾,星环科技推出自主可控云原生湖仓一体平台
The third sprint of Wei long La Tiao: the growth rate of performance declined, and Liu Weiping and Liu Fuping cashed out in advance
UWB精准定位方案,厘米级高精度技术应用,智能配对感应技术
Time format GTM to Beijing time
水球图-利用动态波纹展示百分比
Sophon kg upgrade 3.1: break down barriers between data and liberate enterprise productivity
【第28天】给定一个字符串S,请你判断它是否为回文字符串 | 回文的判断
Metadata management Apache Atlas Compilation integration deployment and testing
CV5200自组网远程WiFi模组,无人机无线图传应用,高清低时延方案
基础 | 在物理引擎中画圆弧
真正的测试 =“半个产品+半个开发”?
如何在 WordPress 中嵌入 iFrame
蓝桥杯2015年CA省赛(填坑中)
The rooster Electronic Society graphical programming real questions and answers analysis of the scratch grade test level 1 June 2022
Key sprite fighting monsters - window binding protection skills and click skills
apache atlas断点查看