Cod sursa(job #1772025)

Utilizator danielNiculaeDaniel Niculae danielNiculae Data 6 octombrie 2016 14:11:10
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.42 kb
#include "fstream"

using namespace std;

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

long N, P;
const int mod = 1999999973;

int main(void) {
    long res = 1;
    fin >> N >> P;

    while(P) {
        if(P % 2 == 1) {
            res = (res * N) % mod;
            P -= 1;
        }
        if(P >= 2) {
            N = (N * N) % mod;
            P = (P / 2) % mod;
        }
    }

    fout << res;

    return 0;
}