-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfraction.cpp
96 lines (82 loc) · 1.91 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// hoge
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;
struct frac {
i64 p, q;
void norm() {
i64 g = __gcd(p, q);
p /= g;
q /= g;
if (q < 0) {
q = -q;
p = -p;
}
}
frac operator+(const frac& rhs) const {
frac ret = {p * rhs.q + q * rhs.p, q * rhs.q};
ret.norm();
return ret;
}
frac operator-(const frac& rhs) const {
frac ret = {p * rhs.q - q * rhs.p, q * rhs.q};
ret.norm();
return ret;
}
frac operator*(const frac& rhs) const {
frac ret = {p * rhs.p, q * rhs.q};
ret.norm();
return ret;
}
frac operator*(const i64 rhs) const {
frac ret = {p * rhs, q};
ret.norm();
return ret;
}
frac operator~() const {
return {q, p};
}
bool operator<(const frac& rhs) const {
frac d = (frac){p, q} - rhs;
return d.p < 0;
}
bool operator>(const frac& rhs) const {
frac d = (frac){p, q} - rhs;
return d.p > 0;
}
bool operator==(const frac& rhs) const {
frac d = (frac){p, q} - rhs;
return d.p == 0;
}
frac operator%(const frac& rhs) {
if ((frac){p, q} < rhs) {
return {p, q};
} else {
frac t = (frac){p, q} * (~rhs);
i64 k = t.p / t.q;
return (frac){p, q} - rhs * k;
}
}
};
ostream& operator<<(ostream& os, frac f) {
os << f.p << "/" << f.q;
return os;
}
frac gcd(frac a, frac b) {
return b.p ? gcd(b, a % b) : a;
}
int main() {
vi t(3);
for (int i = 0; i < 3; i++) {
cin >> t[i];
}
sort(t.begin(), t.end());
frac a = {t[1] * t[0], t[1] - t[0]};
frac b = {t[2] * t[1], t[2] - t[1]};
a.norm();
b.norm();
frac g = gcd(a, b);
cout << a * (~g) * b << endl;
}