Cod sursa(job #991194)
| Utilizator | Data | 29 august 2013 23:14:45 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.55 kb |
#include <fstream>
#include <cmath>
using namespace std;
void gcd(int a, int b, int &x, int &y)
{
if(b == 0)
{
x = 1;
y = 0;
return ;
}
int x0, y0;
gcd(b, a % b, x0, y0);
x = y0;
y = x0 - (a / b) * y0;
}
int main()
{
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int a, x, n, y;
fin >> a >> n;
gcd(a, n, x, y);
int mul = abs(x / n);
if(x < 0)
{
x += (mul + 1) * n;
}
fout << x << '\n';
return 0;
}
