Cod sursa(job #2194883)
Utilizator | Data | 14 aprilie 2018 15:45:50 | |
---|---|---|---|
Problema | Ridicare la putere in timp logaritmic | Scor | 10 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.46 kb |
#include <fstream>
#define R 1999999973
int main() {
std::ifstream in("lgput.in");
std::ofstream out("lgput.out");
long long n, p, c = 1;
in >> n >> p;
n = n % R;
while (c << 1 <= p) {
c <<= 1;
n = (n * n) % R;
}
long long r = p - c;
while (r > 0) {
if (c > r) c >>= 1;
else {
n = ((n >> 1) * n) % R;
c >>= 1;
r -= c;
}
}
out << n;
return 0;
}