Pagini recente » Cod sursa (job #255390) | Cod sursa (job #1864687) | Cod sursa (job #2146322) | Cod sursa (job #1475065) | Cod sursa (job #2909702)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("inversmodular.in");
ofstream out("inversmodular.out");
void euclid(int a, int b, int& x, int& y)
{
if (b == 0)
{
x = 1, y = 1;
}
else
{
int x1, y1;
euclid(b, a % b, x1, y1);
x = y1;
y = x1 - a / b * y1;
}
}
int invmod(int a,int n)
{
int x, y;
euclid(a, n, x, y);
while (x < 0)
x += n;
return x;
}
int main()
{
int n, a;
in >> a >> n;
out << invmod(a, n);
return 0;
}