Cod sursa(job #2255718)

Utilizator flibiaVisanu Cristian flibia Data 7 octombrie 2018 14:40:43
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.48 kb
#include <bits/stdc++.h>
#define ll long long

using namespace std;

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

int t;
ll a, b, c, d, x, y;

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

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