当前位置:网站首页>Mathematical knowledge: finding combinatorial number III - finding combinatorial number
Mathematical knowledge: finding combinatorial number III - finding combinatorial number
2022-07-01 01:39:00 【Fight! Sao Nian!】
subject :AcWing 887. Find the combination number III
Given n Group inquiry , Each group is asked to give three integers a,b,p, among p Prime number , Please output Cbamod p Value .
Input format
The first line contains integers n.
Next n That's ok , Each line contains a set of a,b,p.
Output format
common n That's ok , Each line outputs a solution to the query .
Data range
1≤n≤20,
1≤b≤a≤1018,
1≤p≤105,
sample input :
3
5 3 7
3 1 5
6 4 13
sample output :
3
3
2
Topic analysis :
From the above formula, we can know that the combinatorial number can be decomposed into smaller combinatorial numbers , We can use the fast power to find the inverse , To find the number of combinations
#include <iostream>
using namespace std;
typedef long long LL;
int p;
int qmi(int a,int k,int p) // Find fast power
{
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) // Find the combination number
{
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) // Lucas theorem
{
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;
}
边栏推荐
- Handsontable数据网格组件
- Some essential differences
- 面对产业互联网的时候,甚至还用消费互联网的方式和方法去落地和实践产业互联网
- Use strictmode strictmode principle (1)
- Necessary tools for testing - postman practical tutorial
- qt5-MVC:数据可视化的层次揭秘
- gin 配置文件
- 1500w播放下还藏着什么热点?B站2个未来趋势你不得错过
- 图灵奖得主LeCun指明AI未来的出路在于自主学习,这家公司已踏上征途
- Microbiological health, why is food microbiological testing important
猜你喜欢

微研所,微生物检验中常用的生化反应

For the sustainable development of software testing, we must learn to knock code?

Complete software development process

Construction and beautification of personal blog

数字IC设计流程总结

未来的 Web3会带来什么?

Unknown database connection database error

QT5-布局在创作中的理解应用

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

Unknown database连接数据库错误
随机推荐
The personal test is effective, and the JMeter desktop shortcut is quickly created
Exploration and practice of "flow batch integration" in JD
微生物安全与健康,什么是生物处理?
[Office PDF] PDF merging and splitting will free us from the functional limitations of paid software, OK
visual studio 2019 快捷键备忘
尝试新的可能
短信在企业中的应用有哪些?
Understanding and application of Qt5 layout in creation
One of the basics - overview of sta Basics
Draw some interesting figures with flutter's canvas
直播商城源码,实现左右联动商品分类页面
TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor. cpu() to copy the tensor to
Connectivity basis of Graphs
[dynamic planning] path dp:931 Minimum Falling Path Sum
Laravel+redis generates an order number - automatically increase from 1 on the same day
flutter报错 -- The argument type ‘Function‘ can‘t be assigned to the parameter type ‘void Function()?‘
Sort custom function
医疗HIS行业短信发送解决方案
数字IC设计流程总结
Gin configuration file