Cod sursa(job #2195947)

Utilizator Constantin.Dragancea Constantin Constantin. Data 17 aprilie 2018 21:01:30
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MOD = 1999999973;
ll n, p;

ll lgput(ll a, ll b){
    if (!b) return 1;
    if (b == 1) return a%MOD;
    if (b&1) return ((a%MOD)*lgput((a*a)%MOD, b/2))%MOD;
    else return (lgput((a*a)%MOD, b/2))%MOD;
}

ll lgputit(ll a, ll b){
    ll ans = 1;
    while (b){
        if (b&1) ans = (ans * a)%MOD, b--;
        a = (a * a)%MOD;
        b/=2;
    }
    return ans;
}

int main(){
    ifstream cin ("lgput.in");
    ofstream cout ("lgput.out");
    cin >> n >> p;
    cout << lgput(n, p);
    return 0;
}