Cod sursa(job #1892639)

Utilizator contnouAndrei Pavel contnou Data 25 februarie 2017 10:27:05
Problema Invers modular Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>

using namespace std;

ifstream f("inversmodular.in");
ofstream g("inversmodular.out");

void readNumbers(int &a, int &n) {
	//
	f >> a >> n;
}

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

void computeModularMultiplicativeInverse(int a, int n, int &inv) {
	int x = 0, y = 0, gcd = 0;
	getFactors(n, a, gcd, x, y);
	
	inv = y;
}

int main() {
	//
	int a, n, inv;

	readNumbers(a, n);
	computeModularMultiplicativeInverse(a, n, inv);
	g << inv;
}