Cod sursa(job #1008761)

Utilizator andreiblaj17Andrei Blaj andreiblaj17 Data 11 octombrie 2013 19:39:25
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include <iostream>
#include <fstream>
#define modulo 1999999973
#define unint long long

using namespace std;

ifstream in("lgput.in");
ofstream out("lgput.out");

long long n,p;

long long putere(unint baza, unint exp){
    if (exp==1) return n%modulo;
    else {
        long long half=putere(n, int(exp/2));
        
        if (exp % 2 == 0) return ((half%modulo)*(half%modulo))%modulo;
        else return ((((half%modulo)*(half%modulo))%modulo)*(baza%modulo))%modulo;
    }
}

int main(){
    in >> n >> p;

    out << putere(n, p)%modulo << "\n";
    
    return 0;
}