Cod sursa(job #1923305)

Utilizator flibiaVisanu Cristian flibia Data 10 martie 2017 22:11:50
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.44 kb
#include <bits/stdc++.h>
#define ll long long

using namespace std;

ll a, m;

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

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

int main(){
	in >> a >> m;
	ll ans = 0, help;
	gcd(ans, help, a, m);
	if(ans <= 0) ans = m + ans%m;
	out << ans;
	return 0;
}