P8870 [ChuanZhi Cup #5 Preliminary] B - Lianzi’s Mechanical Dynamics.

Background

**[The two statements in the Background and the Description are completely equivalent. You may choose to read only one of them.]** Lianzi, who focuses on super-unified physics, knows a lot about the motion of mechanical structures. As shown in the figure below, it is a (highly simplified) schematic diagram of a ternary addition calculator. ![](https://cdn.luogu.com.cn/upload/image_hosting/l6gm0j36.png) A 4-digit ternary integer is labeled from low to high as $x_1, x_2, x_3, x_4$. In other words, this number can be written as $\overline{x_4x_3x_2x_1}_{(3)}$. Place it into these four gears, and the digit pointed to by the arrow is the current value of that position. In this kind of mechanical computer, we use the meshing of gears to connect different digit positions, and use the ratio of different gear radii to determine the base. The radius ratio between all the light-gray small gears in the figure and the larger gears connected by belts is $1:3$. Therefore, when a small gear rotates 1 full turn, the large gear rotates $\dfrac{1}{3}$ turn, which is exactly the angle of one digit. Thus, by controlling the gear radii, we achieve carrying in base $3$. To perform ternary addition, we only need to rotate the corresponding gear by the length corresponding to the digit to be added. Here is an example: $\overline{1021}_{(3)} + \overline{0021}_{(3)} = \overline{1112}_{(3)}$. ![](https://cdn.luogu.com.cn/upload/image_hosting/4kcgnp7j.png) Initially, the gear state is as shown above. ![](https://cdn.luogu.com.cn/upload/image_hosting/dcd66l5v.png) Rotate the first gear by 1 unit length, and it becomes as shown above. ![](https://cdn.luogu.com.cn/upload/image_hosting/oiexg3yw.png) Rotate the second gear by 2 unit lengths, and it becomes as shown above. Read the digits to get the result $\overline{1112}_{(3)}$. --- Now Lianzi has designed the mechanical structure shown below. For the $i$-th gear from left to right, the radius ratio between the light-colored small gear on it and the dark-colored small gear on the $(i+1)$-th gear is $1:(i+2)$. That is, when the $i$-th gear rotates 1 full turn, the $(i+1)$-th gear rotates by exactly the angle of one digit on it. ![](https://cdn.luogu.com.cn/upload/image_hosting/zi1d0qnn.png) Lianzi wants to know: under this special positional representation, given $a, b$, what is the computed result of $a + b$.

Description

The question in the Background can be converted into the following: Given two integers $a, b$ with lengths $n, m$, compute their sum. However, note that $a, b$ use a special positional representation. The final result will also use this representation. Specifically, counting from the least significant digit to the most significant digit, the $i$-th digit uses base $i+1$. In other words, compared with the usual decimal rule “carry 1 when reaching 10”, in this representation the $i$-th digit follows “carry 1 when reaching $i+1$”. In the figure below, the left side shows vertical addition in decimal; the right side shows vertical addition in this special base system. The red plus sign indicates that a carry occurred from the previous digit. ![](https://cdn.luogu.com.cn/upload/image_hosting/px92d6yd.png)

Input Format

- The first line contains two integers $n, m$, representing the number of digits of $a$ and $b$. - The second line contains $n$ integers separated by spaces, describing each digit of $a$ **from most significant to least significant**. - The third line contains $m$ integers separated by spaces, describing each digit of $b$ **from most significant to least significant**.

Output Format

- Output several integers, printing the result of $a + b$ in this special representation **from most significant to least significant**.

Explanation/Hint

For all data, it is guaranteed that $1 \le n, m \le 2 \times 10^5$. Counting from low to high digits, $a_i \in [0, i]$ and $b_i \in [0, i]$. Contestants using Java or Python should pay attention to I/O efficiency. Translated by ChatGPT 5