Cod sursa(job #3196227)

Utilizator MagicantPlusIuoras Andrei MagicantPlus Data 23 ianuarie 2024 10:39:36
Problema Invers modular Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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

#define cin fin
#define cout fout

long long a, n;

void euclid(long long a, long long b, long long& x, long long& y);

int main()
{
    long long x, y;

    cin >> a >> n;

    euclid(a, n, x, y);

    while(x < 0)
    {
        x += n;
    }

    cout << x % n;

    return 0;
}

void euclid(long long a, long long b, long long& x, long long& y)
{
    if(b == 0)
    {
        y = 1;
        x = 1;
        return;
    }

    long long x1, y1;

    euclid(b, a % b, x1, y1);

    x = y1;
    y = x1 - y1 * a / b;

    x %= n;
    y %= n;
}