Cod sursa(job #1608767)

Utilizator MarcusPopMarcus Pop MarcusPop Data 22 februarie 2016 12:41:23
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>
#define modulo  1999999973

using namespace std;

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

long long pow(int a, int b)
{
    if (b == 0)
        return (1);
    if (b == 1)
        return (a);
    if (b % 2 == 1)
        return (a * pow(a * a, b - 1));
    else
        return (pow(a * a, b / 2));
}

long long poww(int a, int b)
{
    long long s = 1;

    while (b > 0)
    {
        if (b % 2)
        {
            s *= a;
            b--;
        }
        a *= a;
        b /= 2;
    }
    return (s);
}

int main()
{
    int a, b;

    f >> a >> b;
    g << poww(a, b) % modulo;
    return 0;
}