#C6876. Custom Numeral System Conversion

    ID: 50684 Type: Default 1000ms 256MiB

Custom Numeral System Conversion

Custom Numeral System Conversion

You are given a numeral system described by a base \(n\) and a character set (of length \(n\)). In this numeral system, each digit is represented by a unique character from the given character set. Your task is to convert numbers between the custom numeral system and the decimal system based on a query.

The program will first read an integer \(T\) representing the number of test queries. Each test query begins with a command, followed by the data for conversion:

  • If the command is C2D: You will be given an integer base \(n\), a string representing the character set, and a custom numeral string. You must output the equivalent decimal value.
  • If the command is D2C: You will be given an integer base \(n\), a string representing the character set, and a non-negative decimal number. You must output the equivalent representation in the custom numeral system.

Note: The conversion methods you implement must use the standard algorithm for numeral conversion. In addition, if a decimal number is 0, its representation is the first character of the character set.

All formulae are written in \(\LaTeX\) format. For example, the conversion from a custom numeral \(d_kd_{k-1}...d_0\) to decimal is given by:

[ value = \sum_{i=0}^{k} (\text{index}(d_i)) \times n^{(k-i)} ]

inputFormat

The input is read from stdin and has the following format:

T
query_1
n_1
charset_1
number_1
query_2
n_2
charset_2
number_2
... (for each query)

where:

  • T is the number of test queries.
  • query is either C2D for conversion from the custom numeral system to decimal, or D2C for conversion from decimal to the custom numeral system.
  • n is an integer representing the base (\(n\)) of the numeral system.
  • charset is a string of exactly \(n\) distinct characters representing the numeral system.
  • number is either a custom numeral string (if the query is C2D) or a non-negative decimal number (if the query is D2C).

outputFormat

For each test query, output the result on a separate line to stdout:

  • If the query is C2D, output the decimal equivalent of the provided custom numeral.
  • If the query is D2C, output the representation of the decimal number in the custom numeral system.
## sample
2
C2D
16
0123456789ABCDEF
1A
D2C
8
01234567
58
26

72

</p>