Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何计算 (a*b)%c 形式的模数?
我想计算两个 int 数的乘法模数,它们几乎处于溢出阶段...
这里 c 也是 int
(a * b) % c == ((a % c) * (b % c)) % c
怎么样((a % c) * (b % c)) % c?根据您的架构,这可能比转换为更大的类型更快或更慢。
((a % c) * (b % c)) % c
您可以转换aand cto long long,因此乘法不会溢出。
a
c
long long
((long long)a * (long long)b) % c