Cod sursa(job #1524899)
Utilizator | Moise Andrei Moise_Andrei | Data | 14 noiembrie 2015 15:38:57 |
---|---|---|---|
Problema | Ridicare la putere in timp logaritmic | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.59 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("lgput.in");
ofstream out("lgput.out");
const int mod = 1999999973;
int lgput(int n, int p) {
if(p == 1)
return n % mod;
//if(p == 0)
// return 1;
if(p % 2 == 0) {
int x = lgput(n, p / 2); // calculam n ^ (p/2)
return (1LL * x * x) % mod;
}
else {
int x = lgput(n, p / 2);
return (1LL * ((1LL * x * x) % mod) * n) % mod;
}
}
int main()
{
int n, p;
in >> n >> p;
n = n % mod;
out << lgput(n, p);
return 0;
}