Cod sursa(job #1745186)

Utilizator PaulTPaul Tirlisan PaulT Data 21 august 2016 14:08:13
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.57 kb
#include <fstream>
using namespace std;

ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");

void euclid(int a, int b, int* d, long long* x, long long* y);

int main()
{
	int a, n, d;
	long long x, y;
	fin >> a >> n;
	euclid(a, n, &d, &x, &y);
	if (x <= 0)
       x = n + x % n;
	fout << x << '\n';
	
	fin.close();
	fout.close();
	return 0;
}

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