Pagini recente » Cod sursa (job #170345) | Cod sursa (job #2018584) | Cod sursa (job #1350591) | Cod sursa (job #742674) | Cod sursa (job #1015696)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int extendedEuclidean(int a, int b, int *x, int *y){
if (b == 0){
*x = 1;
*y = 0;
return a;
} else {
int x0, y0;
int d = extendedEuclidean(b, a%b, &x0, &y0);
*x = y0;
*y = x0-y0*(a/b);
return d;
}
}
int main()
{
freopen("inversmodular.in", "r", stdin);
freopen("inversmodular.out", "w", stdout);
int a,n;
scanf("%d %d", &a, &n);
int x0, y0;
int d = extendedEuclidean(a,n, &x0, &y0);
int X = x0/d;
while (X <= 0){
X += n;
}
printf("%d", X);
return 0;
}