Cod sursa(job #146683)

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

#define rest 1999999973

ifstream f("lgput.in");
ofstream g("lgput.out");

long long res, n, p;

int main()
{
    f>>n>>p;
    res=1;
    while(n){
        if( n%2==0 ){
            res=(res*p)%rest;
            n--;
        }
            else{
                p=(p*p)%rest;
                n=n/2;
            }
    }

    g<<res<<'\n';
    g.close();
    return 0;
}