当前位置:网站首页>数学知识:求组合数 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;
}
边栏推荐
- Complete software development process
- 视频教程 | 长安链推出系列视频教程合集(入门)
- TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor. cpu() to copy the tensor to
- WIN11中MathType编辑中“打开数学输入面板”是灰色不可编辑
- Pre training / transfer learning of models
- After working for 6 years, let's take stock of the golden rule of the workplace where workers mix up
- 做生意更加务实
- MFC TCP communication server client demo notes vs2019
- [dynamic planning] path dp:931 Minimum Falling Path Sum
- C# 自定义并动态切换光标
猜你喜欢

【qt5-tab标签精讲】Tab标签及内容分层解析

Neo4j installation, operation, project construction and function realization

Working for eight years as a programmer, but with a salary of three years after graduation, it's too late to be enlightened again

After working for 6 years, let's take stock of the golden rule of the workplace where workers mix up

Qt5 mvc: revealing the secrets of data visualization

亲测有效,快速创建JMeter桌面快捷方式

What will Web3 bring in the future?

农产品换房?“变相”购房补贴!

gin_ gorm

Service grid ASM year end summary: how do end users use the service grid?
随机推荐
Strictmode analysis activity leakage -strictmode principle (3)
Typora的使用
Exploration and practice of "flow batch integration" in JD
"Open math input panel" in MathType editing in win11 is gray and cannot be edited
For the sustainable development of software testing, we must learn to knock code?
TypeError: Argument ‘angle‘ can not be treated as a double
Microbial safety and health, what is biological treatment?
3dsmax插件开发遍历节点对象和Object获取及INode变换矩阵说明
Visual studio 2019 shortcut notes
二季度最后一天
未来的 Web3会带来什么?
45岁程序员告诉你:程序员为什么要跳槽,太真实...
7-2 拼题A打卡奖励 dp
dc_ Study and summary of labs--lab1
PHP crawls data through third-party plug-ins
Open3d point cloud color rendering
软件测试的可持续发展,必须要学会敲代码?
qt5-MVC:数据可视化的层次揭秘
What are the functions of soil microorganisms in microbial detection?
gin 配置文件