Cod sursa(job #2772172)
| Utilizator | Data | 30 august 2021 21:59:40 | |
|---|---|---|---|
| Problema | Ridicare la putere in timp logaritmic | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.59 kb |
#include <fstream>
#define MOD (unsigned long) 1999999973
using namespace std;
unsigned long powerModulo(unsigned long n, unsigned long p) {
if (p == 0UL)
return 1UL;
if (p == 1Ul)
return n;
unsigned long res = powerModulo(n, p >> 1) % MOD;
res = res * res;
res %= MOD;
if (p & 1UL) {
res *= n;
res %= MOD;
}
return res;
}
int main(void) {
ifstream in("lgput.in");
ofstream out("lgput.out");
unsigned long N, P;
in >> N >> P;
out << powerModulo(N % MOD, P);
in.close();
out.close();
return 0;
}
