Cod sursa(job #2685102)

Utilizator PatrickCplusplusPatrick Kristian Ondreovici PatrickCplusplus Data 15 decembrie 2020 23:06:10
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.63 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");

int Euclid(long long a, long long b, long long &x, long long &y){
    if (b == 0){
        x = 1;
        y = 0;
        return a;
    }
    else{
        long long x0, y0;
        long long d = Euclid(b, a % b, x0, y0);
        x = y0;
        y = x0 - 1LL * (a / b) * y0;
        return d;
    }
}

int main(){
    long long a, n;
    fin >> a >> n;
    long long x, y, d;
    d = Euclid(a, n, x, y);
   if (x < 0) x = n + x % n;
    fout << x;
    fin.close();
    fout.close();
    return 0;
}