如果你已经有x和y的mod A,为什么不使用它们呢?像,
如果
x = int_x*A + mod_x
y = int_y*A + mod_y
然后
(x*y)%A = ((int_x*A + mod_x)(int_y*A + mod_y))%A = (mod_x*mod_y)%A
mod_x*mod_y
要小很多,对不对?
编辑:
如果你正在努力寻找模WRT像10e11
大一些,我想你将不得不使用另一种方法。不过,虽然没有真正有效的,像这样的工作
const int MAX_INT = 10e22 // get max int
int larger = max(mod_x, mod_y) // get the larger number
int smaller = max(mod_x, mod_y)
int largest_part = floor(MAX_INT/smaller)
if (largest_part > larger):
// no prob of overflow. use normal routine
else:
int larger_array = []
while(largest_part < larger):
larger_array.append(largest_part)
larger -= largest_part
largest_part = floor(MAX_INT/smaller)
// now use the parts array to calculate the mod by going through each elements mod and adding them etc
如果你了解这个代码和安装程序,你应该能够找出休息
我使用它们,而是在一个糟糕的情况下,他们可以如10^11和10^10,在这种情况下,它们的乘法溢出。 我尝试以下: 'LL big_num_mult(LL的x,LL Y) { \t如果(!Y = 0 && X> C/Y) \t { \t \t LL H [4],解析度= 0; \t \t h [0] = x >> 48; \t \t h [1] =(x&0xFFFFFFFFFFFF)>> 32; \t \t h [2] =(x&0xFFFFFFFF)>> 16; \t \t h [3] =(x&0xFFFF); \t \t对(INT I = 0; I <4;我++) \t \t { \t \t \t RES =(RES + Y * H [I])%A; \t \t} \t \t return res; \t} \t return(x * y)%A; }' – ddsLeonardo
忘了说我实际编辑过,以便它可以像你说的那样使用大量数字。 – xcorat