Cod sursa(job #719178)
Utilizator | Data | 21 martie 2012 15:36:37 | |
---|---|---|---|
Problema | Invers modular | Scor | 40 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.48 kb |
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
long long inv(long long a, long mod) {
long long i, rez = 1;
for (i = 0; (1 << i) <= (mod - 2); ++i) {
if ((1 << i) & (mod - 2))
rez = 1LL * (rez * a) % mod;
a = 1LL * (a * a) % mod;
}
return rez;
}
int main() {
int a, b;
fin >> a >> b;
fout << inv(a, b) << "\n";
fout.close();
return 0;
}