Cod sursa(job #1792162)
Utilizator | Data | 30 octombrie 2016 09:20:08 | |
---|---|---|---|
Problema | Ridicare la putere in timp logaritmic | Scor | 10 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.55 kb |
#include <iostream>
#include <fstream>
const int MOD = 1999999973;
int power(int x, int n){
if(n == 0){
return 1;
}
if(n == 1){
return x % MOD;
}
if(n % 2 == 0){
return power(x, n / 2) * power(x, n / 2) % MOD;
}
if(n % 2 == 1){
return x * power(x, (n - 1) / 2) * power(x, (n - 1) / 2) % MOD;
}
}
int main(){
std::ifstream fin("lgput.in");
std::ofstream fout("lgput.out");
int n, p;
fin >> n >> p;
fout << power(n, p);
fin.close();
fout.close();
return 0;
}