Pagini recente » Cod sursa (job #3145229) | Cod sursa (job #3245639) | Cod sursa (job #2510390) | Cod sursa (job #2840615) | Cod sursa (job #1957014)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
#define ll long long
int a, n;
ll inv, ins;
void Inv(ll& x, ll&y, int a, int b) {
if(!b)
x = 1, y = 0;
else {
Inv(x, y, b, a % b);
ll aux = x;
x = y;
y = aux - y * (a / b);
}
}
int main()
{
fin >> a >> n;
Inv(inv, ins, a, n);
if(inv <= 0)
inv = n + inv % n;
fout << inv;
return 0;
}