Pagini recente » Cod sursa (job #2644502) | Cod sursa (job #1545415) | Cod sursa (job #1013873) | Cod sursa (job #583204) | Cod sursa (job #3143017)
#include <bits/stdc++.h>
using namespace std;
void euclid(int a, int b, int &x, int &y)
{
if(a == 1)
{
x = 1;
y = 0;
return;
}
int x1, y1;
euclid(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
}
int invmod(int x, int mod)
{
if(__gcd(x, mod) != 1)
{
return 0;
}
else
{
int a, b;
euclid(x, mod, a, b);
while(a < 0)
{
a += mod;
}
return a;
}
}
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int main()
{
int m, n;
fin >> n >> m;
fout << invmod(n, m);
return 0;
}