Cod sursa(job #3334744)

Utilizator FistfullOfDollar059Andrei Marin Popa FistfullOfDollar059 Data 19 ianuarie 2026 17:30:32
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ifstream fin("lgput.in");
    ofstream fout("lgput.out");

    int n,p;
    fin>>n>>p;
    int progress = 0;
    const ll m = 1999999973;

    vector<ll> toPow(p+1,0);

    toPow[0] = 1;
    toPow[1] = n%m;
    int cnt;

    for(int i = 2; i <= p; i*=2){
        toPow[i] = (toPow[i/2]*toPow[i/2])%m;
        progress = i;
        cnt = i;
    }





    while(progress < p){
        while(progress+cnt > p){
            cnt/=2;
        }
        toPow[progress+cnt] = (toPow[progress] + toPow[cnt])%m;
        progress += cnt;
    }

    fout<<toPow[p];








    return 0;
}