-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodulo_arithmatic.cpp
69 lines (60 loc) · 1.13 KB
/
modulo_arithmatic.cpp
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
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
using namespace std;
const int p = 5, N = 1e5;
int fact[N];
int addm(int x, int y)
{
return (x + y) % p;
}
int subm(int x, int y)
{
return ((x - y) % p + p) % p;
}
int mulm(long long int x, long long int y)
{
return ((x * y)) % p;
}
int powrm(int x, int y)
{
int res = 1;
while (y)
{
if (y & 1)
res = mulm(res, x);
y /= 2;
x = mulm(x, x);
}
return res;
}
int divm(int x, int y)
{
return mulm(x, powrm(y, p - 2));
}
void calculate_factorials()
{
fact[0] = 1;
for (int i = 1; i < N; i++)
{
fact[i] = mulm(fact[i - 1], i);
}
}
int inv(int x)
{
return powrm(x, p - 2);
}
int ncr(int n, int r)
{
return mulm(mulm(fact[n], inv(fact[r])), inv(fact[n - r]));
}
int main()
{
cout << addm(3, 5) << endl;
cout << subm(-3, 5) << endl;
cout << mulm(324, 234) << endl;
cout << divm(56, 2) << endl;
calculate_factorials();
cout << fact[5]; // if p=7 ans= 1
cout << ncr(5, 2) << endl;
cout << ncr(5778, 123) << endl;
return 0;
}