Under construction

Still being built. Things may change

Conveyor Belts

Time limit1sMemory limit512 MB

Summary
Build a belt network of at most 200 fixed a:b splitters that divides one global input into c:d between the two outputs, given a, b, c, and d.
Level

Hard8 of 10

Topics
Math, Number theory, Recursion
Solved
No attempts yet

Problem

You work as an employee of the Boxing And Processing Company and must distribute boxes in one of the company's enormous warehouses. At BAPC Ltd., boxes travel on conveyor belts. Two belts can be merged into one by letting one drop its contents onto the other. A belt can also be split into two with a splitter, which sends a fixed portion of its input to its first output and the rest to its second output.

Adjustable splitters can divide their input in any ratio. To cut costs, your boss ordered cheap knock-off splitters that only divide their input in a fixed ratio a:ba : b. You need c:dc : d splitters, but instead of arguing with the boss, you decide to build them yourself.

You must watch your costs. The system cannot have boxes that never leave it, and it cannot use splitters that never receive any boxes. You also cannot use too many a:ba : b splitters in total.

Given the ratios a:ba : b and c:dc : d, construct a network of belts and at most 200 knock-off a:ba : b splitters. The network has a single global input belt and two global output belts, and the global input is distributed in the ratio c:dc : d.

A splitter with ratio x:yx : y sends exactly xx+y\frac{x}{x+y} of its incoming boxes to the first output and exactly yx+y\frac{y}{x+y} to the second output.

Input

The first line contains two integers aa and bb with 1≤a,b≤1091 \le a, b \le 10^9 and a+b≤109a + b \le 10^9, the ratio of the knock-off splitter.

The second line contains two integers cc and dd with 1≤c,d≤1091 \le c, d \le 10^9 and c+d≤109c + d \le 10^9, the desired ratio.

Output

The first line contains an integer nn with 1≤n≤2001 \le n \le 200, the number of a:ba : b splitters used.

Then follow nn lines. The ii-th line contains two integers lil_i and rir_i with −2≤li,ri<n-2 \le l_i, r_i < n. Here lil_i is the splitter connected to the left output of the ii-th splitter, which receives aa+b\frac{a}{a+b} of its input. Similarly, rir_i is the splitter receiving ba+b\frac{b}{a+b} of its input. Splitters are indexed from 0. The global input is connected to splitter 0. The values −1-1 and −2-2 stand for the first and second global output, which must receive cc+d\frac{c}{c+d} and dc+d\frac{d}{c+d} of the global input.

A splitter cannot be placed where no box reaches it, and it cannot be placed where a box passing through it never reaches an output. If several solutions exist, any one is accepted.

Examples3

  1. Example 1

    Input
    2 3
    3 2
    
    Expected output
    1
    -2 -1
    
  2. Example 2

    Input
    1 2
    3 4
    
    Expected output
    3
    -1 1
    2 1
    0 -2
    
  3. Example 3

    Input
    1 2
    1 2
    
    Expected output
    3
    -2 1
    2 0
    1 -1