Cod sursa(job #2127302)

Utilizator andra_moldovanAndra Moldovan andra_moldovan Data 10 februarie 2018 15:30:33
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>

#define mod1 1999999973

using namespace std;

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

long long RidicareLaPutere(int N, long long x) {
    long long sol = 1;
    while (N) {
        if (N & 1) { //daca N e impar
            N--;
            sol *= x;
        }
        else {
            x *= x;
            x %= mod1;
            N >>= 1; //N = N / 2
        }
        sol %= mod1;
    }

    return sol;
}

inline void Read() {
    int N; long long x;

    fin >> x >> N; //x - baza, N - puterea

    fout << RidicareLaPutere(N, x);
}

int main () {
    Read();

    fin.close(); fout.close(); return 0;
}