A to Z Numerals
Time limit1sMemory limit128 MB
Convert each positive integer up to 7e17 into its unique A to Z numeral, where letters a to r and A to R stand for powers of ten and their quintuples.
- Level
Hard9 of 10
- Topics
- Greedy, Math, Number theory, Implementation
- Solved
- No attempts yet
Problem
Roman numerals use the symbols I, V, X, L, C, D, M, worth . A written numeral is evaluated by one rule:
- Rule Δ. A symbol is added when it is the last symbol, or when the symbol directly to its right is worth no more than it is. Otherwise the symbol is subtracted.
For example, MMCDLXIX .
To make the numeral for a positive integer unique, the following rules are applied in order of priority:
- Use as few symbols as possible (
IV, notIIII). - The added symbols, read from left to right, form a non-increasing subsequence (
XIV, notVIX). - Among the shortest numerals, use one with the fewest subtracted symbols.
- If a tie still remains, the subtracted symbols are placed as far to the right as possible.
These rules are weaker than the classical Roman restriction, so shorter forms are allowed as long as rule Δ still holds: IM , ICIC , IVC . For both CCVCII and ICICIC evaluate correctly, but rule 3 keeps CCVCII, which subtracts fewer symbols.
Now use a more regular, extensible alphabet in place of the Roman symbols. The letters a, A, b, B, c, C, …, z, Z stand for ; that is, the lowercase letter at position (counting from ) is and its uppercase partner is . This problem uses only a–r and A–R, so the largest symbols are r and R .
Applying formation rules 1–4 together with rule Δ to this alphabet yields A to Z numerals. For example ad and aAc . The same uppercase letter may not appear more than once in a numeral.
Input
The input contains one or more positive integers, each less than , one per line. The list ends with a line containing only 0.
Output
For each positive integer, print its A to Z numeral on its own line. Do not use a method whose running time is exponential in the number of digits.