Cod sursa(job #998028)

Utilizator Cosmin1490Balan Radu Cosmin Cosmin1490 Data 15 septembrie 2013 15:38:26
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;

const string file = "inversmodular";

const string infile = file + ".in";
const string outfile = file + ".out";

const int INF = 0x3f3f3f3f;

int euclidExtins(int a, int b, int& x, int& y)
{
	if(b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}
	else
	{
		int x0 = 0;
		int y0 = 0;
		int d = euclidExtins(b, a % b, x0, y0);
		x = y0;
		y = x0 - (a /b)*y0;
		return d;
	}
}


int main()
{
	fstream fin(infile.c_str(), ios::in);
	int a, b;
	fin >> a >> b;
	fin.close();

	int x;
	int y;
	int d = euclidExtins(a, b, x, y);

	while(x < 0)
		x += b;
	while(x > b)
		x -= b;

	fstream fout(outfile.c_str(), ios::out);
	fout << x << "\n";
	fout.close();
}