当前位置:网站首页>数学知识:求组合数 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;
}
边栏推荐
- 做生意更加务实
- Microbial safety and health, what is biological treatment?
- dc_ Study and summary of labs--lab1
- Visual studio 2019 shortcut notes
- 数据探索电商平台用户行为流失分析
- Mysql database foundation: process control
- C# 自定义并动态切换光标
- 流批一体在京东的探索与实践
- 3500 word summary: a complete set of skills that a qualified software testing engineer needs to master
- For the sustainable development of software testing, we must learn to knock code?
猜你喜欢
医疗HIS行业短信发送解决方案
微生物健康,食品微生物检测为什么很重要
Compile and install oh my Zsh
The argument type 'function' can't be assigned to the parameter type 'void function()‘
KS009基于SSH实现宠物管理系统
Working for eight years as a programmer, but with a salary of three years after graduation, it's too late to be enlightened again
Test essential tool - postman practical tutorial
孙宇晨接受瑞士媒体Bilan采访:熊市不会持续太久
Exploration and practice of "flow batch integration" in JD
45 year old programmer tells you: why do programmers want to change jobs? It's too true
随机推荐
45 year old programmer tells you: why do programmers want to change jobs? It's too true
dc_ Study and summary of labs--lab1
医疗HIS行业短信发送解决方案
laravel 事件 & 监听
C # customize and dynamically switch cursor
[deepin] common sets
System.CommandLine版CSRebot
1500w播放下还藏着什么热点?B站2个未来趋势你不得错过
TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to
More pragmatic in business
mysql数据库基础:流程控制
Construction and beautification of personal blog
KS009基于SSH实现宠物管理系统
Exploration and practice of "flow batch integration" in JD
微生物检测,土壤微生物的作用有哪些?
Sun Yuchen told Swiss media Bilan that the bear market will not last long
做生意更加务实
qt5-MVC:数据可视化的层次揭秘
MFC TCP communication server client demo notes vs2019
亲测有效,快速创建JMeter桌面快捷方式