Cod sursa(job #1483530)

Utilizator aimrdlAndrei mrdl aimrdl Data 9 septembrie 2015 15:46:10
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.45 kb
#include <stdio.h>

void euclid (int a, int b, int *x0, int *y0) {
	if (b == 0) {
		*x0 = 1;
		*y0 = 0;
	} else {
		euclid (b, a % b, x0, y0);
		int y = (*x0) - (a/b) * (*y0);
		int x = *y0;
		*x0 = x;
		*y0 = y;
	}
}

int main (void) {
	freopen("inversmodular.in", "r", stdin);
	freopen("inversmodular.out", "w", stdout);
	
	int a, n, x, y;
	
	scanf("%d %d", &a, &n);
	
	euclid (n, a, &x, &y);
	
	while (y < 0) y+=n;
	
	printf("%d", y);
	
	
	return 0;
}