【发布时间】:2012-08-28 22:21:39
【问题描述】:
有没有办法构建例如(853467 * 21660421200929) % 100000000000007 没有 BigInteger 库(注意每个数字都适合 64 位整数但乘法结果不适合)?
这个解决方案似乎效率低:
int64_t mulmod(int64_t a, int64_t b, int64_t m) {
if (b < a)
std::swap(a, b);
int64_t res = 0;
for (int64_t i = 0; i < a; i++) {
res += b;
res %= m;
}
return res;
}
【问题讨论】:
-
一方面,我建议摆脱 Microsoft 扩展并使用
int64_t。 -
看起来在这种情况下你可以作弊,因为你不关心大于参数
__int64 m(或uint64_t对于那些赞成它的人)的任何东西,因此你只能处理64-位类型。 -
你读过Montgomery reduction算法吗?
-
@ildjarn:不,不知道,谢谢你的链接!
-
有趣,这在 x64 汇编中是微不足道的。