Cod sursa(job #2555390)

Utilizator bem.andreiIceman bem.andrei Data 23 februarie 2020 22:51:43
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.52 kb
#include <iostream>
#include <fstream>

using namespace std;
ifstream r("inversmodular.in");
ofstream w("inversmodular.out");
int A, N;
void euclid(long long &x, long long &y, int a, int b)
{
    if (!b)
        x = 1, y = 0;
    else
    {
        euclid(x, y, b, a % b);
        long long aux = x;
        x = y;
        y = aux - y * (a / b);
    }
}
int main()
{
    long long inv=0, ins;
    r>>A>>N;
    euclid(inv, ins, A, N);
    if (inv <= 0)
        inv = N + inv % N;
    w<<inv;
    return 0;
}