UVA 374 Big mod Solution
Solve in C:
#include<stdio.h>
#include<math.h>
long long int big_mod(long long int base,long long int power,long long int mod)
{
if(power==0)
return 1;
else if(power%2==1)
{
long long int p1=(base%mod);
long long int p2=(big_mod(base,power-1,mod))%mod;
return (p1*p2)%mod;
}
else if(power%2==0)
{
long long int p1=(big_mod(base,power/2,mod))%mod;
return (p1*p1)%mod;
}
}
int main()
{
long long int b,p,m,r,q1,q2,power,base,mod;
while(scanf("%lld%lld%lld",&b,&p,&m)!=EOF)
{
long long r=big_mod(b,p,m);
printf("%lld\n",r);
}
}
No comments