Cod sursa(job #928609)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 26 martie 2013 16:02:42
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.54 kb
#include <fstream>

using namespace std;

#define ll long long

int A, N;

void GCD ( ll &x, ll &y, ll a, ll b ){

    if ( !b ){

        x = 1;
        y = 0;

        return;
    }

    GCD( x, y, b, a % b );

    ll aux = x;
    x = y;
    y = aux - y * (a / b);
}


int main(){

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

    f >> A >> N;

    ll inv = 0, ins;

    GCD( inv, ins, A, N );

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

    g << inv << "\n";

    return 0;
}