当前位置:网站首页>Find the combination number acwing 886 Find the combination number II
Find the combination number acwing 886 Find the combination number II
2022-07-03 08:51:00 【T_ Y_ F666】
Find the combination number AcWing 886. Find the combination number II
Original link
AcWing 886. Find the combination number II
Algorithm tags
Combinatorial mathematics Combination count Inverse element Fast power Fermat's small Theorem
Ideas
because (a / b) % p It's not equal to (a % p) / (b % p), So directly dividing by the corresponding factorial doesn't work , Requires factorial inverse elements 
Code
#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;
// Inverse element
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;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support 
边栏推荐
- MySQL 8
- [RPC] RPC remote procedure call
- [concurrent programming] consistency hash
- Unity interactive water ripple post-treatment
- [concurrent programming] collaboration between threads
- 【Rust笔记】05-错误处理
- Monotonic stack -84 The largest rectangle in the histogram
- Markdown learning
- Format - C language project sub file
- Really explain the five data structures of redis
猜你喜欢

Deeply understand the underlying data structure of MySQL index

Format - C language project sub file

注解简化配置与启动时加载

Constraintlayout's constraintset dynamically modifies constraints

Redux - learning notes

求组合数 AcWing 885. 求组合数 I

Vscode, idea, VIM development tool shortcut keys

Notes and bugs generated during the use of h:i:s and y-m-d

Allocation exception Servlet

Visual Studio (VS) shortcut keys
随机推荐
Monotonic stack -84 The largest rectangle in the histogram
记忆化搜索 AcWing 901. 滑雪
Deep parsing (picture and text) JVM garbage collector (II)
Query XML documents with XPath
First Servlet
Sending and receiving of request parameters
20220630学习打卡
Unity Editor Extension - drag and drop
Gradle's method of dynamically modifying APK package name
【Rust 笔记】08-枚举与模式
<, < <,>, > > Introduction in shell
Unity learning notes
22-05-26 西安 面试题(01)准备
How to delete CSDN after sending a wrong blog? How to operate quickly
MySQL three logs
Really explain the five data structures of redis
URL backup 1
too many open files解决方案
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
cres