当前位置:网站首页>数学知识:求组合数 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;
}
边栏推荐
- 工作6年,来盘点一下职场人混迹职场的黄金法则
- [Qt5 basic \u 1] starting from 0, Mr. Detian will study with you - Introduction to the window
- System settings large page
- Strictmode jamming and leakage detection -strictmode principle (2)
- 测试必备工具-Postman实战教程
- One of the basics - overview of sta Basics
- WIN11中MathType编辑中“打开数学输入面板”是灰色不可编辑
- 一站式洞察行业热点,飞瓜数据B站新功能「流量大盘」上线!
- Draw some interesting figures with flutter's canvas
- MFC TCP通信服务端客户端Demo备忘vs2019
猜你喜欢

元宇宙为 VR/AR 带来的新机会

数字IC设计流程总结

测试必备工具-Postman实战教程

Connectivity basis of Graphs

远程办公如何保持高效协同,实现项目稳定增长 |社区征文

Using recyclerreview to show banner is very simple

With regard to the white box test, you have to master these skills~

Digital IC design process summary

gin_gorm

The personal test is effective, and the JMeter desktop shortcut is quickly created
随机推荐
Necessary tools for testing - postman practical tutorial
Thinking brought by strictmode -strictmode principle (5)
Open3d point cloud bounding box
What are the functions of soil microorganisms in microbial detection?
Microbial safety and health, what is biological treatment?
元宇宙为 VR/AR 带来的新机会
Draw some interesting figures with flutter's canvas
The personal test is effective, and the JMeter desktop shortcut is quickly created
Handsontable data grid component
Selenium经典面试题-多窗口切换解决方案
Compile and install oh my Zsh
微生物检测,土壤微生物的作用有哪些?
DC學習筆記正式篇之零——綜述與基本流程介紹
Complete software development process
数字IC设计流程总结
微生物安全與健康,什麼是生物處理?
亲测有效,快速创建JMeter桌面快捷方式
微生物安全与健康,什么是生物处理?
Test essential tool - postman practical tutorial
Digital IC design process summary