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));
}
}
}