icpc_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ebi-fly13/icpc_library

:heavy_check_mark: utility/rational.hpp

Depends on

Required by

Verified with

Code

#pragma once

#include "../template/template.hpp"

namespace lib {

struct rational {
    rational() : p(0), q(1) {}
    rational(ll n) : p(n), q(1) {}
    rational(ll n, ll m) {
        assert(m != 0);
        if (m < 0) n = -n, m = -m;
        ll g = gcd(n, m);
        p = n / g;
        q = m / g;
    }
    explicit operator const ld () const { return ld(p) / ld(q); }
    rational& operator+=(const rational& rhs){
        ll g = gcd(q, rhs.q);
        ll np = rhs.q / g * p + q / g * rhs.p;
        ll nq = q / g * rhs.q;
        ll ng = gcd(np, nq);
        p = np / ng, q = nq / ng;
        return *this;
    }
    rational& operator-=(const rational& rhs) {
        (*this) += rational(-rhs.p, rhs.q);
        return *this;
    }
    rational& operator*=(const rational& rhs) {
        ll g1 = gcd(q, rhs.p), g2 = gcd(p, rhs.q);
        ll np = p / g2 * rhs.p / g1;
        ll nq = q / g1 * rhs.q / g2;
        p = np, q = nq;
        return *this;
    }
    rational& operator/=(const rational& rhs) {
        (*this) *= rational(rhs.q, rhs.p);
        return *this;
    }
    rational operator+() const {
        return *this;
    }
    rational operator-() const {
        return rational() - *this;
    }
    friend rational operator+(const rational& lhs, const rational& rhs) {
        return rational(lhs) += rhs;
    }
    friend rational operator-(const rational& lhs, const rational& rhs) {
        return rational(lhs) -= rhs;
    }
    friend rational operator*(const rational& lhs, const rational& rhs) {
        return rational(lhs) *= rhs;
    }
    friend rational operator/(const rational& lhs, const rational& rhs) {
        return rational(lhs) /= rhs;
    }
    friend bool operator==(const rational& lhs, const rational& rhs) {
        return lhs.p == rhs.p && lhs.q == rhs.q;
    }
    friend bool operator!=(const rational& lhs, const rational& rhs) {
        return lhs.p != rhs.p || lhs.q != rhs.q;
    }
    friend bool operator<(const rational lhs, const rational rhs) {
        return less_than(lhs, rhs);
    }
    friend bool operator>(const rational lhs, const rational rhs) {
        return less_than(rhs, lhs);
    }
    friend bool operator<=(const rational lhs, const rational rhs) {
        return lhs == rhs || lhs < rhs;
    }
    friend bool operator>=(const rational lhs, const rational rhs) {
        return lhs == rhs || lhs > rhs;
    }
    friend std::ostream& operator<<(std::ostream& os, const rational& r) {
        return os << r.p << " / " << r.q;
    }
    std::pair<ll,ll> val() const {
        return {p, q};
    }

  private:
    ll p, q;
    static bool less_than(rational lhs, rational rhs) {
        __int128_t lv = __int128_t(lhs.p) * __int128_t(rhs.q);
        __int128_t rv = __int128_t(lhs.q) * __int128_t(rhs.p);
        return lv < rv;
    }
};

}  // namespace lib
#line 2 "utility/rational.hpp"

#line 2 "template/template.hpp"

#include <bits/stdc++.h>

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)
#define all(v) v.begin(), v.end()

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template <typename T> bool chmin(T &a, const T &b) {
    if (a <= b) return false;
    a = b;
    return true;
}
template <typename T> bool chmax(T &a, const T &b) {
    if (a >= b) return false;
    a = b;
    return true;
}

namespace lib {

using namespace std;

}  // namespace lib

// using namespace lib;
#line 4 "utility/rational.hpp"

namespace lib {

struct rational {
    rational() : p(0), q(1) {}
    rational(ll n) : p(n), q(1) {}
    rational(ll n, ll m) {
        assert(m != 0);
        if (m < 0) n = -n, m = -m;
        ll g = gcd(n, m);
        p = n / g;
        q = m / g;
    }
    explicit operator const ld () const { return ld(p) / ld(q); }
    rational& operator+=(const rational& rhs){
        ll g = gcd(q, rhs.q);
        ll np = rhs.q / g * p + q / g * rhs.p;
        ll nq = q / g * rhs.q;
        ll ng = gcd(np, nq);
        p = np / ng, q = nq / ng;
        return *this;
    }
    rational& operator-=(const rational& rhs) {
        (*this) += rational(-rhs.p, rhs.q);
        return *this;
    }
    rational& operator*=(const rational& rhs) {
        ll g1 = gcd(q, rhs.p), g2 = gcd(p, rhs.q);
        ll np = p / g2 * rhs.p / g1;
        ll nq = q / g1 * rhs.q / g2;
        p = np, q = nq;
        return *this;
    }
    rational& operator/=(const rational& rhs) {
        (*this) *= rational(rhs.q, rhs.p);
        return *this;
    }
    rational operator+() const {
        return *this;
    }
    rational operator-() const {
        return rational() - *this;
    }
    friend rational operator+(const rational& lhs, const rational& rhs) {
        return rational(lhs) += rhs;
    }
    friend rational operator-(const rational& lhs, const rational& rhs) {
        return rational(lhs) -= rhs;
    }
    friend rational operator*(const rational& lhs, const rational& rhs) {
        return rational(lhs) *= rhs;
    }
    friend rational operator/(const rational& lhs, const rational& rhs) {
        return rational(lhs) /= rhs;
    }
    friend bool operator==(const rational& lhs, const rational& rhs) {
        return lhs.p == rhs.p && lhs.q == rhs.q;
    }
    friend bool operator!=(const rational& lhs, const rational& rhs) {
        return lhs.p != rhs.p || lhs.q != rhs.q;
    }
    friend bool operator<(const rational lhs, const rational rhs) {
        return less_than(lhs, rhs);
    }
    friend bool operator>(const rational lhs, const rational rhs) {
        return less_than(rhs, lhs);
    }
    friend bool operator<=(const rational lhs, const rational rhs) {
        return lhs == rhs || lhs < rhs;
    }
    friend bool operator>=(const rational lhs, const rational rhs) {
        return lhs == rhs || lhs > rhs;
    }
    friend std::ostream& operator<<(std::ostream& os, const rational& r) {
        return os << r.p << " / " << r.q;
    }
    std::pair<ll,ll> val() const {
        return {p, q};
    }

  private:
    ll p, q;
    static bool less_than(rational lhs, rational rhs) {
        __int128_t lv = __int128_t(lhs.p) * __int128_t(rhs.q);
        __int128_t rv = __int128_t(lhs.q) * __int128_t(rhs.p);
        return lv < rv;
    }
};

}  // namespace lib
Back to top page