Cod sursa(job #3285669)

Utilizator AdrianRosuRosu Adrian Andrei AdrianRosu Data 13 martie 2025 12:24:48
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>
#define DIM 100001
#define int long long

using namespace std;

ifstream fin("inversmodular.in");

ofstream fout("inversmodular.out");

int Q, a, b, c, d, x, y, MOD;

void Modular_Inverse(int a, int b, int &d, int &x, int &y){

    if(!b){

        d = a;

        x = 1;

        y = 1;

    }

    else {

        int x1, y1;

        Modular_Inverse(b, a % b, d, x1, y1);

        x = y1;

        y = x1 - a / b * y1;

    }

}

int32_t main(){

    Q = 1;

    while(Q--){

        fin >> a >> MOD;

        Modular_Inverse(a, MOD, d, x, y);

        while(x < 0)

            x += MOD;

        fout << x << "\n";

    }

}