Answer the question
In order to leave comments, you need to log in
How to implement such an algorithm?
It is required to implement the binary exponentiation algorithm
std::vector<bool> temp = toBinary(power);
std::vector<long> result;
long S = 1, c = base;
for (auto i = 0; i < temp.size(); ++i)
{
if (temp[i] == 1)
{
S = (S * c) % mod;
std::cout << "S = " << S << std::endl;
}
else
{
c = (c * c) % mod;
std::cout << "c = " << c << std::endl;
}
}
Answer the question
In order to leave comments, you need to log in
uint64_t mod_power(uint64_t a, uint64_t b, uint64_t n) {
uint64_t bit = 0x8000000000000000ui64;
uint64_t result = a % n;
while (bit && (bit & b == 0))
bit = bit >> 1;
while ((bit = bit >> 1))
result = (result * result * ((bit & b) ? a : 1)) % n;
return result;
}
First, long is int32, so if your mod is greater than 2^15, it's better to use long long.
Secondly, the line
S = (S * c) % mod;should be replaced with
c*=base;(if you want to follow the algorithm), in addition, set the condition that temp[i] is not the last bit of the bit representation of the number - otherwise we will once again bring it to the square.
c%=mod;
c*=c;
c%=mod;
long long bp(long long base, long long power)
{
std::vector<bool> temp = toBinary(power);
std::vector<long> result;
long long c = 1;
for (auto i = 0; i<temp.size(); i++)
{
if (temp[i] == 1)
{
c = (c*base)%mod;
}
if(i!=temp.size()-1)
c*=c;
c%=mod;
}
return c;
}
long long binpow(long long base, long long power)
{
if(power==0)
return 1;
if(power%2)
return (binpow(base, power-1)*base)%mod;
long long tmp = binpow(base, power/2);
return (tmp*tmp)%mod;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question