Cod sursa(job #3302575)

Utilizator 13wannabedevBenczik Roberto-Patrik 13wannabedev Data 8 iulie 2025 23:21:40
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <iostream>
#include <vector>
#include <fstream>
#define MOD 1999999973
using namespace std;

ifstream f("lgput.in");
ofstream g("lgput.out");

int pow(int a, int b) {
    int r = 1;
    for (int i = 0; i < b;i++) {
        r *= a;
    }
    return r;
}

unsigned long long fast_pow(int a, int b) {
    if (b % 2 == 0) {
        return pow(a * a, b / 2);
    }
    else {
        return a * pow(a * a, ((b - 1) / 2));
    }
}

// ! impar: a^b = a * pow(a*a,(b-1)/2)
// ! par: a^b = pow(a*a,b/2)
// ! 2^4 = (2*2)^2= 4^2 = pow(16,1) = 16 * pow(16*16, 0)
// ! 2^7 = 
// ! 2^7 = 2 * 2^6 = 
int main(void) {
    unsigned long long n, p;
    f >> n >> p;
    unsigned long long r = fast_pow(n, p);
    g << r % MOD;
    return 0;
}