Cod sursa(job #731490)
| Utilizator | Data | 8 aprilie 2012 11:07:08 | |
|---|---|---|---|
| Problema | Ridicare la putere in timp logaritmic | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.5 kb |
#include <fstream>
#define MOD 1999999973
using namespace std;
ifstream fin("lgput.in");
ofstream fout("lgput.out");
long long lgput(int p, int a, int m);
int main()
{
int a, b;
fin >> a >> b;
fout << lgput(a, b, MOD);
fin.close();
fout.close();
return 0;
}
long long lgput(int a, int b, int m)
{
if ( b == 1 )
return a;
if ( b == 0 )
return 1;
long long rez = 1;
rez = lgput(a, b / 2, m);
rez = (rez * rez ) % m;
if ( b & 1 ) // b - impar
rez *= a;
return rez % m;
}