Cod sursa(job #2569194)

Utilizator richardbaczur1Baczur Richard richardbaczur1 Data 4 martie 2020 11:27:40
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <bits/stdc++.h>
#define infile "inversmodular.in"
#define outfile "inversmodular.out"

using namespace std;

void cmmdc(long long &x, long long &y, int a, int b)
{
    if (!b)
    {
        x = 1;
        y = 0;
    }
    else
    {
        cmmdc(x, y, b, a % b);
        long long aux = x;
        x = y;
        y = aux - y * (a / b);
    }
}

int a, n, x, y;
long long inv, ins;
int main()
{
    freopen(infile, "r", stdin);
    freopen(outfile, "w", stdout);

    scanf("%d %d", &a, &n);
    cmmdc(inv, ins, a, n);

    if (inv <= 0)
    {
        inv = n + inv % n;
    }

    printf("%d", inv);

    fclose(stdin);
    fclose(stdout);
    return 0;
}