当前位置:网站首页>求组合数 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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Unity notes 1
- 基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
- URL backup 1
- createjs easeljs
- I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
- [rust notes] 02 ownership
- Osgearth topographic shading map drawing
- Dom4j traverses and updates XML
- [concurrent programming] concurrent security
- Unity interactive water ripple post-treatment
猜你喜欢

JS ternary operator - learning notes (with cases)

Simple demo of solving BP neural network by gradient descent method

Unity editor expansion - scrolling list

Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial

Kunlunbase meetup is waiting for you!

Es8 async and await learning notes

Message queue for interprocess communication

UE4 source code reading_ Mobile synchronization
![[redis] redis persistent RDB vs AOF (source code)](/img/57/b6a86c49cedee31fc00dc5d1372023.jpg)
[redis] redis persistent RDB vs AOF (source code)
![[RPC] RPC remote procedure call](/img/dc/872204ea47fcff04cdb72e18a2a4ef.jpg)
[RPC] RPC remote procedure call
随机推荐
How to delete CSDN after sending a wrong blog? How to operate quickly
Cesium for unreal quick start - simple scenario configuration
MySQL index types B-tree and hash
【Rust 笔记】13-迭代器(上)
[linear table] basic operation of bidirectional linked list specify node exchange
Osgearth target selection
How does unity fixedupdate call at a fixed frame rate
Really explain the five data structures of redis
C language student management system based on linked list, super detailed
Deep parsing JVM memory model
Animation_ IK overview
使用dlv分析golang进程cpu占用高问题
Mortgage Calculator
Allocation exception Servlet
MySQL 8
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
Get the link behind? Parameter value after question mark
【Rust 笔记】09-特型与泛型
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
22-05-26 西安 面试题(01)准备