lintcode icon indicating copy to clipboard operation
lintcode copied to clipboard

issue about fast power

Open yuansip opened this issue 8 years ago • 0 comments

when n is odd, your code is as below: // odd case // a(bc) mod n = ((a mod n)(bc mod n)) mod n // --> bc mod n = ((b mod n)(c mod n)) mod n // hMs: half Multiply single long hMs = ((half % b) * (a % b)) % b; return (int)((half * hMs) % b); But I think 'long hMs = (half * (a % b)) % b;' is the right statement. (a^(n/2) * a^(n/2) * a) mod b = ((a^(n/2) mod b) * ((a^(n/2) * a ) mod b)) mod b = (half * (((a^(n/2) mod b) * (a mod b)) mod b) mod b = (half * ((half * (a mod b)) mod b)) mod b

yuansip avatar Dec 30 '16 09:12 yuansip