当前位置:网站首页>求组合数 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));
}
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- LeetCode 1200. Minimum absolute difference
- 阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
- Leetcode recursion
- Leetcode-556: the next larger element III
- 高斯消元 AcWing 884. 高斯消元解异或线性方程组
- 4. Object mapping Mapster
- 927. Trisection simulation
- LeetCode 0107. Sequence traversal of binary tree II - another method
- Règlement sur la sécurité des réseaux dans les écoles professionnelles secondaires du concours de compétences des écoles professionnelles de la province de Guizhou en 2022
- MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
猜你喜欢
QQ computer version cancels escape character input expression
Single chip computer engineering experience - layered idea
Appium自动化测试基础 — Appium测试环境搭建总结
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
Quickly use Amazon memorydb and build your own redis memory database
[2020]GRAF: Generative Radiance Fields for 3D-Aware Image Synthesis
Data visualization chart summary (II)
MySQL advanced part 1: stored procedures and functions
阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields
随机推荐
Liunx starts redis
Doing SQL performance optimization is really eye-catching
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
MIT-6874-Deep Learning in the Life Sciences Week 7
阿里巴巴成立企业数智服务公司“瓴羊”,聚焦企业数字化增长
MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
__ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
传统数据库逐渐“难适应”,云原生数据库脱颖而出
多屏电脑截屏会把多屏连着截下来,而不是只截当前屏
MySQL advanced part 2: optimizing SQL steps
Chapter 6 relational database theory
【Rust 笔记】16-输入与输出(上)
【Rust 笔记】17-并发(下)
【Rust 笔记】15-字符串与文本(上)
1041 Be Unique
C job interview - casting and comparing - C job interview - casting and comparing
Record the process of configuring nccl and horovod in these two days (original)
Filter the numbers and pick out even numbers from several numbers
How to generate an image from text on fly at runtime
4. Object mapping Mapster