#P1581. Prime Base Addition
Prime Base Addition
Prime Base Addition
You are given two numbers represented in a special numeral system. In this system, each digit has a different base. The bases are defined by the sequence of prime numbers in increasing order. Specifically, the rightmost (units) digit is in base \(2\), the next digit to the left is in base \(3\), then base \(5\), then base \(7\), then base \(11\), and so on. The digits of each number are given separated by commas, where the leftmost digit is the most significant digit.
Add the two numbers according to the following procedure:
- Reverse the order of digits in each number so that the units digit comes first.
- Add the corresponding digits along with any carry. For the \(i\)-th digit (starting at \(i=0\) for the units place), use the base equal to the \((i+1)\)-th prime number. That is, \(p_1=2, p_2=3, p_3=5, \ldots\).
- If the sum of the digits and the carry is \(s\), then the resulting digit is \(s \bmod p_{i+1}\) and the new carry is \(\lfloor s/p_{i+1} \rfloor\).
- Continue the addition process for all digits. If a carry remains after processing all digits of both numbers, extend the computation using additional primes as necessary.
For example, consider the addition:
[ 1,0 + 2,1 = 1,0,1 ]
Explanation:
- The units (rightmost) digits: \(0 + 1 = 1\) in base \(2\) (no carry since \(1 < 2\)).
- The next digit: \(1 + 2 = 3\) in base \(3\); we write \(3 \bmod 3 = 0\) and carry \(1\) (since \(3/3 = 1\)).
- An extra digit is produced by the carry: \(1\) in base \(5\), so the digit is \(1\) and there is no further carry.
The final result is 1,0,1
.
inputFormat
The input consists of two lines. Each line contains a number represented by its digits separated by commas. The leftmost digit is the most significant digit. Each digit is guaranteed to be a valid digit in its corresponding prime base (i.e. for the units digit, the digit is in \(\{0,1\}\); for the tens digit in \(\{0,1,2\}\); for the hundreds digit in \(\{0,1,2,3,4\}\); etc.).
outputFormat
Output the sum of the two numbers, with the digits separated by commas. The output should have no extra spaces.
sample
1,0
2,1
1,0,1