This is the code I\'m using for calculating (n^p)%mod
. Unfortunately, it fails for large values of mod
(in my case mod = 10000000000ULL
) w
One of the possible issues here seems to be that when you do (a*b)%c
, the a*b
part itself could overflow, resulting in a wrong answer. One way to work around that is to use the identity that
(a*b)%c
is equivalent to
(a%c * b%c)%c
This will prevent overflows in the intermediate multiplications as well.