Cod sursa(job #2098233)

Utilizator flibiaVisanu Cristian flibia Data 2 ianuarie 2018 16:23:03
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;

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

ll a, b, x, y, d;

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

int main(){
	in >> a >> b;
	gcd(a, b, d, x, y);
	while(x < 0)
		x += b;
	out << x;
	return 0;
}