Cod sursa(job #3171440)

Utilizator gianiferSpita Alexandru-Mihai gianifer Data 18 noiembrie 2023 21:05:29
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.54 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("inversmodular.in");

ofstream fout("inversmodular.out");

void gcd(long long N, long long  M, long long &x, long long  &y)
{
    if (M == 0)
    {
        x = 1, y = 0;
        return;
    }
    else
    {
        gcd(M, N % M, x, y);
        long long aux=x;
        x = y;
        y = aux - (N / M) *y;
    }
}

int main()
{
    long long a, n, x = 0, y = 0;
    fin >> a >> n;
    gcd(a, n, x, y);
    if (x <= 0)
        x = n + x % n;
    fout << x;
}