Pagini recente » Cod sursa (job #3256459) | Cod sursa (job #140202) | Cod sursa (job #1563673) | Cod sursa (job #194210) | Cod sursa (job #2471893)
#include <bits/stdc++.h>
using namespace std;
// Function for extended Euclidean Algorithm
int gcdExtended(int a, int b, int *x, int *y) {
// Base Case
if (a == 0) {
*x = 0;
*y = 1;
return b;
}
int x1, y1; // To store results of recursive call
int gcd = gcdExtended(b%a, a, &x1, &y1);
// Update x and y using results of recursive call
*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}
int main()
{
//freopen("euclid3.in", "r", stdin);
//freopen("euclid3.out", "w", stdout);
freopen("inversmodular.in", "r", stdin);
freopen("inversmodular.out", "w", stdout);
int t, x, y, a, b, c = 1;
//scanf("%d", &t);
//while (t--) {
//scanf("%d %d %d", &a, &b, &c);
scanf("%d %d", &a, &b);
//gcd(a, b) = 1
int g = gcdExtended(a, -b, &x, &y);
printf("%d\n", x * (c / g));
//}
return 0;
}