简体   繁体   English

如何找到以100000007为模的大数乘法

[英]how to find muliplication of large numbers modulo 100000007

As we know 1000000007 is a large prime number. 我们知道1000000007是一个大质数。 How can I find multiplication of two large numbers modulo 1000000007 如何找到两个大数的模1000000007的乘法

For example if I want to find 78627765*67527574 mod 1000000007, how can I do it. 例如,如果我想找到78627765 * 67527574 mod 1000000007,该怎么办。

At least if anyone tell me the procedure I shall try 至少有人告诉我我会尝试的程序

Note: pls let me know the solution with primitive datatypes like int,long or long long Thanks in advance 注意:请让我知道具有int,long或long long等原始数据类型的解决方案,谢谢

Modulo chaining works with reasonable numbers that are pushing the limits of your numerical comp-space: 模链具有合理的数量,这些数量正在推高数字比较空间的极限:

(A * B) % C == ((A % C) * (B % C)) % C.

The proof for this is pretty straight forward and there are literally thousands of examples on cryptography websites all over the world. 证明很简单,全世界加密网站上确实有成千上万个示例。 A simple sample: 一个简单的示例:

(7 * 8) % 5 = 56 % 5 = 1 (7 * 8)%5 = 56%5 = 1

and

((7 % 5) * (8 % 5)) % 5 = (2 * 3) % 5 = 6 % 5 = 1

I hope this helps. 我希望这有帮助。 Obviously when A and B are already pushed to your top-end platform limits and are still smaller than C, it gets pointless, but it can be very handy when this is not the case (Ie when A > C and/or B > C). 显然,当A和B已经达到您的高端平台限制,并且仍然小于C时,它就变得毫无意义,但是当情况并非如此时(例如,A> C和/或B> C )。

Since this looks like a homework or context problem, I will only give hints. 由于这看起来像是作业或上下文问题,因此我仅给出提示。

If you know x%m and y%m, how can you find (x+y)%m? 如果您知道x%m和y%m,如何找到(x + y)%m? If you know x%m, how can you find (2x)%m? 如果您知道x%m,怎么找到(2x)%m?

Since you want to find (a*b)%m, is there a way you can decompose b so that you can use the above two hints? 由于您想找到(a * b)%m,有没有一种方法可以分解b,以便可以使用上面的两个提示?

Why don't you want to use 64-bit arithmetic for that ? 您为什么不想为此使用64位算术? Of course this only works if the operands being multplied do not exceed 32 bits each (but this can also be fixed). 当然,这仅在被乘法的操作数每个不超过32位的情况下才有效(但这也可以是固定的)。 Consider: 考虑:

typedef unsigned long long uint64;
uint64 m = 1000000007UL;
uint64 r = (uint64)a * (uint64)b;
r = r % m; // get the residue

One can also optimize it to avoid '%' which might be expensive: 您还可以对其进行优化,以避免可能昂贵的'%':

double inv = 1.0 / 1000000007UL; // precompute inverse
uint64 r = (uint64)a * (uint64)b;
uint64 rf = (uint64)floor((double)a * (double)b * inv); // floor(a * b / m) 
r = r - rf * m; //  residue

Note that the second method may require some playing around with accuracy. 请注意,第二种方法可能需要一定的准确性。 You can also use 'long double' instead 您也可以使用“ long double”代替

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

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