-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinatorics.cpp
More file actions
40 lines (35 loc) · 1.07 KB
/
Copy pathCombinatorics.cpp
File metadata and controls
40 lines (35 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "bits/stdc++.h"
using namespace std;
typedef long long int ll;
const long long mod=1e9+7;
long long binpow(long long a, long long b, long long m) {
a %= m;
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
void factorial(vector<long long> &fact, long long n){
fact.resize(n + 1, 1);
fact[0] = 1;
fact[1] = 1;
for (int i = 2; i <= n ; ++i){
fact[i] = ((fact[i - 1] % mod) *1LL* (i % mod)) % mod;
}
}
long long mod_inv(long long a) { return (binpow(a, mod - 2, mod)) % mod; }
long long ncr(long long n, long long r, vector<long long> &fact){
if (r > n or n < 0 or r < 0) return 0LL;
return (((fact[n] % mod) * (mod_inv(fact[n - r]) % mod)) % mod * (mod_inv(fact[r]) % mod)) % mod;
}
void modInv(vector<long long> &inv, long long n, vector<long long> &fact){
inv.resize(n + 1, 1);
inv[n] = mod_inv(fact[n]);
for (int i = n - 1; i >= 0 ; --i){
inv[i] = ((inv[i + 1]) % mod * ((i + 1) % mod)) % mod;
}
}