Cod sursa(job #3004614)

Utilizator Raresp2006Papacioc Rares-Ioan Raresp2006 Data 16 martie 2023 14:44:20
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.46 kb
#include<iostream>
#include<fstream>
using namespace std;

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

void invmod(int a, int n, int &x, int &y){
    if(n == 0){
        x = 1;
        y = 1;
    }
    else{
        int x1, y1;
        invmod(n, a % n, x1, y1);
        x = y1;
        y = x1 - a / n * y1;
    }
}

int main(){
    int n, a, x, y;
    fin >> a >> n;
    invmod(a, n, x, y);
    while(x < 0)
        x += n;
    fout << x;
}