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

Автор omkar21_pro, история, 19 месяцев назад, По-английски

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

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

»
19 месяцев назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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

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

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 месяцев назад, # ^ |
      Проголосовать: нравится +3 Проголосовать: не нравится

    oh Now I got it , Thank you very much

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

      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 месяцев назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

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

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

        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 месяцев назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

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

          OUTPUT : 3825

          • »
            »
            »
            »
            »
            »
            19 месяцев назад, # ^ |
            Rev. 3   Проголосовать: нравится -9 Проголосовать: не нравится

            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 месяцев назад, # ^ |
            Проголосовать: нравится 0 Проголосовать: не нравится

          Thanks

»
19 месяцев назад, # |
Rev. 2   Проголосовать: нравится -7 Проголосовать: не нравится

»
19 месяцев назад, # |
  Проголосовать: нравится +34 Проголосовать: не нравится

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 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 месяцев назад, # |
  Проголосовать: нравится -10 Проголосовать: не нравится

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

That's the correct code for multiplication and moduling.