Cod sursa(job #533091)

Utilizator ssergiussSergiu-Ioan Ungur ssergiuss Data 13 februarie 2011 00:36:12
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.68 kb
#include <algorithm>
#include <fstream>

using namespace std;

const char Input[] = "inversmodular.in";
const char Output[] = "inversmodular.out";

int A, N;

void EucEx( int a, int b, int &d, int &x, int &y ) {

    int x0, y0;

    if( b == 0 ) {

        d = a;
        x = 1;
        y = 0;
    }
    else {

        EucEx( b, a % b, d, x0, y0 );
        x = y0;
        y = x0 - (a / b) * y0;
    }
}

int main() {

    ifstream fin( Input );
    ofstream fout( Output );

    int d, x, y;

    fin >> A >> N;
    EucEx( A, N, d, x, y );

    while( x < 0 )
        x += N;

    fout << x % N;

    fin.close();
    fout.close();

    return 0;
}