UVa 516 - Prime Land

516 - Prime Land

 * http://acm.uva.es/p/v5/516.html

Summary
For this problem you have to know what the Prime Base Representation of a number is.

Use the Prime Sieve of Eratosthenes to create an array of all prime numbers less or equal than 32767, and then use the array to factor numbers.

Explanation
For the Prime Base Representation of a number you have to print the prime factors of the number and the frequency they appear in it's factorization (in decreasing order of prime factors). Something like this:

$$p_n e_n p_{n-1} e_{n-1} ... p_1 e_1$$

Where p is a decreasing sequence of primes and e is the corresponding sequence of powers of p. Each e must be greater than 0, so primes which are not factors are omitted from the prime base representation.

First you have to read a Prime Base Representation of a number $$x$$ and calculate $$x$$ just as you would expect by multiplying

$$p_n ^{e_n} \cdot p_{n-1} ^ {e_{n-1}} \cdot ... \cdot p_1 ^{e_1}$$

Then you have to output the Prime Base Representation of $$x - 1$$.

For example, for the input 5 1 2 1 $$x = 5^1 \cdot 2^1 = 10$$

Then $$x - 1 = 9$$

and

$$9 = 3^2$$ Therefore, the output is 3 2.

Input
17 1 5 1 2 1 509 1 59 1 3 1 151 1 31 1 7 1 131 1 5 3 2 1 0

Output
2 4 3 2 13 1 11 1 7 1 5 1 3 1 2 1 2 1 127 1 43 1 3 1 2 1 32749 1