Egyptian Fractions#1818

Egyptian Fractions

1 second
64 megabytes
Very easy

During the Middle Kingdom of Egypt, the Egyptians developed a way to write fractions. By adding a symbol to the hieroglyph for an integer, they represented the reciprocal of that integer. This made it easy to write any fraction of the form (called a unit fraction)—simply add the reciprocal symbol to the hieroglyph for .

There was no direct method to represent a fraction of the form , where m > 1. Instead, such a fraction was written as the sum of unit fractions. For example, the fraction could be written as:

prb1818-4
prb1818-3

Notice that multiple sums may yield the same result; for example, can also be written as:

prb1818-5
prb1818-2
prb1818-6
prb1818-7

Your task is to take a fraction and write it as a sum of unit fractions using a "greedy" search. In this approach, you repeatedly subtract the largest possible unit fraction from the remainder until you reach zero. For example, consider the fraction . The greedy search would find the sum in three steps:

prb1818-8

Note that at each step, we subtract the largest possible unit fraction from what remains of our original fraction.

prb1818-9
prb1818-10
prb1818-11
prb1818-12
prb1818-13

To keep solutions manageable, there is an additional requirement: Each time you subtract a unit fraction, the resulting fraction must have a denominator strictly less than . For example, consider the fraction . The first two unit fractions to subtract are and , leaving . At this point, the largest possible unit fraction is , leaving

prb1818-14
prb1818-15

However, this creates a fraction with a denominator greater than , so this unit fraction cannot be used. Move to the next possible unit fraction, , which results in

prb1818-16
prb1818-17

Now the answer reduces to a fraction with a denominator less than , so this unit fraction can be used, leaving a final result of .

In this case, only one fraction was skipped. In general, you may need to skip many fractions to find a suitable one. As you search, you may need to perform calculations on fractions with large denominators; ensure you do so efficiently, or your program may take too long to run.

Also, use a data type large enough to hold the large numbers involved. Even though denominators are restricted to , intermediate calculations may use denominators up to 10^{12}. A 64-bit integer is required to store such values (long in Java, long long in C/C++).

Finally, the nature of the greedy algorithm means it will always find some solution with denominators less than since any fraction can be represented as a sum of unit fractions with its own denominator. For example:

prb1818-18

Input

prb1818-2

The input consists of a sequence of fractions, one per line. Each line contains two integers and with 1 < M < N < 100, representing the fraction . and have no common divisors greater than . The end of the input is indicated by .

Output

prb1818-2

For each input fraction , output a single line containing numbers D_1 \le D_2 \le D_3 \le \dots such that:

prb1818-19

This must be the first sum found by the greedy algorithm while respecting the denominator limit of . Each number should be separated by a space, with no leading or trailing whitespace.

Examples

Example 1

Loading
Loading