Addition (a+b problem)
Time limit1sMemory limit1024 MB
Add two huge integers given in run-length encoded decimal form and print the sum in the same run-length encoded format.
- Level
Medium6 of 10
- Topics
- Implementation, Math, Simulation, String
- Solved
- No attempts yet
Problem
Write a program that prints the sum of two integers.
Input
Read the following input from standard input.
The input gives the two integers to add, one after the other, in the format below.
- The first line of the data for the first integer contains an integer m (1 ≤ m ≤ 20,000). Of the following m lines, the i-th line contains an integer ai (0 ≤ ai ≤ 9) and an integer li (1 ≤ li ≤ 100,000,000) separated by a space. These mean that the first integer, in decimal notation from the most significant digit, consists of l1 copies of a1, l2 copies of a2, …, lm copies of am. Here a1 ≠ 0 and ai ≠ ai+1 (1 ≤ i ≤ m − 1).
- Immediately after the data for the first integer comes the data for the second integer in the same format.
The values of li do not necessarily fit in 32 bits. You should use a 64-bit data type such as long long. See the technical information for details.
Output
Print the following data to standard output.
Suppose the sum of the two integers, in decimal notation from the most significant digit, consists of L1 copies of A1, L2 copies of A2, …, LM copies of AM (0 ≤ Ai ≤ 9, A1 ≠ 0, Ai ≠ Ai+1, Li = 1). Then the output consists of M + 1 lines: the first line contains the integer M, and the (i + 1)-th line (1 ≤ i ≤ M) contains the integers Ai and Li in that order, separated by a space.
Hint
In this case, the two integers given in the input are 111222233333 and 999988877. Their sum is 112222222210, so the output is as shown above.