当前位置:网站首页>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;
}
边栏推荐
- System.CommandLine版CSRebot
- Last day of the second quarter
- Uniapp official component clicking item is invalid, solution
- gin_gorm
- The personal test is effective, and the JMeter desktop shortcut is quickly created
- 测试必备工具-Postman实战教程
- Connectivity basis of Graphs
- 45岁程序员告诉你:程序员为什么要跳槽,太真实...
- [simulation] 922 Sort Array By Parity II
- Thinking brought by strictmode -strictmode principle (5)
猜你喜欢

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

gin_ gorm

Compile and install oh my Zsh

Exploration and practice of "flow batch integration" in JD

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

一站式洞察行业热点,飞瓜数据B站新功能「流量大盘」上线!

Neo4j installation, operation, project construction and function realization

Using recyclerreview to show banner is very simple

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

PHP crawls data through third-party plug-ins
随机推荐
Working for eight years as a programmer, but with a salary of three years after graduation, it's too late to be enlightened again
"Open math input panel" in MathType editing in win11 is gray and cannot be edited
Exploration and practice of "flow batch integration" in JD
The argument type 'function' can't be assigned to the parameter type 'void function()‘
Neo4j installation, operation, project construction and function realization
[simulation] 922 Sort Array By Parity II
Compile and install oh my Zsh
45 year old programmer tells you: why do programmers want to change jobs? It's too true
面对产业互联网的时候,甚至还用消费互联网的方式和方法去落地和实践产业互联网
laravel Carbon 时间处理类使用
[Office PDF] PDF merging and splitting will free us from the functional limitations of paid software, OK
Qt5 mvc: revealing the secrets of data visualization
Visual studio 2019 Download
gin_ gorm
【队列】933. Number of Recent Calls
工作八年的程序员,却拿着毕业三年的工资,再不开窍就真晚了...
Handsontable數據網格組件
WIN11中MathType编辑中“打开数学输入面板”是灰色不可编辑
DC學習筆記正式篇之零——綜述與基本流程介紹
More pragmatic in business