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

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

I recently came across this problem while solving E1 of Codeforces Round 967 (Div. 2)

My original code used (a+=b)%p; and it worked perfectly with small test cases, but it gave WA when the test cases get larger. After i changed it to a =(a+b)%p; the code got accepted.

I'm a bit frustrated because it took me until after the contest was finished that i found the dumb mistake that probably stopped me from getting the Candidate Master title, so I made this post to discuss the differences between those 2 syntax to help people avoid the same mistake as me (and mostly just for me to vent T^T )

Here's my original code (Please don't pay attention to the dumb variable names hehe)

My Code

It only worked on test 1, and failed test 2. However, when i changed this line, it worked perfectly

//for(int j = 2;j<=k;j++) (dp2[i][j]+=dp2[i][j-1])%p;
for(int j = 2;j<=k;j++) dp2[i][j]=(dp2[i][j]+dp2[i][j-1])%p;

Can anyone explain why this single line broke my code? :(

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

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

Try for(int j = 2;j<=k;j++) (dp2[i][j]+=dp2[i][j-1])%=p;. Notice the equals sign after the mod ;)

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

    Thanks it works now, but why does the one without the equal sign work with small numbers?

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

      That's because the += operator goes, through, but the % operator doesn't do anything; its result is not assigned to anything.

      • »
        »
        »
        »
        3 часа назад, # ^ |
          Проголосовать: нравится -7 Проголосовать: не нравится

        that is not very sigma

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

Just because.. this shouldn't take mod and it's pretty logical

The result of the % operation is not assigned to anything