有理数逆元板子

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;
    }
};

 

posted @ 2022-11-10 03:59  tiany7  阅读(25)  评论(0编辑  收藏  举报