Cod sursa(job #1568731)
| Utilizator | Data | 14 ianuarie 2016 17:48:38 | |
|---|---|---|---|
| Problema | Ridicare la putere in timp logaritmic | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.55 kb |
#include <fstream>
using namespace std;
ifstream fin("lgput.in");
ofstream fout("lgput.out");
int squaringExponentiation(int x, int n, int m) {
if (n==0)
return 1;
else if (n == 1)
return x;
else if (n % 2 == 1)
return 1LL * ((x%m) * (squaringExponentiation(x * x, (n - 1) / 2, m)%m))%m;
else if (n % 2 == 0)
return 1LL * (squaringExponentiation(x * x, n / 2, m)%m)%m;
}
int main()
{
int a, b, c;
fin >> a >> b >> c;
fout << squaringExponentiation(a, b, c);
return 0;
}
