1.

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS11
* Problem: 374 - Big Mod
* Link: http://uva.onlinejudge.org/external/3/374.pdf
* @author Mitterreiter Christian
* @author Posselt Christian
* @version 1.0, 03/23/2011
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 0.124
*/

import java.util.Scanner;

public class Main {

public static long bigmod(long b, long n, long m) {
long temp = 0;
if(n==0) return 1;
if(n==1) return b;

if((n%2)!=0) return ((bigmod(b,n-1,m)%m) * (b % m))% m;
temp = bigmod(b, n/2, m);
return ((temp % m) * (temp % m))% m ;

}

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
long b = 0;
long p = 0;
long m = 0;

while(sc.hasNextLong()) {

//Zahlen einlesen
b = sc.nextLong();
p = sc.nextLong();
m = sc.nextLong();

if(b==0) {
System.out.printf("0\n");
}
else {
//Ausgabe und Auswertung
System.out.printf("%d\n", bigmod(b,p,m));
}
}
}
}



--------------------------------------------------------------------------------------

1.

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, WS10/11
* Problem: ##374## Big Mod
* Link: http://uva.onlinejudge.org/external/3/374.pdf
*
* @author Felix Dietrich
* @version 1.0, 14.10.2010
*
* Method : BigInteger
* Status : Accepted
* Runtime: 0.112
*/

import java.math.BigInteger;
import java.util.Scanner;

public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

while (sc.hasNext())
{
int B = sc.nextInt();
int P = sc.nextInt();
int M = sc.nextInt();

// calculate R = B^P mod M
int multiplier = 1;

BigInteger R = BigInteger.valueOf(B).modPow(BigInteger.valueOf(P), BigInteger.valueOf(M));

System.out.println(R.toString());
}
}

}




2.

/**
* FWP: Ausgewaehlte Probleme aus dem ACM
*
* Problem: 374 - Big Mod
* Accepted: 0.120
* @author Evgeni Pavlidis

*/

import java.io.*;
import java.util.*;

class Main {

/**
* Recursively calculates the b^p mod m
*/
public static long exp(long b,long p,long m)
{
if(p==0)
return 1;

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

long tmp = exp(b, p / 2, m) % m;
tmp = (tmp * tmp) % m;

if(p % 2 == 0)
return tmp;

return tmp * b % m;
}


public static void main(String...args)
{
long b,p,m;
Scanner scanner = new Scanner(System.in);

while(scanner.hasNextInt())
{
b = scanner.nextInt();
p = scanner.nextInt();
m = scanner.nextInt();

System.out.println(exp(b,p,m));
}
}
}