A Plus B (Hard)

View as PDF

Submit solution

Points: 12
Time limit: 2.5s
Memory limit: 64M

Problem type
Allowed languages
ALGOL68, Assembly, Brain****, C, C++, COBOL, Forth, Fortran, Java, Lua, Text, Turing

Xyene's teacher realized that he was cheating, and was using the code you wrote to save his marks. So, the math teacher decided that he will mess up your program by using numbers larger than 2^{64}. In fact, he will give problems involving addition of 100\,000-digit numbers as punishment. However, Xyene is once again on top — he has promised you a "reward" if you help him again. You suspect it might not be anything more than 12 points, but you still have your hopes up...

Input Specification

The first line will contain an integer N, the number of addition problems Xyene needs to do. The next N lines will each contain two space-separated integers with up to 100\,000 digits in decimal, the two integers Xyene needs to add. N will never be greater than 50.

Warning: the test cases are a lot harder than the sample.

Output Specification

Output N lines, the solutions to the addition problems in order.

Sample Input

1 1
226077045628835347875 -572260769919042128358
-803119834418378628674 236083700054616110639
-435599336891761067707 451767479989987922363

Sample Output



  • 0
    PhoenixCoolIce  commented on April 16, 2024, 3:30 p.m.

    No Big integer = pain

  • -2
    fbain1  commented on April 7, 2022, 3:37 p.m.

    I'm pretty sure half the top solutions are just bigint classes copied and pasted from other sources. Like, if it was written just for this problem, why would they implement abs, and never use it? Or multiply?

  • -8
    dxke02  commented on Jan. 29, 2021, 10:22 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.

    • 11
      WilliamWu277  commented on Jan. 30, 2021, 2:52 a.m. edited

      String ans = "This is " + "slow";

  • 10
    hxxr  commented on Aug. 22, 2019, 3:51 a.m.

    Why is BF not an allowed language for this question?

    The only practical way to solve even the original "A Plus B" in BF would require you to implement your own algorithm for addition of arbitrary-length integers.

    • 8
      c  commented on Sept. 21, 2019, 6:48 p.m.

      BF has been enabled.

  • -13
    LunarCoffee  commented on Nov. 5, 2018, 2:26 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.

    • 21
      Relativity  commented on Nov. 5, 2018, 2:35 p.m.

      BigInteger is disabled

  • 8
    DerekZhang  commented on Dec. 24, 2017, 5:17 a.m.

    Not sure why I'm WA on two cases. On case #2, the input is 7808787 -2084742, and it WA because it's outputting 5403285 (wrong), but when I run the same input locally, it outputs 5724045 (right). I have no idea why this is happening. I thought it was because some variables weren't being reset, but all of the variables used in calculations are local. Any ideas?

  • -15
    SBOUIFERS  commented on Oct. 29, 2017, 12:53 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.

    • 8
      wleung_bvg  commented on Oct. 29, 2017, 1:47 p.m. edited

      Unfortunately, using a double is not sufficient enough (nor accurate enough) to store a 100\,000 digit number. Try finding another way you can store the digits (it may involve creating your own data structure).

      • 9
        Ninjaclasher  commented on Oct. 29, 2017, 2:19 p.m.

        cough cough string cough cough

  • 2
    rsylshzxdkh  commented on Oct. 21, 2017, 6:55 p.m.

    Can someone tell me what I'm doing wrong? Or at least give a case to test?

    • 7
      TypicalToxic  commented on Oct. 22, 2017, 9:25 p.m. edited


      99 99
      -99 99
      99 -99
      100 99
      -100 99
      100 -99

  • -3
    aeternalis1  commented on Sept. 15, 2017, 12:07 p.m.

    Can the two integers have leading zeros?

    • -1
      Pleedoh  commented on Sept. 15, 2017, 3:06 p.m.


  • -6
    RourisGaSuki  commented on March 30, 2017, 3:16 p.m. edit 2

    This comment is hidden due to too much negative feedback. Show it anyway.

    • 5
      Xyene  commented on March 30, 2017, 5:01 p.m. edit 2

      The difficulty of this in Python is the same as the regular aplusb (i.e. not worth 12 points).

  • -32
    trollakatroll  commented on Jan. 21, 2017, 11:15 p.m. edited

    This comment is hidden due to too much negative feedback. Show it anyway.

    • 31
      Xyene  commented on Jan. 22, 2017, 12:00 a.m. edited

      Because copy/pasting code from Java's BigInteger implementation was never intended as a correct solution to this problem. BigInteger is explicitly disallowed, so why would copypasta of it be any different?

  • 16
    Xyene  commented on Aug. 28, 2016, 9:36 p.m. edited

    Java is now allowed for this problem, though use of the BigInteger and BigDecimal classes is disallowed.

    • 2
      max  commented on Oct. 8, 2017, 9:39 a.m. edited