Cod sursa(job #1413527)

Utilizator o_micBianca Costin o_mic Data 1 aprilie 2015 22:09:41
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.47 kb
#include <iostream>
#include <fstream>
#define LL long long 
 
using namespace std;

void invers(LL a, LL b, LL &x, LL &y){
    if(!b){
        x = 1;
        y = 0;
        return;
    }
    invers(b, a % b, x, y);
    LL aux = x;
    x = y;
    y = aux - a/b * y;
}

int main()
{
    LL a, n, x, y;
    ifstream fin("inversmodular.in");
    ofstream fout("inversmodular.out");
    fin >> a >> n;
    invers(a, n, x, y);
    if(x < 0)
        x += n;
    fout << x;
    return 0;
}