简体   繁体   English

如何计算形式的模数(a * b)%c?

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

How to calculate modulus of the form (a*b)%c? 如何计算形式的模数(a * b)%c?

i want to calculate modulus of multiplication of two int numbers where they are almost in the stage of overflow... 我想计算两个int数的乘法模数,它们几乎处于溢出阶段......

here c is also int 这里c也是int

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

What about ((a % c) * (b % c)) % c ? ((a % c) * (b % c)) % c怎么样? Depending on your architecture this could be faster or slower than casting to a bigger type. 根据您的架构,这可能比投射到更大的类型更快或更慢。

您可以将aclong long ,因此乘法不会溢出。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM