Cod sursa(job #2867238)
| Utilizator | Data | 10 martie 2022 11:38:01 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.57 kb |
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int n, a, b, c;
int x, y;
int Euclid_E(int a, int b, int &x, int &y);
int main()
{
int aux;
fin>>a>>n;
Euclid_E(a, n, x, y);
if (x < 0) {
aux = (-1)*x;
aux/=n;
x+=(aux+1)*n;
}
fout<<x<<'\n';
return 0;
}
int Euclid_E(int a, int b, int &x, int &y) {
int x1, y1, d;
if (b == 0) {
x = 1; y = 0;
return a;
}
d = Euclid_E(b, a%b, x1, y1);
x = y1;
y = x1-y1*(a/b);
return d;
}
