Cod sursa(job #1193605)
| Utilizator | Data | 1 iunie 2014 09:03:22 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.6 kb |
#include <fstream>
using namespace std;
ifstream cin("inversmodular.in");
ofstream cout("inversmodular.out");
void Euclid(const int A, const int B, int &X, int &Y)
{
if (B == 0)
{
X = 1;
Y = 0;
return;
}
int X0, Y0;
Euclid(B, A % B, X0, Y0);
X = Y0;
Y = X0 - (A / B) * Y0;
}
int InvMod(const int A, const int N)
{
int x, y;
Euclid(A, N, x, y);
if (x < 0)
x = N + x % N;
return x;
}
int main()
{
int A, N;
cin >> A >> N;
cout << InvMod(A, N);
cin.close();
cout.close();
}
