1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
use std::ops::{Add, AddAssign, Mul, Neg};

// https://atcoder.jp/contests/abc294/editorial/6017
pub trait FractionBisect: Sized + SbInt {
    fn fraction_bisect(
        self,
        pred: impl Fn(Self, Self) -> bool,
    ) -> ((Self, Self), (Self, Self)) {
        let bound = self;

        let fr_neg_infty = (Self::ONE.neg(), Self::ZERO);
        let fr_zero = (Self::ZERO, Self::ONE);
        let ztf = pred(fr_zero.0, fr_zero.1);
        let pred = {
            if !ztf && !Self::SIGNED {
                return (fr_zero, fr_zero);
            }
            if Self::SIGNED && !ztf && !pred(fr_neg_infty.0, fr_neg_infty.1) {
                return (fr_neg_infty, fr_neg_infty);
            }
            move |fr: Fraction<Self>| {
                if ztf { pred(fr.0, fr.1) } else { !pred(fr.0.neg(), fr.1) }
            }
        };

        let mut lower = Fraction::zero();
        let mut upper = Fraction::infty();
        let (small, large) = 'outer: loop {
            let cur = lower + upper;
            if cur.is_deeper(bound) {
                break (lower, upper);
            }

            let tf = pred(cur);
            let (from, to) = if tf { (lower, upper) } else { (upper, lower) };

            let mut lo = Self::ONE;
            let mut hi = lo + Self::ONE;
            while pred(from + to * hi) == tf {
                lo += lo;
                hi += hi;
                if (from + to * lo).is_deeper(bound) {
                    let steps = bound.steps(from.into_inner(), to.into_inner());
                    let front = from + to * steps;
                    let res = if tf { (front, upper) } else { (lower, front) };
                    break 'outer res;
                }
            }

            while lo.lt1(hi) {
                let mid = lo.avg(hi);
                let tmp = from + to * mid;
                let cur = pred(tmp) == tf && !tmp.is_deeper(bound);
                *(if cur { &mut lo } else { &mut hi }) = mid;
            }

            let next = from + to * lo;
            *(if tf { &mut lower } else { &mut upper }) = next;
        };

        let (left, right) = if ztf { (small, large) } else { (-large, -small) };
        (left.into_inner(), right.into_inner())
    }
}

impl<I: SbInt> FractionBisect for I {}

#[derive(Clone, Copy, Eq, PartialEq)]
struct Fraction<I>(I, I);

pub trait SbInt:
    Copy
    + Eq
    + PartialOrd<Self>
    + AddAssign<Self>
    + Add<Self, Output = Self>
    + Mul<Self, Output = Self>
    + std::fmt::Display
{
    const ZERO: Self;
    const ONE: Self;
    const SIGNED: bool;
    fn lt1(self, other: Self) -> bool;
    fn avg(self, other: Self) -> Self;
    fn abs(self) -> Self;
    fn neg(self) -> Self;
    fn steps(self, from: (Self, Self), to: (Self, Self)) -> Self;
}

impl<I: SbInt> Neg for Fraction<I> {
    type Output = Self;
    fn neg(self) -> Self { self.neg() }
}

macro_rules! impl_uint {
    ( $($ty:ty)* ) => { $(
        impl SbInt for $ty {
            const ZERO: $ty = 0;
            const ONE: $ty = 1;
            const SIGNED: bool = false;
            fn lt1(self, other: Self) -> bool { self + 1 < other }
            fn avg(self, other: Self) -> Self {
                self + (other - self) / 2
            }
            fn abs(self) -> Self { self }
            fn neg(self) -> Self { self.wrapping_neg() } // not to be called
            fn steps(self, from: (Self, Self), to: (Self, Self)) -> Self {
                if to.1 == 0 {
                    Self::ZERO
                } else {
                    (self - from.1) / to.1
                }
            }
        }
    )* }
}

impl_uint! { u8 u16 u32 u64 u128 usize }

