开发者

How to calculate modulus of the form (a*b)%c?

开发者 https://www.devze.com 2023-03-03 04:47 出处:网络
How to calculate modulus of the form (a*b)%c? i want to calculate modulus of multiplication of two int numbers where they are almost in the stag开发者_StackOverflowe of overflow...

How to calculate modulus of the form (a*b)%c?

i want to calculate modulus of multiplication of two int numbers where they are almost in the stag开发者_StackOverflowe of overflow...

here c is also int


(a * b) % c == ((a % c) * (b % c)) % c


What about ((a % c) * (b % c)) % c? Depending on your architecture this could be faster or slower than casting to a bigger type.


You may cast a and c to long long, so the multiplication won't overflow.

((long long)a * (long long)b) % c
0

精彩评论

暂无评论...
验证码 换一张
取 消