Cod sursa(job #2923631)
Utilizator | mihai popa PopaMihai | Data | 16 septembrie 2022 22:09:52 |
---|---|---|---|
Problema | Invers modular | Scor | 40 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.5 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int a, MOD;
int FastPOW(int base, int exp)
{
int ans = 1;
int aux = base;
for(int i = 0; (1 << i) <= exp; ++i) {
if((exp >> i) & 1)
ans = (1LL * ans * aux) % MOD;
aux = (1LL * aux * aux) % MOD;
}
return ans;
}
int main()
{
fin >> a >> MOD;
fout << FastPOW(a, MOD - 2);
return 0;
}