Cod sursa(job #3245131)

Utilizator inacioataCioata Ana Irina inacioata Data 27 septembrie 2024 17:14:41
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("lgput.in");
ofstream fout("lgput.out");


/**
2 ^ 27 = 2 ^ 1 + 2 ^ 2 + 2 ^ 8 + 2 ^ 16

                      43210
27 = 16 + 8 + 2 + 1 = 11011
*/

long long ExpoLog(long long a, long long n, int mod)
{
    int p = 1;
    while(n > 0)
    {
        if(n % 2 == 1) p = p * a % mod;
        n /= 2;
        a = a * a % mod;
    }
    return p;
}

int main()
{
    long long a, n;
    fin >> a >> n;
    fout << ExpoLog(a, n, 1999999973) << "\n";
    return 0;
}