Pagini recente » Cod sursa (job #10832) | Cod sursa (job #1555020) | Cod sursa (job #1141579) | Cod sursa (job #522239) | Cod sursa (job #1708759)
#include <cstdio>
using namespace std;
long long gcd(long long a, long long b, long long &x, long long &y) {
if (!b) {
x = 1;
y = 0;
return a;
}
long long x0, y0, d = gcd(b, a % b, x0, y0);
x = y0;
y = x0 - (a/b) * y0;
return d;
}
long long invers(long long A, long long N) {
long long inv, x;
gcd(A, N, inv, x);
while (inv <= 0) inv += N;
return inv;
}
int main() {
freopen("inversmodular.in", "r", stdin );
freopen("inversmodular.out", "w", stdout);
long long A, N;
scanf("%lld%lld", &A, &N);
printf("%lld\n", invers(A, N));
return 0;
}