Cod sursa(job #1972102)
| Utilizator | Data | 21 aprilie 2017 21:37:34 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.59 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("inversmodular.in");
ofstream out("inversmodular.out");
int A, N;
void euclid (int a, int b, long long &x, long long &y)
{
if (b == 0)
{
x = 1;
y = 0;
}
else
{
euclid (b, a % b, x, y);
long long aux = x;
x = y;
y = aux - (a / b) * y;
}
}
int main()
{
long long inv = 0, ins;
in >> A >> N;
euclid (A, N, inv, ins);
if (inv <= 0)
inv = N + inv % N;
out << inv;
return 0;
}
