Cod sursa(job #904298)

Utilizator Cosmin1490Balan Radu Cosmin Cosmin1490 Data 4 martie 2013 03:54:49
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <fstream>
#include <iostream>
#include <string>
#include <queue>
#include <vector>

using namespace std;


const string file = "inversmodular";

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



void euclid(int a, int b, long long &x, long long &y)
{
	if(b == 0)
	{
		x = 1;
		y = 0;
	}
	else
	{
		long long x0 = 0;
		long long y0 = 0;
		euclid(b, a % b, x0, y0);
		
		x = y0;
		y = x0 - (a/b)*y0;

	}

}



int main()
{

	int A;
	int N;


	fstream fin(infile.c_str(), ios::in);

	fin >> A >> N;

	fin.close();


	long long x = 0;
	long long y = 0;

	euclid(A, N, x, y);

	while(x < 0)
		x += N;


	long long raspuns = x;

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

}