当前位置:网站首页>求组合数 AcWing 886. 求组合数 II
求组合数 AcWing 886. 求组合数 II
2022-07-27 10:35: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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 泰山OFFICE技术讲座:缩放比例与打开文件
- The difference of iteration number and information entropy
- 数字三角形模型 AcWing 275. 传纸条
- Introduction to software vulnerability analysis (I)
- Knapsack model acwing 1024. Packing problem
- The longest ascending subsequence model acwing 1017. The glider wing of the strange thief Kidd
- Shock simulation of engine mounting system transient modal dynamic analysis and response spectrum analysis
- 栈 AcWing 3302. 表达式求值
- 基于FPGA的ECG信号采集,存储以及传输系统verilog实现
- Backpack model acwing 1022. Collection of pet elves
猜你喜欢

XXX packages are looking for funding run 'NPM fund' for details solutions

博弈论 AcWing 892. 台阶-Nim游戏

迭代次数的差异与信息熵

Kangaroo cloud stack based on CBO in spark SQL optimization

迭代次数和熵之间关系的一个验证试验

Longest ascending subsequence model acwing 1010. Interceptor missile

Wechat push - template message parameter configuration

Redis+caffeine two-level cache enables smooth access speed

Sort th in antd table to prevent hovering color change +table hovering row color change +table header color change

c语言指针函数和函数指针的辨析
随机推荐
Students, don't copy all my code, remember to change it, or we both want G
Thank you for your likes and attention
The open source project Taier version 1.1 was officially released, and the list of new functions is fast
洛谷P1441 砝码称重
Cancer DDD
高斯消元 AcWing 884. 高斯消元解异或线性方程组
IMG SRC is empty or SRC does not exist, and the picture has a white border
Ten year structure five year life-07 young and vigorous transformation
Instructions for mock platform
推导重叠积分的详细展开式 STO overlap integrals
Antd table+checkbox default value display
Data assets are king. How to analyze the relationship between enterprise digital transformation and data asset management?
What changes will metauniverse bring to the music industry in the trillion market?
BeautifulSoup的使用
区间问题 AcWing 906. 区间分组
How to build a real-time development platform to deeply release the value of enterprise real-time data?
Chengying, kangaroo cloud one-stop fully automated operation and maintenance steward, is officially open source
properties文件
Description and feelings
Neural network learning notes