Hello!
I am got stuck in understanding the concept of modular Exponentiation. When I need this and how this works.
Suppose I am calling the power function as : power(2,n-1).
How the loops will be executed for say n=10 and also the time and space complexity for the below problem
#define m 1000000007
unsigned long long int power(unsigned long long int x, unsigned long long int n){
unsigned long long int res = 1;
while(n > 0){
if(n & 1){
res = res * x;
res = res % m;
}
x = x * x;
x= x % m;
n >>= 1;
}
return res;
}