当前位置:网站首页>数学知识:求组合数 III—求组合数
数学知识:求组合数 III—求组合数
2022-07-01 01:02:00 【奋斗吧!骚年!】
题目:AcWing 887. 求组合数 III
给定 n 组询问,每组询问给定三个整数 a,b,p,其中 p 是质数,请你输出 Cbamod p 的值。
输入格式
第一行包含整数 n。
接下来 n 行,每行包含一组 a,b,p。
输出格式
共 n 行,每行输出一个询问的解。
数据范围
1≤n≤20,
1≤b≤a≤1018,
1≤p≤105,
输入样例:
3
5 3 7
3 1 5
6 4 13
输出样例:
3
3
2
题目分析:
从上面的公式可以知道组合数可以分解为数更小的组合数,我们可以使用快速幂求出逆元,来求的其组合数
#include <iostream>
using namespace std;
typedef long long LL;
int p;
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 C(int a,int b,int p) // 求组合数
{
if(b>a)return 0;
int res=1;
for(int i=1,j=a;i<=b;i++,j--)
{
res=(LL)res*j%p;
res=(LL)res*qmi(i,p-2,p)%p;
}
return res;
}
int lucas(LL a,LL b,int p) // 罗卡斯定理
{
if(a<p&&b<p)return C(a,b,p);
else return (LL)C(a%p,b%p,p)*lucas(a/p,b/p,p)%p;
}
int main()
{
int n;
cin>>n;
while(n--)
{
LL a,b;
cin>>a>>b>>p;
cout<<lucas(a,b,p)<<endl;
}
return 0;
}
边栏推荐
- Working for eight years as a programmer, but with a salary of three years after graduation, it's too late to be enlightened again
- Typora的使用
- "Open math input panel" in MathType editing in win11 is gray and cannot be edited
- Using recyclerreview to show banner is very simple
- 工作6年,来盘点一下职场人混迹职场的黄金法则
- Connectivity basis of Graphs
- TypeError: Argument ‘angle‘ can not be treated as a double
- 【栈】921. Minimum Add to Make Parentheses Valid
- [stack] 921 Minimum Add to Make Parentheses Valid
- System settings large page
猜你喜欢
随机推荐
【栈】921. Minimum Add to Make Parentheses Valid
Unknown database connection database error
工作6年,来盘点一下职场人混迹职场的黄金法则
PHP converts two-dimensional array elements into key value pairs
visual studio 2019 下载
Working for eight years as a programmer, but with a salary of three years after graduation, it's too late to be enlightened again
laravel 事件 & 监听
Why build a personal blog
The personal test is effective, and the JMeter desktop shortcut is quickly created
Try new possibilities
Service grid ASM year end summary: how do end users use the service grid?
zabbix如何配置告警短信?(预警短信通知设置流程)
股票开户有哪些优惠活动?另外,手机开户安全么?
Use strictmode strictmode principle (1)
一些本质的区别
visual studio 2019 快捷键备忘
Some essential differences
未来的 Web3会带来什么?
Note d'étude du DC: zéro dans le chapitre officiel - - Aperçu et introduction du processus de base
System settings large page

![[Qt5 basics] random number display](/img/1f/a3d310788dbc45c71d3b5c47d50a5b.png)






