template <int M = 1000000007> struct rational{ ll p, q; rational(ll p = 0, ll q = 1):p(p), q(q){} rational operator + (const rational &rhs) const{ return rational(p * rhs.q + q * rhs.p, q * rhs.q); } rational operator - (const rational &rhs) const{ return rational(p * rhs.q - q * rhs.p, q * rhs.q); } rational operator * (const rational &rhs) const{ return rational(p * rhs.p, q * rhs.q); } rational operator / (const rational &rhs) const{ return rational(p * rhs.q, q * rhs.p); } bool operator < (const rational &rhs) const{ return p * rhs.q < q * rhs.p; } bool operator > (const rational &rhs) const{ return p * rhs.q > q * rhs.p; } bool operator == (const rational &rhs) const{ return p * rhs.q == q * rhs.p; } bool operator != (const rational &rhs) const{ return p * rhs.q != q * rhs.p; } bool operator <= (const rational &rhs) const{ return p * rhs.q <= q * rhs.p; } bool operator >= (const rational &rhs) const{ return p * rhs.q >= q * rhs.p; } void format(){ ll g = __gcd(p, q); p /= g; q /= g; } friend ostream& operator<<(ostream & os, rational rhs) { rhs.format(); os << rhs.p << "/" << rhs.q << endl; } ll inv(){ format(); return (p * quickPow(q, mod - 2)) % mod; } };
标签:const,rhs,板子,return,逆元,bool,rational,operator,有理数 From: https://www.cnblogs.com/tiany7/p/16875781.html