递归:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long int ll;
ll pow(ll x,ll y,ll p)
{
if(y==0)
return 1;
else
{
ll res=pow(x,y>>1,p);
res=res*res%p;
if(y&1)
{
res=res*x%p;
}
return res%p;
}
}
int main()
{
ll x,y,p;
while(cin>>x>>y>>p)
{
ll ans=pow(x,y,p);
cout<<ans<<endl;
}
}
非递归:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long int ll;
ll pow(ll x,ll y,ll p)
{
ll r=1,base=x;
while(y)
{
if(y&1)
{
r=r*base%p;
}
base=base*base%p;
y>>=1;
}
return r;
}
int main()
{
ll x,y,p;
while(cin>>x>>y>>p)
{
ll sum=pow(x,y,p);
printf("%lld\n",sum);
}
}