当前位置:网站首页>求组合数 AcWing 887. 求组合数 III
求组合数 AcWing 887. 求组合数 III
2022-07-05 06:16:00 【T_Y_F666】
求组合数 AcWing 887. 求组合数 III
原题链接
算法标签
组合数学 组合计数 Lucas定理 逆元 快速幂 费马小定理
思路
代码
#include<bits/stdc++.h>
#define int long long
#define abs fabs
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>=b;--i)
using namespace std;
const int N = 105;
double a[N][N], eps = 1e-8;
int n;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
int qmi(int a, int b, int p){
int ans=1;
while(b){
if(b&1){
ans=ans*a%p;
}
a=a*a%p;
b>>=1;
}
return ans;
}
int C(int a, int b, int p){
if(a<b){
return 0;
}else{
int res=1;
// for循环只执行了b次,所以是从a开始乘了b个数,即a*(a-1)*…*(a-b+1),也就等于a!/(a-b)!
for(int i=1, j=a; i<=b; ++i, --j){
res=res*j%p;
res=res*qmi(i, p-2, p)%p;
}
return res;
}
}
int lu(int a, int b, int p){
if(a<p&&b<p){
return C(a, b, p);
}else{
return C(a%p, b%p, p)*lu(a/p, b/p, p)%p;
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
n=read();
while(n--){
int a=read(), b=read(), p=read();
printf("%lld\n", lu(a, b, p));
}
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Winter vacation water test 1 Summary
- SQLMAP使用教程(二)实战技巧一
- 中国剩余定理 AcWing 204. 表达整数的奇怪方式
- MySQL advanced part 2: storage engine
- Leetcode-31: next spread
- CPU内核和逻辑处理器的区别
- New title of module a of "PanYun Cup" secondary vocational network security skills competition
- Appium automation test foundation - Summary of appium test environment construction
- Matrixdb V4.5.0 was launched with a new mars2 storage engine!
- leetcode-6109:知道秘密的人数
猜你喜欢
Is it impossible for lamda to wake up?
Introduction to LVS [unfinished (semi-finished products)]
容斥原理 AcWing 890. 能被整除的数
SPI 详解
数据可视化图表总结(一)
[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
Appium基础 — 使用Appium的第一个Demo
LVS简介【暂未完成(半成品)】
leetcode-6110:网格图中递增路径的数目
MySQL advanced part 2: optimizing SQL steps
随机推荐
JS quickly converts JSON data into URL parameters
What's wrong with this paragraph that doesn't work? (unresolved)
How to understand the definition of sequence limit?
LeetCode-61
4. Object mapping Mapster
【LeetCode】Easy | 20. Valid parentheses
MySQL advanced part 2: the use of indexes
[rust notes] 15 string and text (Part 1)
1039 Course List for Student
redis发布订阅命令行实现
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
Leetcode-1200: minimum absolute difference
对for(var i = 0;i < 5;i++) {setTimeout(() => console.log(i),1000)}的深入分析
[rust notes] 17 concurrent (Part 1)
实时时钟 (RTC)
【Rust 笔记】15-字符串与文本(上)
QQ computer version cancels escape character input expression
Spark中groupByKey() 和 reduceByKey() 和combineByKey()
Presentation of attribute value of an item
Filter the numbers and pick out even numbers from several numbers