Egyptian Fractions

Time limit1sMemory limit128 MB

Summary
Apply the greedy Egyptian fraction algorithm to M/N, capping each remainder's denominator below 1,000,000, and print the unit fraction denominators.
Level

Medium5 of 10

Topics
Greedy, Number theory, Math, Simulation
Solved
No attempts yet

Problem

The ancient Egyptians used a distinctive way to write fractions. They created a hieroglyph for each unit fraction 1k\frac{1}{k} (a fraction whose numerator is 11) and represented other fractions by adding unit fractions together. Because this system could not directly express a fraction with a numerator greater than 11, every fraction was written as a sum of unit fractions.

For example, 34\frac{3}{4} can be written as

34=12+14\frac{3}{4} = \frac{1}{2} + \frac{1}{4}

A fraction may have several such representations. For instance, 34\frac{3}{4} can also be written as

34=14+14+14\frac{3}{4} = \frac{1}{4} + \frac{1}{4} + \frac{1}{4}

Given a fraction MN\frac{M}{N}, we want to express it as a sum of unit fractions using the greedy method. The greedy method repeatedly subtracts the largest unit fraction that can be taken from the current remainder, until the remainder becomes 00. For example, applying the greedy method to 920\frac{9}{20} gives

920=13+19+1180\frac{9}{20} = \frac{1}{3} + \frac{1}{9} + \frac{1}{180}

To keep the denominators from growing too large, we add the following restriction. After subtracting a unit fraction, the denominator of the remaining fraction (in lowest terms) must always be less than 1,000,0001{,}000{,}000. If subtracting the largest possible unit fraction would leave a remainder whose denominator is 1,000,0001{,}000{,}000 or more, that unit fraction may not be used. In that case we try the next unit fractions in turn ( 1d+1\frac{1}{d+1} instead of 1d\frac{1}{d}, increasing the denominator by 11 each time) and use the largest one for which the remaining denominator drops below 1,000,0001{,}000{,}000.

For example, starting from 1769\frac{17}{69}, the first two unit fractions are 15\frac{1}{5} and 122\frac{1}{22}, leaving 77590\frac{7}{7590}. The largest unit fraction that could be subtracted next is 11085\frac{1}{1085}, but

77590−11085=11647030\frac{7}{7590} - \frac{1}{1085} = \frac{1}{1647030}

leaves a denominator greater than 1,000,0001{,}000{,}000. So 11085\frac{1}{1085} cannot be used, and subtracting the next unit fraction 11086\frac{1}{1086} gives

77590−11086=1686895\frac{7}{7590} - \frac{1}{1086} = \frac{1}{686895}

which satisfies the restriction. The final answer is therefore

1769=15+122+11086+1686895\frac{17}{69} = \frac{1}{5} + \frac{1}{22} + \frac{1}{1086} + \frac{1}{686895}

Every fraction can also be written as a sum of unit fractions that all share the same denominator; for example, MN\frac{M}{N} equals 1N\frac{1}{N} added MM times. Hence there is no fraction that cannot be represented by this method.

Input

The input consists of several test cases. Each test case is a single line containing two integers MM and NN separated by a space, denoting the fraction MN\frac{M}{N}. It is guaranteed that 1<M<N<1001 < M < N < 100 and that gcd⁡(M,N)=1\gcd(M, N) = 1. The last line contains 0 0, which is not processed.

Output

For each test case, output on one line the denominators D1,D2,D3,…D_1, D_2, D_3, \dots of the unit fractions produced by the greedy method described above, separated by spaces, so that

MN=1D1+1D2+1D3+⋯\frac{M}{N} = \frac{1}{D_1} + \frac{1}{D_2} + \frac{1}{D_3} + \cdots

holds. Print them in the order D1≤D2≤D3≤⋯D_1 \le D_2 \le D_3 \le \cdots.

Examples2

  1. Example 1

    Input
    3 4
    2 7
    9 20
    17 69
    0 0
    
    Expected output
    2 4
    4 28
    3 9 180
    5 22 1086 686895
    
  2. Example 2

    Input
    2 3
    0 0
    
    Expected output
    2 6