Cod sursa(job #3233215)

Utilizator MirceaDonciuLicentaLicenta Mircea Donciu MirceaDonciuLicenta Data 2 iunie 2024 19:55:26
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <iostream>
#include <fstream>

using namespace std;

const long long MOD = 1999999973;

long long power_mod(long long base, long long exponent, long long mod) {
    if (exponent == 0) {
        return 1;
    }
    long long half = power_mod(base, exponent / 2, mod);
    long long half_squared = (half * half) % mod;
    if (exponent % 2 != 0) {
        half_squared = (half_squared * base) % mod;
    }
    return half_squared;
}

int main() {
    ifstream infile("lgput.in");
    ofstream outfile("lgput.out");

    if (!infile || !outfile) {
        cerr << "Error opening file" << endl;
        return 1;
    }

    long long N, P;
    infile >> N >> P;

    long long result = power_mod(N, P, MOD);
    outfile << result << endl;

    infile.close();
    outfile.close();

    return 0;
}