This page is still under construction.

Parts of this page are still being built. What you see may change.

Prime Land

Interview

Time limit1sMemory limit128 MB

Summary
Given an integer x in prime-base form (prime powers in decreasing order), print the same representation for x - 1.
Level

Medium5 of 10

Topics
Number theory, Math, Implementation, Brute force
Solved
No attempts yet

Problem

In Prime Land everyone uses a prime-base number system. In this system every positive integer xx is written as follows. Let {pi}i=0∞\{p_i\}_{i=0}^{\infty} be the increasing sequence of all prime numbers, so p0=2p_0 = 2, p1=3p_1 = 3, p2=5p_2 = 5, and so on. Every integer x>1x > 1 has exactly one factorization into prime powers, so there is an index kxk_x and uniquely determined exponents ekx,ekx−1,…,e1,e0e_{k_x}, e_{k_x - 1}, \dots, e_1, e_0 with ekx>0e_{k_x} > 0 such that

x=pkxekx⋅pkx−1ekx−1⋯p1e1⋅p0e0.x = p_{k_x}^{e_{k_x}} \cdot p_{k_x - 1}^{e_{k_x - 1}} \cdots p_1^{e_1} \cdot p_0^{e_0}.

The sequence (ekx,ekx−1,…,e1,e0)(e_{k_x}, e_{k_x - 1}, \dots, e_1, e_0) is the representation of xx in the prime-base number system.

In this system multiplication and division are easy, but addition and subtraction are hard. Your task is the operation "minus one": given xx in prime-base representation, output x−1x - 1 in prime-base representation.

For convenience the prime-base representation is written as a sequence of pairs pi eip_i\ e_i, listing only those ii for which ei>0e_i > 0, in decreasing order of pip_i.

Input

The input consists of one or more lines. Every line except the last holds the prime-base representation of a single integer xx with 2<x≤327672 < x \le 32767: the pairs pi eip_i\ e_i (only those with ei>0e_i > 0) in decreasing order of pip_i, with all numbers separated by single spaces. The last line contains a single 00 and is not processed.

Output

For every input line except the last, print one line with x−1x - 1 in prime-base representation: the pairs pi eip_i\ e_i (only those with ei>0e_i > 0) in decreasing order of pip_i, separated by single spaces.

Examples1

  1. Example 1

    Input
    17 1
    5 1 2 1
    509 1 59 1
    0
    
    Expected output
    2 4
    3 2
    13 1 11 1 7 1 5 1 3 1 2 1