Large Number Arithmetic
Prerequisites: Integer Ticks and Price Representation, Floating-Point Representation
Suppose a data format stores exact counters with far more digits than any built-in type holds. Reading such a field into a fixed-width type would overflow. Implement exact addition, subtraction, and multiplication directly on decimal strings.
| Type | Holds integers exactly up to |
|---|---|
int64, for money in Integer Ticks and Price Representation | about 9.2 × 10^18 (19 digits) |
unsigned __int128, for products there that could outgrow 64 bits | about 3.4 × 10^38 (39 digits) |
double, per Floating-Point Representation | 2^53, about 9.0 × 10^15; larger integers are rounded |
What to implement
compute(op, a, b)returns the exact result as a string.- On stdin, the first line holds
op, the second line the operanda, and the third line the operandb. The output is the one linecomputereturns. - Both operands are non-negative and canonical: only the digits
0to9, with no leading zeroes, except that zero itself is written0. - The result must be canonical too, so
1000000 - 1returns999999, not0999999. - There are three parts. Each adds an operation. One solution must pass all fourteen tests.
op | Returns |
|---|---|
ADD | a + b |
SUB | a - b (Part 2) |
MUL | a * b (Part 3) |
Work from the digits
- Do not convert a whole operand to a built-in arbitrary-precision integer, such as Python's
int, and do not use a big-number library. - Converting one character to its digit value, with
ch - '0'in C++ orord(ch) - ord("0")in Python, is the intended building block.
Part 1: Add
When op is ADD, return a + b.
Limits
- Each operand has between 1 and 10^5 digits.
Tests 1–3 and 11 cover this part. Test 11 adds two 480-digit numbers.
Part 2: Subtract decimal strings
The format must also support counters that decrease.
- When
opisSUB, returna - b. - Borrow directly across the decimal digits.
- Return zero as
0.
Limits
- The input guarantees
a >= b, so the result is never negative. - The operands have the same length limits as in Part 1.
Tests 4–6 and 12 cover this part. Test 12 borrows across 469 places.
Part 3: Multiply decimal strings
When op is MUL, return the exact product a * b, again without converting either complete operand to an integer.
Limits
- Each operand has at most 2,000 digits.
Tests 7–10, 13, and 14 cover this part.
- The product in test 10 has 36 digits, beyond a 64-bit integer.
- Test 13 multiplies two 405-digit numbers, far beyond even
__int128. - In test 14 every digit is 9, so the middle place collects 405 products of 81, a sum of 32,805 that overflows a signed 16-bit accumulator (maximum 32,767).
ADD 12345678901234567890 987654321
12345678902222222211
a is about 1.2 × 10^19, already above the int64 limit of about 9.2 × 10^18, so even reading it into a long long overflows.
Working from the rightmost column: 0 + 1 = 1. Then 9 + 2 = 11 writes 1 and carries 1. Each of the next seven columns sums to 12 with the carry, writing 2 and carrying 1.
In the tenth column b has run out, and 1 + 1 = 2 absorbs the carry. The remaining ten digits of a copy down unchanged.