Cod sursa(job #1944901)
Utilizator | Data | 29 martie 2017 12:00:38 | |
---|---|---|---|
Problema | Invers modular | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.53 kb |
#include <fstream>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int a, n;
long long exp(int x, int y) {
long long r = 1;
for (int i = 0; (1 << i) <= y; ++i) {
if ((1 << i) & y) {
r *= x;
r %= n;
}
x *= x;
x %= n;
}
return r;
}
int main() {
fin >> a >> n;
fout << exp(a, n - 2);
fout.close();
return 0;
}