Cod sursa(job #2742533)

Utilizator RaresFelixTudose Rares Felix RaresFelix Data 21 aprilie 2021 09:58:06
Problema Invers modular Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.44 kb
#include <bits/stdc++.h>

using namespace std;

int euclid_ext(int a, int b, int &x, int &y)
{
	if(!b)
	{
		x = 1;
		y = 0;
		return a;
	}
	int x1, y1, g;
	g = euclid_ext(b, a%b, x1, y1);
	x = y1;
	y = x1 - y1 * (a/b);
	return g;
}
ifstream fi("inversmodular.in");
ofstream fo("inversmodular.out");
int n, a;
int main() {
	fi >> a >> n;
	int x, y, d;
	d = euclid_ext(a, n, x, y);
	//a*x + n * y = d (mod n)
	//a*x = d (mod n)
	fo << x/d;
    return 0;
}