Cod sursa(job #1944888)
| Utilizator | Data | 29 martie 2017 11:57:41 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 40 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.52 kb |
#include <fstream>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
ll a, n;
ll exp(ll x, ll y) {
ll r = 1;
for (ll i = 0; (1 << i) <= y; ++i) {
if ((ll)(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;
}