macro_rules! impl_int {
    ( $($ty:ty)* ) => { $(
        impl SbInt for $ty {
            const ZERO: $ty = 0;
            const ONE: $ty = 1;
            const SIGNED: bool = true;
            fn lt1(self, other: Self) -> bool { self + 1 < other }
            fn avg(self, other: Self) -> Self {
                self + (other - self) / 2
            }
            fn abs(self) -> Self { self.abs() }
            fn neg(self) -> Self { -self}
            fn steps(self, from: (Self, Self), to: (Self, Self)) -> Self {
                if to.1 == 0 {
                    Self::ZERO
                } else {
                    (self - from.1) / to.1
                }
            }
        }
    )* }
}

impl_int! { i8 i16 i32 i64 i128 isize }

impl<I: SbInt> Fraction<I> {
    fn zero() -> Self { Self(I::ZERO, I::ONE) }
    fn infty() -> Self { Self(I::ONE, I::ZERO) }
}

impl<I: SbInt> Mul<I> for Fraction<I> {
    type Output = Self;
    fn mul(self, a: I) -> Self { Self(self.0 * a, self.1 * a) }
}

impl<I: SbInt> Add<Fraction<I>> for Fraction<I> {
    type Output = Self;
    fn add(self, other: Self) -> Self {
        // mediant
        Self(self.0 + other.0, self.1 + other.1)
    }
}

impl<I: SbInt> Fraction<I> {
    fn is_deeper(self, bound: I) -> bool { self.1.abs() > bound }
    fn neg(self) -> Self { Self(self.0.neg(), self.1) }
    fn into_inner(self) -> (I, I) { (self.0, self.1) }
}

#[test]
fn sanity_check() {
    let sqrt3 = 5000_u64.fraction_bisect(|x, y| x * x <= 3 * y * y);
    assert_eq!(sqrt3, ((3691, 2131), (5042, 2911)));

    assert_eq!(10_u64.fraction_bisect(|_, _| false), ((0, 1), (0, 1)));
    assert_eq!(10_i64.fraction_bisect(|_, _| false), ((-1, 0), (-1, 0)));

    let neg_sqrt3 = 5000_i64.fraction_bisect(|x, y| x < 0 && x * x > 3 * y * y);
    assert_eq!(neg_sqrt3, ((-5042, 2911), (-3691, 2131)));

    let lt = 5000_i64.fraction_bisect(|x, y| 5 * x < 2 * y);
    assert_eq!(lt, ((1999, 4998), (2, 5)));
    let le = 5000_i64.fraction_bisect(|x, y| 5 * x <= 2 * y);
    assert_eq!(le, ((2, 5), (1999, 4997)));
}

#[test]
fn sqrt() {
    let sqrt3 = 10_u128.pow(18).fraction_bisect(|x, y| x * x <= 3 * y * y);
    let sqrt4 = 10_u128.pow(18).fraction_bisect(|x, y| x * x <= 4 * y * y);

    assert_eq!(sqrt3.0, (734231055024833855, 423908497265970753));
    assert_eq!(sqrt3.1, (1002978273411373057, 579069776145402304));

    assert_eq!(sqrt4.0, (2, 1));
    assert_eq!(sqrt4.1, (999999999999999999, 499999999999999999));
}

#[test]
fn improper_fraction() {
    let x = 6_u32.fraction_bisect(|x, y| x * 5 <= y * 13);
    assert_eq!(x, ((13, 5), (8, 3)));
}

#[test]
fn iterate() {
    let bound = 6_u32;
    let next = |p, q| bound.fraction_bisect(|x, y| x * q <= p * y).1;

    let (p, q): (Vec<_>, Vec<_>) =
        std::iter::successors(Some((0, 1)), |&(p, q)| Some(next(p, q)))
            .take_while(|&(p, q)| p <= q)
            .unzip();

    assert_eq!(p, [0, 1, 1, 1, 1, 2, 1, 3, 2, 3, 4, 5, 1]);
    assert_eq!(q, [1, 6, 5, 4, 3, 5, 2, 5, 3, 4, 5, 6, 1]);
}