Cod sursa(job #2952387)

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

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

int p1(long long x, long long x_cpy, long long n, long long i) {
    if (i == n / 2) {
        return x * x;
    }
    p1(x * x_cpy, x_cpy, n, i + 1);
}

int p2(long long x, long long x_cpy, long long n, long long i) {
    if (i == n - 1) {
        return x_cpy * x;
    }
    //cout << "x = " << x << "\n";
    p2(x * x_cpy, x_cpy, n, i + 1);
}

int main() {
    long long x, n;
    long long k = 1999999973;
    fin >> x >> n;
    if (n == 0) {
        fout << 1 % k;
    } else if (n % 2 == 0) {
        long long x_cpy = x;
        fout << p1(x, x_cpy, n, 1) % k;
    } else {
        long long x_cpy = x;
        fout << p2(x, x_cpy, n, 1) % k;
    }
    return 0;
}