374 - Big Mod

#include<stdio.h>
int Big_Mod(int base,int power,int mod)
{
    int a,b;
    if(power==0)
    {
        return 1;
    }
    else if(power%2==1)
    {
        a=base%mod;
        b=(Big_Mod(base,power-1,mod))%mod;
        return (a*b)%mod;
    }
    else if(power%2==0)
    {
        a=(Big_Mod(base,power/2,mod))%mod;
        return (a*a)%mod;
    }
}
int main()
{
    long long int a,base,power,mod;
    while(scanf("%lld%lld%lld",&base,&power,&mod)==3)
    {
        a=Big_Mod(base,power,mod);
        printf("%lld\n",a);
    }
    return 0;
}

0 comments: (+add yours?)