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://codeforces.net/contest/1828/submission/205934626
Here is same submission for which got ac by changing ans = ans* something
Both of them are already accepted??? you have provided the same link twice
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.
oh Now I got it , Thank you very much
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!!
i think you should multiply first and then take the mod in different lines maybe that would work
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
int ans = 85 ; int something = 445 ; ans *= something % 100 ; cout<< ans<< el;
OUTPUT : 3825
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.
Thanks
ㅤ
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 operationans < 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 guaranteeans < 1e9 + 7
.This (
ans = ans * (max ( 0LL , k - sub)) % MOD;
) would have worked even ifans
was declared as an integer, because the*
operation is performed between all
(result ofstd::max
) and an int (ans
) that will be casted toll
. The result of%
will also bell
, but since it is lower than1e9 + 7
, it will be casted correctly back toint
when attributing it back toans
.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;
You should use
(ans *= k) %= MOD;
.That's the correct code for multiplication and moduling.