Cod sursa(job #1174810)

Utilizator mircea.dobreanuMircea Dobreanu mircea.dobreanu Data 23 aprilie 2014 22:38:44
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.5 kb
#include<fstream>
#include<iostream>
using namespace std;
void euclid_ext (int a, int b, int& d, int& x, int& y)
{
	if (b == 0)
	{
		d = a;
		x = 1;
		y = 0;
	}
	else
	{
        int x0, y0;
        euclid_ext(b, a%b, d, x0, y0);
        x = y0;
        y = x0 - (a/b) * y0;
	}
}
int main()
{
	int a, n, d, x, y;
	ifstream fin("inversmodular.in");
	fin>>a>>n;
	euclid_ext(a, n, d, x, y);
	while (x<0)
		x += n;
	ofstream fout("inversmodular.out");
	fout<<x<<'\n';
	return 0;
}