def f(x, y): if x < y: return (0, x) (s, t) = f(x//2, y) (s, t) = (2*s, 2*t) t += x%2 if t >= y: return (s+1, t-y) else: return (s, t)