-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodular_arithmetic.cpp
More file actions
66 lines (46 loc) · 1.04 KB
/
modular_arithmetic.cpp
File metadata and controls
66 lines (46 loc) · 1.04 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
62
63
64
65
66
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct Mod{
int mod=998244353;
Mod(int new_mod){
mod=new_mod;
}
Mod(){
}
int add(int x,int y){
return (x+y)%mod;
}
int sub(int x,int y){
return (x-y+mod)%mod;
}
int mul(int x,int y){
return ((x%mod)*(y%mod))%mod;
}
int poww(int x,int y){
if(y==0)return 1;
int xx=poww(x,y/2LL);
xx=mul(xx,xx);
if(y%2==1){
xx=mul(xx,x);
}
return xx;
}
// only works if y is prime, else returns incorrect results
int divv(int x,int y){
int yinv=poww(y,mod-2LL);
return mul(x,yinv);
}
};
int main(){
Mod mod;
cout<<"Testing mod...\n";
assert(mod.add(998244353,5)==5);
assert(mod.sub(5,6)==998244352);
assert(mod.divv(mod.mul(100000,100000),100000)==100000);
Mod Mod2(5);
assert(mod.add(4,5)==4);
assert(mod.sub(4,5)==4);
cout<<"Tests passed!\n";
return 0;
}