Блог пользователя backBencher24

Автор backBencher24, история, 4 года назад, По-английски

In contest Codeforces Round 670 (Div. 2) when I submitted my code 92619898 for 1406B - Maximum Product It showed me output for 1st test case as

Output
-120
-12
0
0

and when I tested the same code for the 1st test case on my compiler as well as other online IDE's (CodeChef) compiler with the same version, the output was

Output
-120
-12
0
945

I am much confused about this behaviour and tried a lot but still unable to figure it out, Please help me in this

Теги g++
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +2 Проголосовать: не нравится

The problem can be overflow. While subtracting from something.size() it can overflow. I typecasted all those kind of subtraction to (int)something.size() — 1. Then it passed the first test. Click

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    thanks kenechi

    I tried that solution 92700187 submitting again (by removing mistakes) and It passed all pretest by typecasting that you told above

    but if I remove that it says runtime error 92700090 for 2nd test case, if there is any overflow, so how it is passing all the test cases for a solution 92700187 by doing typecasting only (it should be giving the wrong answer)

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      Try running this

      Code

      The size type variable doesn't support negative values so when you subtract something bigger from it, it prints arbitrarily large value.

»
4 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

In that particular test case, the value of your l1 variable becomes -1, thus causing undefined behavior.