当前位置:网站首页>数学知识:求组合数 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;
}
边栏推荐
- Microbiological health, why is food microbiological testing important
- Digital IC design process summary
- 二季度最后一天
- 微研所,微生物检验中常用的生化反应
- 微生物安全与健康,什么是生物处理?
- Using recyclerreview to show banner is very simple
- 元宇宙为 VR/AR 带来的新机会
- Draw some interesting figures with flutter's canvas
- Thinking brought by strictmode -strictmode principle (5)
- Why build a personal blog
猜你喜欢

New opportunities for vr/ar brought by metauniverse

6月第4周榜单丨飞瓜数据UP主成长排行榜(哔哩哔哩平台)发布!

3500字归纳总结:一名合格的软件测试工程师需要掌握的技能大全

1500w播放下还藏着什么热点?B站2个未来趋势你不得错过

Creating ASCII art with C #

医疗HIS行业短信发送解决方案

小程序中实现excel数据的批量导入

What will Web3 bring in the future?

Exploration and practice of "flow batch integration" in JD

关于白盒测试,这些技巧你得游刃有余~
随机推荐
visual studio 2019 快捷键备忘
PHP array splicing MySQL in statement
Relationship between ASCII, Unicode, GBK, UTF-8
用 Flutter 的 Canvas 画点有趣的图形
Log logrus third party library usage
Last day of the second quarter
After working for 6 years, let's take stock of the golden rule of the workplace where workers mix up
Connectivity basis of Graphs
PHP数组拼接MySQL的in语句
Use strictmode strictmode principle (1)
PHP通过第三方插件爬取数据
mysql插入\更新前+判断条件
直播商城源码,实现左右联动商品分类页面
Visual studio 2019 shortcut notes
Compile and install oh my Zsh
What will Web3 bring in the future?
Applet Custom Grid
mysql数据库基础:流程控制
日志 logrus第三方库的使用
For the sustainable development of software testing, we must learn to knock code?