Cod sursa(job #1185047)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 14 mai 2014 21:50:37
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.5 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int A, N;

int main()
{
    ifstream f("inversmodular.in");
    ofstream g("inversmodular.out");

    f >> A >> N;

    int x, y;

    gcd( A, N, x, y );

    if ( x <= 0 ) x = N + x % N;

    g << x << "\n";

    return 0;
}