#include
#include
using namespace std;
int main() {
long int a=0,b=0,c=0;
int i=0;
int last_bit = 0;
long int ans=1;
cin>>a>>b>>c;
while(b)
{
last_bit =(b&1);
ans*= pow(a,(pow(2,i)*last_bit));
b=b>>1;
i++;
}
ans = ans%c;
cout<<ans;
return 0;
}
Modular Exponentiation falling test cases
@shivansh.sm1 Using inbuilt function pow will not work for large test cases.
he constraints given are 1<=a,b,c<=100000. So (a^b) can reach a very large value which cannot be handled by the datatype. This is why your code is giving wrong answer for many test cases. You have to handle those cases when a^b reaches a very large value.
So refer this video: