Cod sursa(job #2952382)

Utilizator coso2312Cosmin Bucur coso2312 Data 9 decembrie 2022 08:18:25
Problema Ridicare la putere in timp logaritmic Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 kb
#include <iostream>
#include <fstream>
using namespace std;

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

int main() {
    long long x, n;
    int k = 1999999973;
    fin >> x >> n;
    if (n == 0) {
        fout << 1 % k;
    } else if (n % 2 == 0) {
        for (int i = 1; i <= n / 2; ++i) {
            x *= x;
        }
        fout << x % k;
    } else {
        int x_cpy = x;
        for (int i = 1; i <= n - 1; ++i) {
            x *= x;
        }
        fout << (x_cpy * x) % k;
    }
    return 0;
}