当前位置:网站首页>求组合数 AcWing 886. 求组合数 II
求组合数 AcWing 886. 求组合数 II
2022-07-03 08:41:00 【T_Y_F666】
求组合数 AcWing 886. 求组合数 II
原题链接
算法标签
组合数学 组合计数 逆元 快速幂 费马小定理
思路
由于(a / b) % p 不等于(a % p) / (b % p),所以直接除以相应的阶乘不行, 需要乘阶乘逆元
代码
#include<bits/stdc++.h>
#define int long long
#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 = 2005, mod = 1e9+7;
int f[N], fn[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;
}
void init(){
f[0]=fn[0]=1;
rep(i, 1, N){
f[i]=f[i-1]*i%mod;
// 求逆元
fn[i]=fn[i-1]*qmi(i, mod-2, mod)%mod;
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read();
init();
while(n--){
int a=read(), b=read();
printf("%lld\n", f[a]*fn[b]%mod*fn[a-b]%mod);
}
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
- Markdown learning
- Explain sizeof, strlen, pointer, array and other combination questions in detail
- LinkedList set
- Installation of PHP FPM software +openresty cache construction
- 请求参数的发送和接收
- Allocation exception Servlet
- [concurrent programming] atomic operation CAS
- Osgearth topographic shading map drawing
- 【Rust 笔记】11-实用特型
猜你喜欢

MySQL three logs
![[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle](/img/b7/7bf2a4a9ab51364352aa5e0a196b6d.jpg)
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle

Redux - learning notes

单调栈-84. 柱状图中最大的矩形
![[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q](/img/76/6561a78b7f883a0e75a53e037153c3.jpg)
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q

Es8 async and await learning notes

First Servlet

JS non Boolean operation - learning notes

Annotations simplify configuration and loading at startup

Introduction to hexadecimal coding
随机推荐
Message pack in C deserializes array objects
Notes on understanding applets 2022/7/3
[rust notes] 12 closure
【Rust 笔记】08-枚举与模式
Redis cluster series 4
[concurrent programming] concurrent tool class of thread
Creation and content of mapnode -- osgearth rendering engine series (2)
Cesium for unreal quick start - simple scenario configuration
ES6 promise learning notes
Introduction to Base64 coding
Simple demo of solving BP neural network by gradient descent method
Concurrent programming (III) detailed explanation of synchronized keyword
[rust notes] 07 structure
Unity editor expansion - draw lines
Alibaba canaladmin deployment and canal cluster Ha Construction
Installation of PHP FPM software +openresty cache construction
UE4 source code reading_ Bone model and animation system_ Animation node
Collection interface
Mysql容器化(1)Docker安装MySQL
Allocation exception Servlet