当前位置:网站首页>求组合数 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));
}
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 927. 三等分 模拟
- MIT-6874-Deep Learning in the Life Sciences Week 7
- Overview of variable resistors - structure, operation and different applications
- MIT-6874-Deep Learning in the Life Sciences Week 7
- 背包问题 AcWing 9. 分组背包问题
- 实时时钟 (RTC)
- SQLMAP使用教程(二)实战技巧一
- SQLMAP使用教程(一)
- Data visualization chart summary (II)
- 做 SQL 性能优化真是让人干瞪眼
猜你喜欢

实时时钟 (RTC)

中国剩余定理 AcWing 204. 表达整数的奇怪方式

Navicat连接Oracle数据库报错ORA-28547或ORA-03135

Appium基础 — 使用Appium的第一个Demo

Leetcode-6110: number of incremental paths in the grid graph

Single chip computer engineering experience - layered idea

MySQL advanced part 1: View

博弈论 AcWing 891. Nim游戏

开源存储这么香,为何我们还要坚持自研?

Doing SQL performance optimization is really eye-catching
随机推荐
Appium自动化测试基础 — Appium测试环境搭建总结
Leetcode-3: Longest substring without repeated characters
Leetcode-1200: minimum absolute difference
Leetcode-9: palindromes
Flutter Web 硬件键盘监听
MySQL advanced part 1: View
Golang uses context gracefully
[leetcode] day94 reshape matrix
Matrixdb V4.5.0 was launched with a new mars2 storage engine!
阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
4. 对象映射 - Mapping.Mapster
11-gorm-v2-03-basic query
博弈论 AcWing 894. 拆分-Nim游戏
Leetcode stack related
Sum of three terms (construction)
1.15 - input and output system
Open source storage is so popular, why do we insist on self-development?
Traversal of leetcode tree
LVS简介【暂未完成(半成品)】
实时时钟 (RTC)