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