Cod sursa(job #791353)
Utilizator | Data | 23 septembrie 2012 21:16:00 | |
---|---|---|---|
Problema | Invers modular | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.51 kb |
#include <fstream>
using namespace std;
void euclid(int a, int b, long long &x, long long &y)
{
if(!b)
x = 1, y = 0;
else
{
euclid(b, a % b, x, y);
long long aux = x;
x = y;
y = aux - y * (a / b);
}
}
int a, n;
int main()
{
long long x, y;
ifstream in("inversmodular.in"); ofstream out("inversmodular.out");
in>>a>>n;
euclid(a, n, x, y);
if(x < 0) x += n;
out<<x;
in.close(); out.close();
return 0;
}