Cod sursa(job #2173930)

Utilizator alex_HarryBabalau Alexandru alex_Harry Data 16 martie 2018 09:44:03
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include <fstream>
using namespace std;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");
void euclid(long long a, long long b, long long *d, long long *x, long long *y)
{
    if(b == 0)
    {
        *d = a;
        *x = 1;
        *y = 0;
        return;
    }
    long long x0, y0;
    euclid(b, a % b, d, &x0, &y0);
    *x = y0;
    *y = x0 - (a / b) * y0;
}

int main()
{
    long long A, N, d, x, y;
    f >> A >> N;
    euclid(A, N, &d, &x, &y);
    x %= N;
    if(x < 0)
        x += N;
    g << x << "\n";
    return 0;
}