Created
February 1, 2017 09:11
-
-
Save zhrkvl/ce2a6b5c92028673c86207544fe3dcfd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ll gcd(ll x, ll y) | |
{ | |
while(x > 0 && y > 0) | |
{ | |
if (x > y) | |
x = x % y; | |
else | |
y = y % x; | |
} | |
return x + y; | |
} | |
struct fract | |
{ | |
ll x, y; | |
bool operator<(const fract& to) const | |
{ | |
if (x != to.x) | |
return x < to.x; | |
return y < to.y; | |
} | |
bool operator==(fract& to) | |
{ | |
return x == to.x && y == to.y; | |
} | |
bool operator==(fract to) | |
{ | |
return x == to.x && y == to.y; | |
} | |
bool operator==(fract to) const | |
{ | |
return x == to.x && y == to.y; | |
} | |
fract operator+(fract to) | |
{ | |
fract nx = *this, ny = to; | |
ll comm = y / gcd(y, to.y) * to.y; | |
nx.x *= comm / nx.y; | |
ny.x *= comm / ny.y; | |
return fract(nx.x + ny.x, comm); | |
} | |
fract operator/(int x) | |
{ | |
fract res = *this; | |
res.y *= x; | |
ll k = gcd(res.x, res.y); | |
res.x /= k; | |
res.y /= k; | |
return res; | |
} | |
fract(ll x, ll y) : x(x), y(y) | |
{ | |
ll k = gcd(x, y); | |
x /= k; | |
y /= k; | |
} | |
fract() : x(0), y(1) {} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment