jueves, 8 de diciembre de 2011

374 - Big Mod , uva

#include<iostream>
#include<ctime>
#include<cstring>
#include<cstdio>
#define mod 131071
#define db(a) cout << #a << " = " << a << endl
#define db2(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl
using namespace std;
int m;
int power(int n, int p) {
 if (p == 0) return 1;
 if (p == 1) return n;
 if (p & 1) {
  return ((n % m)  * (power(((n % m) * (n % m)) % m, (p - 1) / 2) % m)) % m;
 }
 else {
  return power(((n % m) * (n % m))% m, p / 2);
 }
}
int main() {
 int b, p;
 while (cin >> b >> p >> m) {
  cout << power(b, p) << endl;
 }
 return 0;
}
Click here for a great explanation

No hay comentarios:

Publicar un comentario