Cod sursa(job #1646286)

Utilizator theodor1289Theodor Amariucai theodor1289 Data 10 martie 2016 15:39:21
Problema Ridicare la putere in timp logaritmic Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.47 kb
#include <fstream>
#define MOD 1999999973
using namespace std;
ifstream fin("lgput.in");
ofstream fout("lgput.out");
long long a, b, n, k;

int main()
{
    fin>>n>>k;
    a=n;
    if(k%2==0)
        b=n, k--;
    else
        b=1;

    while(k>1)
    {
        if(k%2==0)
        {
            a=(a*a)%MOD;
            k/=2;
        }
        else
        {
            b=(b*n)%MOD;
            k--;
        }
    }

    fout<<a*b%MOD;
    return 0;
}