Hexaroman Numbers
Time limit1sMemory limit128 MB
Parse and output hexadecimal Roman numerals, choosing the shorter of additive or subtractive notation per digit, then evaluate +, -, and * expressions.
- Level
Medium7 of 10
- Topics
- String, Implementation, Math, Simulation
- Solved
- No attempts yet
Problem
Roman numerals are the numeral system of ancient Rome, based on letters of the alphabet that are combined to signify the sum (or in some cases the difference) of their values. The system is decimal but not directly positional: the same digit in different positions of the usual decimal system is represented by different Roman symbols, and a single decimal digit may be represented by several Roman symbols.
The seven Roman symbols correspond to the decimal values , , , , , , . Roman numerals are generally written from left to right in descending order and added in sequence; for example is interpreted as . Certain combinations use the subtractive principle: when a symbol of smaller value precedes a symbol of larger value, the smaller value is subtracted from the larger one and the result is added to the total. For example . may precede or , and may precede or . The symbols , , and may not be followed by a symbol of greater or equal value.
The subtractive principle was introduced in the Middle Ages. Originally it allowed only a single smaller symbol to precede a larger one. Today this restriction can be dropped to allow shorter notation for some numbers. Because modern computers work much more naturally with hexadecimal numbers, it also makes sense to introduce a "hexadecimal" Roman notation.
Let hexadecimal Roman notation be a notation in which the Roman symbols correspond to the following values: , , , , , , (that is, , , , , , , in hexadecimal). For each hexadecimal digit, the notation with fewer symbols (using addition or subtraction) is used. If both notations use the same number of symbols, the additive notation is used. For example, the hexadecimal number is written as , and becomes .
You must write a program that performs computation in hexadecimal Roman notation, namely addition, subtraction, and multiplication. Every input number and every result is an integer between and (hexadecimal), inclusive.
Input
The first line contains one integer (), the number of test lines. Each of the following lines contains data in the format <A><O><B> with no spaces. <A> and <B> are numbers in hexadecimal Roman notation, and <O> denotes the operation, one of +, -, or *.
Output
For each input line, output the hexadecimal Roman notation of the corresponding result on its own line.