Cod sursa(job #3377896)

Utilizator mchiriacMatei Chiriac mchiriac Data 24 noiembrie 2026 18:15:49
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <fstream>
#include <vector>

using namespace std;

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

    const long long MOD = 1999999973;

    long long n, p;
    fin >> n >> p;

    vector<long long> pows;
    pows.push_back(n % MOD);

    long long ans = 1;
    int i = 1;

    while ((1LL << i) <= p) {
        pows.push_back(
            (pows[i - 1] * pows[i - 1]) % MOD
        );
        i++;
    }

    i--;

    while (i >= 0) {
        if ((1LL << i) <= p) {
            ans = (ans * pows[i]) % MOD;
            p -= (1LL << i);
        }
        i--;
    }

    fout << ans;

    return 0;
}