1. Erste Version: C, Evgeni Pavlidis


/**
* ACM programming Contest WS 08/09
* UVa Status: accepted
* Run Time: 0.000
* @author Evgeni Pavlidis evgenipavlidis@yahoo.de
*/
#include <stdio.h>

int main()
{
int b,p,m;

while(1)
{
scanf("%d\n%d\n%d\n\n", &b, &p, &m);
printf("%d\n", mulModulo(b,p,m));

if(feof(stdin))
return 0;
}
}

int mulModulo(int b, int p, int m)
{
if(p == 0 || m == 1)
return 1;

if(b == 0)
return 0;

if(b == 1 || p == 1)
return b % m;

if(p % 2 == 0)
{
int aux = mulModulo(b, p/2, m);
return aux * aux % m;
}
else
return (mulModulo(b, p-1, m) * (b%m)) % m;
}