Cod sursa(job #1223951)

Utilizator borcanirobertBorcani Robert borcanirobert Data 29 august 2014 11:41:23
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>
#include <cmath>
using namespace std;

ifstream fin("lgput.in");
ofstream fout("lgput.out");

const long long MOD = 1999999973;
long long a, b;

void Putere();

int main()
{
    fin >> a >> b;

    Putere();

    fin.close();
    fout.close();
    return 0;
}

void Putere()
{
    long long p = 1;
    bool m2 = false;

    if ( b % 2 == 0 )
        b--, m2 = true;

    while ( b > 0 )
    {
        if ( b % 2 )
        {
            p *= a;
            p %= MOD;
            b--;
        }
        if ( !b ) break;
        p = p * p;
        p %= MOD;
        b /= 2;
    }

    if ( m2 )
        p *= a;

    p %= MOD;

    fout << p << '\n';
}