Cod sursa(job #355458)

Utilizator borsoszalanBorsos Zalan borsoszalan Data 11 octombrie 2009 10:59:37
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.41 kb
#include <stdio.h>

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

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