Cod sursa(job #1220538)

Utilizator ptquake10ptquake10 ptquake10 Data 17 august 2014 16:44:43
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <map>
using namespace std;
#define MAX 1000001

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


int main() {
	long long a, b, d, x, y;
	
	freopen("inversmodular.in","r",stdin);
	freopen("inversmodular.out","w",stdout);
		
	cin >> a >> b;
	gcd(x,y,d,a,b);
	if (x < 0) {
		x += ((-x)/b + 1) * b;
	}
	
	cout << x << endl;
	
	return 0;
}