Cod sursa(job #146687)

Utilizator igorPirnau Igor igor Data 1 martie 2008 23:38:17
Problema Ridicare la putere in timp logaritmic Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.46 kb
#include<stdio.h>

#define rest 1999999973

long long res, n, p;

int main()
{
    freopen ("lgput.in", "r", stdin);
    freopen ("lgput.out", "w", stdout);
    scanf("%lld %lld", &n, &p);
    res=1;
    while(n){
        if( n%2==0 ){
            res=(res*p)%rest;
            n--;
        }
            else{
                p=(p*p)%rest;
                n=n/2;
            }
    }

    printf("%lld\n", res);
    g.close();
    return 0;
}