Cod sursa(job #2481343)

Utilizator hurjui12AlexandruHurjui Alexandru-Mihai hurjui12Alexandru Data 26 octombrie 2019 19:31:22
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.55 kb
#include <fstream>
using namespace std;

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

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

int main()
{
    //     a*x = 1 (mod n)
    // <=> x = 1/a (mod n)
    int a, n;
    fin >> a >> n;
    long long r, g;
    euclid(a, n, r, g);
    if (r < 0)
        r = (r+n)%n;
    fout << r%n;
    return 0;
}