Cod sursa(job #3357698)

Utilizator dragos_22Dragos Stiuca dragos_22 Data 12 iunie 2026 23:50:26
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.6 kb
#include <iostream>

using namespace std;

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

int main(){
    freopen("inversmodular.in","r",stdin);
    freopen("inversmodular.out","w",stdout);

    int A, N;
    cin >> A >> N;
    
    int x,y,d = 1;
    euclidExtins(A,N,&d,&x,&y);
    if(x >= 0)
        cout << x;
    else{
        while(x < 0)
            x+=N;
        cout << x;
    }
    return 0;
}