So when I output the first results like this:
std::cout << std::fixed << x << std::endl << y << std::endl;
I see this:
2138415301692701661114266637060519453227273059369895888628790658837784821760.000000
206.000000
when I used this number above for x
s value like so:
double y = fmod(2138415301692701661114266637060519453227273059369895888628790658837784821760.000000, 221);
then I get the result of 206
for y
from the first example, the main problem is that you are hitting limit with IEEE double.
Update
This algorithm for modular power:
template <typename T>
T modpow(T base, T exp, T modulus) {
base %= modulus;
T result = 1;
while (exp > 0) {
if (exp & 1) result = (result * base) % modulus;
base = (base * base) % modulus;
exp >>= 1;
}
return result;
}
that I found from here will give you the correct result.