Cod sursa(job #1796389)

Utilizator teo.cons98Constantin Teodor-Claudiu teo.cons98 Data 3 noiembrie 2016 14:30:19
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
ifstream fin("lgput.in");
ofstream fout("lgput.out");

bool A[33];
long long n, p, s = 0, nr = 1;

void citire()
{
    fin>>n>>p;
}

void binar()
{
    long long p1 = p;
    long long x = powl(2, 32);
    for(int i = 32; i >= 0; --i)
    {
        if(p1 >= x)
        {
            A[i] = 1;
            p1 -= x;
        }
        x /= 2;
    }
}

int main()
{
    citire();
    binar();
    nr = n;
    for(int i = 0; i <= 32; ++i)
    {
        if(A[i] == 1)
        {
            s += nr;
            s %= 1999999973;

        }
        nr *= nr;
        nr %= 1999999973;
    }
    fout<<s;

}