Cod sursa(job #1921210)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 10 martie 2017 11:44:53
Problema Ridicare la putere in timp logaritmic Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <iostream>
#include <fstream>

using namespace std;

#define MOD 1999999973

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

inline void fasterLLDivMod(unsigned long long x, unsigned y, unsigned &out_d, unsigned &out_m) {
    unsigned xh = (unsigned)(x >> 32), xl = (unsigned)x, d, m;
#ifdef __GNUC__
    asm(
        "divl %4; \n\t"
        : "=a" (d), "=d" (m)
        : "d" (xh), "a" (xl), "r" (y)
    );
#else
    __asm {
        mov edx, dword ptr[xh];
        mov eax, dword ptr[xl];
        div dword ptr[y];
        mov dword ptr[d], eax;
        mov dword ptr[m], edx;
    };
#endif
    out_d = d; out_m = m;
}

//x / y < 2^32 !
inline unsigned fasterLLMod(unsigned long long x, unsigned y) {
    unsigned dummy, r;
    fasterLLDivMod(x, y, dummy, r);
    return r;
}

///ceva = fasterLLMod(numar,mod);

uint64_t lgput(uint64_t base,uint64_t exponent){
    uint64_t n=1;
    for (;exponent;exponent>>=1){
        if (1&exponent)
            n=fasterLLMod(n*base,MOD);
        base=fasterLLMod(base*base,MOD);
    }
    return n;
}

int main()
{
    uint64_t n,p;
    f>>n>>p;
    t<<lgput(n,p);
    return 0;
}