forked from sahilbansal17/Competitive_Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnCrModuloP.cpp
More file actions
61 lines (53 loc) · 1.43 KB
/
nCrModuloP.cpp
File metadata and controls
61 lines (53 loc) · 1.43 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// program to compute nCr modulo a prime no. p, greater than n
#include <bits/stdc++.h>
using namespace std;
typedef vector <int> vi;
typedef pair <int,int> pii;
typedef long long ll;
typedef unsigned long long ull;
#define fl(i,a,b) for(int i(a);i<(b);i++)
#define rep(i,n) fl(i,0,n)
#define rfl(i,a,b) for(int i(a);i>=(b);i--)
#define srt(v) sort((v).begin(),(v).end())
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define slen(s) s.length()
#define F first
#define S second
/* Iterative Function to calculate (x^y)%p in O(log y) */
ll power(ll x, ll y, ll p){
ll res = 1;
x = x % p;
while (y > 0){
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p (used Fermat's little theorem)
ll modInverse(ll n, ll p){
return power(n, p-2, p);
}
// Returns nCr % p using Fermat's little theorem.
ll nCrModP(ll n, ll r, ll p){
// Base case
if(r == 0)
return 1;
// Fill factorial array so that we can find all factorial of r, n and n - r
ll fact[n + 1];
fact[0] = 1;
fl(i, 1, n + 1){
fact[i] = (fact[i - 1] * i) % p;
}
return (fact[n] * modInverse(fact[r], p) % p * modInverse(fact[n - r], p) % p) % p;
}
int main(){
cout << nCrModP(10, 2, 13);
// 10C2 is 45 and 45 % 13 = 6
return 0;
}