#P1013. [NOIP 1998 提高组] 进制位

[NOIP 1998 提高组] 进制位

Description

To check students’ understanding of positional numeral systems, the famous scientist Lu Si gave the following addition table, where letters represent digits. For example:

$$\def\arraystretch{2} \begin{array}{c||c|c|c|c} \rm + & \kern{.5cm} \rm \mathclap{L} \kern{.5cm} & \kern{.5cm} \rm \mathclap{K} \kern{.5cm} & \kern{.5cm} \rm \mathclap{V} \kern{.5cm} & \kern{.5cm} \rm \mathclap{E} \kern{.5cm} \\ \hline\hline \rm L & \rm L & \rm K & \rm V & \rm E \\ \hline \rm K & \rm K & \rm V & \rm E & \rm \mathclap{KL} \\ \hline \rm V & \rm V & \rm E & \rm \mathclap{KL} & \rm \mathclap{KK} \\ \hline \rm E & \rm E & \rm \mathclap{KL} & \rm \mathclap{KK} & \rm \mathclap{KV} \\ \end{array}$$

Its meaning is:

L+L=LL+L=LL+K=KL+K=KL+V=VL+V=VL+E=EL+E=E

K+L=KK+L=KK+K=VK+K=VK+V=EK+V=EK+E=KLK+E=KL

\cdots

E+E=KVE+E=KV

From these rules, we can deduce: L=0L=0K=1K=1V=2V=2E=3E=3.

We can also determine that the table represents base 44 addition.

Input Format

The first line contains an integer nn (3n93 \le n \le 9) representing the number of rows.

The following nn lines each contain nn strings separated by spaces.

Let si,js_{i,j} denote the string in row ii and column jj. The testdata guarantees s1,1=+s_{1,1}=\texttt{+}, si,1=s1,is_{i,1}=s_{1,i}, si,1=1|s_{i,1}|=1, and si,1sj,1s_{i,1} \ne s_{j,1} (iji \ne j).

It is guaranteed that there is at most one solution.

Output Format

On the first line, output which number each letter represents, in the format like: L=0 K=1 \cdots sorted in the given letter order. Different letters must represent different digits.

On the second line, output the base of the addition.

If it is impossible to form a valid addition table, output ERROR!.

5
+ L K V E
L L K V E
K K V E KL
V V E KL KK
E E KL KK KV

L=0 K=1 V=2 E=3
4

Hint

NOIP 1998 Senior Problem 3.

Translated by ChatGPT 5