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
It seems that you can't avoid it.
If mod
is 10000000000ULL
, in (a*b)%c
in your program, both a
and b
are smaller than mod so we treat them as 9999999999ULL
, a*b
will be 99999999980000000001
, but unsigned long long
can only express 2^64-1=18446744073709551615 < 99999999980000000001
so your method will be overflow.