forked from beet-aizu/library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfraction.cpp
49 lines (49 loc) · 1.32 KB
/
fraction.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
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
//BEGIN CUT HERE
struct frac{
int num,dom;
frac(){}
frac(int num,int dom):num(num),dom(dom){}
frac norm(){
if(num==0) return frac(0,1);
int tmp=__gcd(num,dom);
return frac(num/tmp,dom/tmp);
}
frac norm2(){
if(num==0) return frac(0,1);
while(num<0) num+=dom;
while(num>=dom) num-=dom;
int tmp=__gcd(num,dom);
return frac(num/tmp,dom/tmp);
}
frac operator+(frac a){return frac(num*a.dom+a.num*dom,dom*a.dom).norm();}
frac operator-(frac a){return frac(num*a.dom-a.num*dom,dom*a.dom).norm();}
frac operator*(frac a){return frac(num*a.num,dom*a.dom).norm();}
frac operator/(frac a){return frac(num*a.dom,dom*a.num).norm();}
frac operator*(int k){return frac(num*k,dom).norm();}
frac operator/(int k){return frac(num,dom*k).norm();}
bool operator<(const frac a)const{
return num*a.dom<a.num*dom;
}
bool operator>(const frac a)const{
return num*a.dom>a.num*dom;
}
bool operator==(const frac a)const{
return num*a.dom==a.num*dom;
}
bool operator!=(const frac a)const{
return num*a.dom!=a.num*dom;
}
bool operator<=(const frac a)const{
return num*a.dom<=a.num*dom;
}
bool operator>=(const frac a)const{
return num*a.dom>=a.num*dom;
}
};
//END CUT HERE
signed main(){
return 0;
}