Cod sursa(job #361743)

Utilizator digital_phreakMolache Andrei digital_phreak Data 6 noiembrie 2009 16:16:02
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.5 kb
#include <iostream>
#include <fstream>
using namespace std;

typedef long long ll;

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

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

int main() {
	ll A,N,x,y;
	
	fin >> A >> N;
	
	gcd(A,N,x,y);
	
	if (x <= 0) {
		x = N + x % N;
	}
	
	fout << x << "\n";
	
	fin.close();
	fout.close();
	return 0;
}