Cod sursa(job #2451053)

Utilizator Narcis09Grecu Narcis Narcis09 Data 25 august 2019 14:58:13
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.4 kb
#include <fstream>
using namespace std;
ifstream cin("inversmodular.in");
ofstream cout("inversmodular.out");
long gcd(long a, long b, long &x, long &y){
	if (!b){
		x=1;
		y=0;
		return a;
	}
	else{
		long d, x0, y0;
		d=gcd(b, a%b, x0, y0);
		x=y0;
		y=x0-(a/b)*y0;
		return d;
	}
}
int main()
{
	long a, n, x, y;
	cin>>a>>n;
	gcd(a, n, x, y);
	while (x<0){
		x=x+n;
	}
	cout<<x;
	cin.close();
	cout.close();
}