Quote:
Can't understand what the line modulo 10^9 + 7 wants to convey;
It protects you from overflow of
64
-bit integers, because:
(A * B) mod C = (A mod C * B mod C) mod C
(see, for instance,
Modular multiplication>[
^]).
Try
#include <iostream>
#include <cstdint>
using namespace std;
uint32_t powermod(uint32_t a, uint32_t b)
{
constexpr uint32_t MOD = 1E9+7;
uint64_t result = 1;
while ( b--)
{
result *= a;
result %= MOD;
}
return static_cast<uint32_t>(result);
}
int main()
{
uint32_t a[][2]{ {3, 4}, {2, 8}, {123, 123} };
for ( auto p : a)
{
cout << "powermod(" << p[0] << ", " << p[1] << ") = " << powermod(p[0],p[1])<< "\n";
}
}