|
| 1 | +#include <cassert> |
| 2 | +#include <limits> |
| 3 | +#include <type_traits> |
| 4 | + |
| 5 | +// Dyadic rational, surreal numbers (超現実数) |
| 6 | +// https://atcoder.jp/contests/abc229/submissions/28887690 |
| 7 | +template <class Int, class Uint = unsigned long long> struct DyadicRational { |
| 8 | + Int integ; // 整数部分 |
| 9 | + Uint frac; // 小数部分の分子 |
| 10 | + |
| 11 | + static constexpr int FracLen = std::numeric_limits<Uint>::digits - 1; // 2^63 |
| 12 | + static constexpr Uint denom = Uint(1) << FracLen; // 小数部分の分母 |
| 13 | + |
| 14 | + DyadicRational(Int x = 0) : integ(x), frac(0) { |
| 15 | + static_assert( |
| 16 | + 0 < FracLen and FracLen < std::numeric_limits<Uint>::digits, "FracLen value error"); |
| 17 | + static_assert(std::is_signed<Int>::value == true, "Int must be signed"); |
| 18 | + } |
| 19 | + static DyadicRational _construct(Int x, Uint frac_) { |
| 20 | + DyadicRational ret(x); |
| 21 | + ret.frac = frac_; |
| 22 | + return ret; |
| 23 | + } |
| 24 | + double to_double() const { return integ + double(frac) / denom; } |
| 25 | + |
| 26 | + // static DyadicRational from_rational(Int num, int lg_den); |
| 27 | + bool operator==(const DyadicRational &r) const { return integ == r.integ and frac == r.frac; } |
| 28 | + bool operator!=(const DyadicRational &r) const { return integ != r.integ or frac != r.frac; } |
| 29 | + bool operator<(const DyadicRational &r) const { |
| 30 | + return integ != r.integ ? integ < r.integ : frac < r.frac; |
| 31 | + } |
| 32 | + bool operator<=(const DyadicRational &r) const { return (*this == r) or (*this < r); } |
| 33 | + bool operator>(const DyadicRational &r) const { return r < *this; } |
| 34 | + bool operator>=(const DyadicRational &r) const { return r <= *this; } |
| 35 | + |
| 36 | + DyadicRational operator+(const DyadicRational &r) const { |
| 37 | + auto i = integ + r.integ; |
| 38 | + auto f = frac + r.frac; |
| 39 | + if (f >= denom) ++i, f -= denom; // overflow |
| 40 | + return DyadicRational::_construct(i, f); |
| 41 | + } |
| 42 | + DyadicRational operator-(const DyadicRational &r) const { |
| 43 | + auto i = integ - r.integ; |
| 44 | + auto f = frac - r.frac; |
| 45 | + if (f > denom) --i, f += denom; // overflow |
| 46 | + return DyadicRational::_construct(i, f); |
| 47 | + } |
| 48 | + DyadicRational operator-() const { return DyadicRational() - *this; } |
| 49 | + DyadicRational &operator+=(const DyadicRational &r) { return *this = *this + r; } |
| 50 | + |
| 51 | + DyadicRational right() const { |
| 52 | + if (frac == 0) { |
| 53 | + if (integ >= 0) { |
| 54 | + return DyadicRational(integ + 1); |
| 55 | + } else { |
| 56 | + return DyadicRational::_construct(integ, Uint(1) << (FracLen - 1)); |
| 57 | + } |
| 58 | + } |
| 59 | + int d = __builtin_ctzll(frac); |
| 60 | + return DyadicRational::_construct(integ, frac ^ (Uint(1) << (d - 1))); |
| 61 | + } |
| 62 | + DyadicRational left() const { |
| 63 | + if (frac == 0) { |
| 64 | + if (integ <= 0) { |
| 65 | + return DyadicRational(integ - 1); |
| 66 | + } else { |
| 67 | + return DyadicRational::_construct(integ - 1, Uint(1) << (FracLen - 1)); |
| 68 | + } |
| 69 | + } |
| 70 | + int d = __builtin_ctzll(frac); |
| 71 | + return DyadicRational::_construct(integ, frac ^ (Uint(3) << (d - 1))); |
| 72 | + } |
| 73 | + |
| 74 | + // Surreal number { l | r } |
| 75 | + static DyadicRational surreal(const DyadicRational &l, const DyadicRational &r) { |
| 76 | + assert(l < r); |
| 77 | + DyadicRational x(0); |
| 78 | + if (l.integ > 0) x = DyadicRational(l.integ); |
| 79 | + if (r.integ < 0) x = DyadicRational(r.integ); |
| 80 | + while (true) { |
| 81 | + if (x <= l) { |
| 82 | + x = x.right(); |
| 83 | + } else if (x >= r) { |
| 84 | + x = x.left(); |
| 85 | + } else { |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + return x; |
| 90 | + } |
| 91 | + template <class OStream> friend OStream &operator<<(OStream &os, const DyadicRational &x) { |
| 92 | + return os << x.to_double(); |
| 93 | + } |
| 94 | +}; |
| 95 | +// using dyrational = DyadicRational<long long>; |
0 commit comments