Cod sursa(job #1262455)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 13 noiembrie 2014 10:49:44
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.37 kb
#include <fstream>

using namespace std;

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

int main() {
	ifstream fin("inversmodular.in");
	ofstream fout("inversmodular.out");
	int a, n, x, y;
	fin >> a >> n;
	gcd(a, n, x, y);
	if(x < 0)
		x = n + x % n;
	fout << x << '\n';
}