Cod sursa(job #1700410)

Utilizator batistaUPB-Oprea-Cosmin-Dumitru batista Data 10 mai 2016 14:42:18
Problema Invers modular Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include<fstream>
using namespace std;

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

int invers_modular(int nr, int mod)
{
	int x, y;
	
	euclid(nr, mod, x, y);
	while(x < 0)
        x += N;

	return x;
}

int main()
{
	int A, N;
    ifstream f("inversmodular.in");
    ofstream g("inversmodular.out");
	
    f >> A >> N;
    g << invers_modular(A, N);

    f.close();
    g.close();
	
	return 0;
}