Integers in Rational Bases

Time limit1sMemory limit512 MB

Summary
Given coprime p and q, write a positive integer n in the unique base p/q expansion whose digits are all at most p-1, printing digits 0-9, A-Z, a-z.
Level

Hard8 of 10

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

Problem

Given relatively prime positive integers p>qp > q, any positive integer nn can be written uniquely as a linear combination of powers of (p/q)(p/q) with coefficients ranging from 00 to p−1p-1.

n=a0+a1(p/q)+a2(p/q)2+⋯n = a_0 + a_1(p/q) + a_2(p/q)^2 + \cdots

For example,

15=2(3/2)4+1(3/2)3+0(3/2)2+1(3/2)+015 = 2(3/2)^4 + 1(3/2)^3 + 0(3/2)^2 + 1(3/2) + 0

15=4(7/4)2+1(7/4)+115 = 4(7/4)^2 + 1(7/4) + 1

Write a program that finds the base (p/q)(p/q) expansion of an integer nn. Use the characters 0-9, then A-Z, then a-z as digits for the base (p/q)(p/q) expansion.

Input

Input consists of a single line containing 3 space-separated decimal values. They are the numerator pp (3≤p≤623 \le p \le 62) of the fractional base, followed by the denominator qq (2≤q≤p−12 \le q \le p-1) of the fractional base, followed by the positive integer nn to be represented in base (p/q)(p/q). The values of pp, qq, and nn are chosen so that pp and qq are relatively prime, the expansion has at most 40 digits, and nn fits in a 32-bit unsigned integer.

Output

Your program should produce a single output line containing a string of digits [0-9,A-Z,a-z] with the most significant digit first.

Examples3

  1. Example 1

    Input
    3 2 15
    
    Expected output
    21010
    
  2. Example 2

    Input
    7 4 15
    
    Expected output
    411
    
  3. Example 3

    Input
    59 31 987654321
    
    Expected output
    V3bkX4XQVKITSN3ur6TAGF1pSFi