Cod sursa(job #2776930)

Utilizator andcovAndrei Covaci andcov Data 21 septembrie 2021 17:21:19
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
//
// Created by Andrei Covaci on 03.09.2021.
// https://infoarena.ro/problema/ciur
//

#include <fstream>
#include <iostream>
#include <vector>
#include <map>

#define INPUT "lgput.in"
#define OUTPUT "lgput.out"
//#define INPUT "input.in"
//#define OUTPUT "output.out"

using namespace std;


pair<long long, int> read() {
    ifstream in(INPUT);

    int a, b;
    in >> a >> b;

    in.close();
    return pair<long long, int>(a, b);
}

long long solve(pair<int, int> p) {
    long long a = p.first;
    int b = p.second;

    long long s = 1, exp = 0;

    map<int, long long> cache;
    cache[1] = a;
    cache[2] = a * a;
    cache[3] = a * a * a;
    cache[4] = cache[2] * cache[2];
    cache[5] = cache[2] * cache[3];
    cache[6] = cache[3] * cache[3];

    while(exp != b) {
        int delta = b - exp;
        pair<int, long long> curr;
        for(auto e : cache) {
            if (e.first > delta) {
                break;
            }
            curr = e;
        }

        s *= curr.second;
        exp += curr.first;
    }

    return s % 1999999973;
}

void print(int n) {
    ofstream out(OUTPUT);

    out << n;

    out.close();
}

int main() {
    auto nums = read();
    auto res = solve(nums);
    print(res);

    return 0;
}