#!/usr/bin/env python3 from fractions import Fraction a, b = map(int, input().split()) c, d = map(int, input().split()) target = Fraction(c,c+d) split = Fraction(a,a+b) done = {Fraction(1): -1, Fraction(0): -2} pairs = {split: (-1, -2), 1-split: (-2, -1)} solution = [None] def solve(root, target): assert len(solution) == root+1 #print('solve', root, target, len(pairs)) # Are we done: if target in done: solution[root] = [done[target], done[target]] return # Find the minimal remaining denominator bestval = None bestmove = None # (None, move) or (move, None) for v, move in done.items(): # try on the left # target = split * v + (1-split)*t2 t2 = (target - v*split)/(1-split) if t2 >= 0 and t2 <= 1 and (bestval is None or t2.denominator < bestval.denominator): bestval = t2 bestmove = [move, None] # try on the right # target = split * t2 + (1-split)*v t2 = (target - v*(1-split))/split if t2 >= 0 and t2 <= 1 and (bestval is None or t2.denominator < bestval.denominator): bestval = t2 bestmove = [None, move] # Try to write target as a pair split*t1 + (1-split)*t2 directly for v, move in pairs.items(): if v == target: solution[root] = move #print('FOUND PAIR SOLUTION') return # try on the left # target = split * v + (1-split)*t2 t2 = (target - v*split)/(1-split) if t2 >= 0 and t2 <= 1 and (bestval is None or t2.denominator < bestval.denominator): bestval = t2 bestmove = [move, None] # try on the right # target = split * t2 + (1-split)*v t2 = (target - v*(1-split))/split if t2 >= 0 and t2 <= 1 and (bestval is None or t2.denominator < bestval.denominator): bestval = t2 bestmove = [None, move] for v, move in done.items(): pairs[split*v + (1-split)*target] = (move, root) pairs[split*target + (1-split)*v] = (root, move) done[target] = root for i in range(2): if bestmove[i] is None: bestmove[i] = root+1 #print('\t\t\t\t', bestmove) solution[root] = bestmove solution.append(None) solve(root+1, bestval) solve(0, target) i = 0 while i < len(solution): l, r = solution[i] if not isinstance(l, int): u = len(solution) solution[i][0] = u solution.append(l) if not isinstance(r, int): u = len(solution) solution[i][1] = u solution.append(r) i += 1 print(len(solution)) for l, r in solution: print(l, r)