omkar21_pro's blog

By omkar21_pro, history, 19 months ago, In English

In recent codeforces div 2 contest I got wa because of this in problem C — counting orders

I don's understand what is difference between two ,Aren't they both same ?

Here is my submission for which got wa

https://mirror.codeforces.com/contest/1828/submission/205934626

Here is same submission for which got ac by changing ans = ans* something

https://mirror.codeforces.com/contest/1828/submission/205934493

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

| Write comment?
»
19 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Both of them are already accepted??? you have provided the same link twice

»
19 months ago, # |
  Vote: I like it +1 Vote: I do not like it

Learn modular arithmetic properly. While you did ans *= something % MOD; in your WA submission it gets overflow because multiplication operation doing first. In your AC submission you did ans = ans * something % MOD; Here "something" is reducing first then it multiplying. Hope you get it.

  • »
    »
    19 months ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    oh Now I got it , Thank you very much

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

      there's my submission 205933688 i think i have done the same thing but i don't know how it is giving integer overflow to your's!!

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

        i think you should multiply first and then take the mod in different lines maybe that would work

      • »
        »
        »
        »
        19 months ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        I guess when I do ans*= something %M , Modulo operation is not performed only multiplication performed , Bad that I could not solve problem c because of this small error

        Congratulation on being specialist

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

          int ans = 85 ; int something = 445 ; ans *= something % 100 ; cout<< ans<< el;

          OUTPUT : 3825

          • »
            »
            »
            »
            »
            »
            19 months ago, # ^ |
            Rev. 3   Vote: I like it -9 Vote: I do not like it

            Sorry, the previous statement was wrong. This part is correct though.

            If you really want to be careful then you should just do : ans = (ans%mod) * (something%mod); ans %= mod;

            This is because ans and something should both be less than mod when they are being multiplied if this is not the case than : ans > mod, something > mod, ans * something > mod*mod. This means it is > 1e18, which can overflow.

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

          Thanks

»
19 months ago, # |
Rev. 2   Vote: I like it -7 Vote: I do not like it

»
19 months ago, # |
  Vote: I like it +34 Vote: I do not like it

The other comments are wrong. * and % have the same precedence. The left-most operator takes priority. Check this link.

Your line works (ans = ans * (max ( 0LL , k - sub)) % MOD;) because the last operation done is %, so at the end of the operation ans < 1e9 + 7.

This (ans *= (max ( 0LL , k - sub)) % MOD;) doesn't work because *= has a lower priority than %, so at the end of the operation we can't guarantee ans < 1e9 + 7.

This (ans = ans * (max ( 0LL , k - sub)) % MOD;) would have worked even if ans was declared as an integer, because the * operation is performed between a ll (result of std::max) and an int (ans) that will be casted to ll. The result of % will also be ll, but since it is lower than 1e9 + 7, it will be casted correctly back to int when attributing it back to ans.

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

when you have to modulo the value, you can write it as ans*=j%mod, rather you will have to write it as ans=(ans*j)%mod;

»
19 months ago, # |
  Vote: I like it -10 Vote: I do not like it

You should use (ans *= k) %= MOD;.

That's the correct code for multiplication and moduling.