当前位置:网站首页>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;
}
边栏推荐
- zabbix如何配置告警短信?(预警短信通知设置流程)
- 【队列】933. Number of Recent Calls
- System.CommandLine版CSRebot
- 孙宇晨接受瑞士媒体Bilan采访:熊市不会持续太久
- Construction and beautification of personal blog
- Last day of the second quarter
- 【office办公-pdf篇】pdf合并与拆分让我们摆脱付费软件的功能限制好不好
- 图的连通性基础
- "Open math input panel" in MathType editing in win11 is gray and cannot be edited
- WIN11中MathType编辑中“打开数学输入面板”是灰色不可编辑
猜你喜欢
After working for 6 years, let's take stock of the golden rule of the workplace where workers mix up
Log logrus third party library usage
Understanding and application of Qt5 layout in creation
[Qt5 basics] random number display
3dsmax plug-in development traversal node object and object acquisition and inode transformation matrix description
[problem handled] -nvidia SMI command cannot obtain the GPU process number of its own container and the external GPU process number
物业怎么发短信通知给业主?
zabbix如何配置告警短信?(预警短信通知设置流程)
图的连通性基础
gin_gorm
随机推荐
Gin configuration file
农产品换房?“变相”购房补贴!
For the sustainable development of software testing, we must learn to knock code?
Laravel+redis generates an order number - automatically increase from 1 on the same day
【栈】921. Minimum Add to Make Parentheses Valid
【模拟】922. Sort Array By Parity II
Visual studio 2019 Download
【队列】933. Number of Recent Calls
C# 自定义并动态切换光标
Understanding and application of Qt5 layout in creation
45岁程序员告诉你:程序员为什么要跳槽,太真实...
Test essential tool - postman practical tutorial
二季度最后一天
Composants de la grille de données portatifs
Institute of Microbiology, commonly used biochemical reactions in microbiological testing
未来的 Web3会带来什么?
Handsontable data grid component
mysql插入\更新前+判断条件
What will Web3 bring in the future?
sort自定义函数