Cod sursa(job #3132376)

Utilizator Alex_BerbescuBerbescu Alexandru Alex_Berbescu Data 22 mai 2023 16:00:14
Problema Ridicare la putere in timp logaritmic Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include<bits/stdc++.h>
#define MOD 1999999973
using namespace std;
class InParser {
    vector<char> str;
    int ptr;
    ifstream fin;

    char getChar() {
        if (ptr == int(str.size())) {
            fin.read(str.data(), str.size());
            ptr = 0;
        }
        return str[ptr++];
    }

    template<class T>
    T getInt() {
        char chr = getChar();
        while (!isdigit(chr) && chr != '-')
            chr = getChar();
        int sgn = +1;
        if (chr == '-') {
            sgn = -1;
            chr = getChar();
        }
        T num = 0;
        while (isdigit(chr)) {
            num = num * 10 + chr - '0';
            chr = getChar();
        }
        return sgn * num;
    }

public:
    InParser(const char* name) : str(1e5), ptr(str.size()), fin(name) { }
    ~InParser() { fin.close(); }

    template<class T>
    friend InParser& operator>>(InParser& in, T& num) {
        num = in.getInt<T>();
        return in;
    }
};
int n, p;
InParser fin("lgput.in");
ofstream fout("lgput.out");
int main()
{
    fin >> n >> p;
    unsigned long long a = 1;
    while(p > 0)
    {
        if(p & 1)
        {
            a = ((a % MOD) * n) % MOD;
        }
        p = p / 2;
        n = (n * n) % MOD;
    }
    fout << (a % MOD);
    return 0;
}