Cod sursa(job #515245)

Utilizator cmiNCosmin Poieana cmiN Data 20 decembrie 2010 20:59:25
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>
#include <math.h>
#define mod 1999999973

using namespace std;

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

int main()
{
    unsigned long a, b;
    ifstream fin("lgput.in");
    ofstream fout("lgput.out");
    fin >> a >> b;
    fout << lgpowmod(a, b, mod);
    fin.close();
    fout.close();
    return 0;
}