Cod sursa(job #2049066)
| Utilizator | Data | 26 octombrie 2017 20:23:08 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 50 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.51 kb |
#include <iostream>
#include <fstream>
// A * X mod N = 1
std::ifstream cin("inversmodular.in");
std::ofstream cout("inversmodular.out");
int A, N, nr, a, b, c;
int euclid(int a, int b, int &x, int &y)
{
if (b == 0) {
x = 1;
y = 0;
return a;
}
int x0, y0, d;
d = euclid(b, a % b, x0, y0);
x = y0;
y = x0 - (a / b) * y0;
return d;
}
int main () {
cin >> A >> N;
c = 1;
int x, y, d;
d = euclid(A, N, x, y);
cout << x * (c / d);
return 0;
}