Cod sursa(job #1536263)

Utilizator alexandra_udristoiuUdristoiu Alexandra Maria alexandra_udristoiu Data 25 noiembrie 2015 21:37:36
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.53 kb
#include<fstream>
using namespace std;
long long a, n, x, y;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
void f(long long a, long long b, long long &x, long long &y){
    if(b == 0){
        x = 1;
        y = 0;
    }
    else{
        long long x1, y1;
        f(b, a % b, x1, y1);
        x = y1;
        y = x1 - a / b * y1;
    }
}
int main(){
    fin>> a >> n;
    f(a, n, x, y);
    if(x < 0){
        x = ( x +  (((-x) / n + 1) * n)  ) % n;
    }
    fout<< x <<"\n";
    return 0;
}