Cod sursa(job #915542)

Utilizator howsiweiHow Si Wei howsiwei Data 15 martie 2013 09:52:25
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.41 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
int x, y;

void euclid(int a, int b) {
	if (b==0) {
		x=1;
		y=0;
		return;
	}
	euclid(b, a%b);
	int tx=y;
	int ty=x-y*(a/b);
	x=tx;
	y=ty;
}

int main() {
	ifstream fin("inversmodular.in");
	ofstream fout("inversmodular.out");
	int a, b;
	fin >> a >> b;
	euclid(a, b);
	x%=b;
	if (x<0) x+=b;
	fout << x;
	return 0;
}