robertlewantest's blog

By robertlewantest, history, 11 months ago, In English

Here are my 2 submissions for the problem — https://codeforces.net/contest/1541/problem/B

243140165 -1 243140452 -2

Only difference between the two is the if condition — in first if(l<r and var==(l+r) and in second if(l<r and var%(l+r)==0 and (var/(l+r))==1)

Ideally both should produce same result but on test 103 it is failing - The test 103 is this

n=4 a=5 2 4 1

its answer should be 1 and indeed its 1 when i run in local or custom invoction but on submit it is giving answer 2 hence WA

Can someone pls help why is behaviour?

  • Vote: I like it
  • +3
  • Vote: I do not like it

»
11 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Learning to debug is something you should master. :) Try searching for debug templates or learn how to stress test.

»
11 months ago, # |
  Vote: I like it 0 Vote: I do not like it

You have undefined behavior in your code. The size of mp is mx+1, i and j might have values mx+1 and you're calling mp[i] and mp[j]. With UB your code might sometimes work and sometimes fail — you got lucky with your other solution. This really doesn't have anything to do with the line of code you changed.

  • »
    »
    11 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks, it was a silly mistake, but still it got accepted only when i changed that line of code, and regarding getting lucky, i submitted multiple times still was getting WA on the first and AC on the changed code

    • »
      »
      »
      11 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Hmm... It might not've been luck in the sense that "every time I submit there is a 10% chance the code passes" but instead, maybe it was that "this small change accidentally made the UB not matter". I honestly don't know. My point is that with UB, you can't be sure if your code will fail or not.