Cod sursa(job #3262359)
Utilizator | Data | 9 decembrie 2024 20:48:59 | |
---|---|---|---|
Problema | Invers modular | Scor | 40 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.38 kb |
#include <bits/stdc++.h>
using namespace std;
int main() {
ifstream in("inversmodular.in");
ofstream out("inversmodular.out");
int n, mod;
in >> n >> mod;
int ans = 1, aux = n, p = mod - 2;
for(int i = 0; (1 << i) <= p; i++) {
if((1 << i) & p) {
ans = 1LL * ans * aux % mod;
}
aux = 1LL * aux * aux % mod;
}
out << ans << '\n';
return 0;
}