Cod sursa(job #2404496)

Utilizator ezioconnorVlad - Gabriel Iftimescu ezioconnor Data 12 aprilie 2019 21:32:24
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.51 kb
#include <bits/stdc++.h>
#define ll long long

using namespace std;

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

inline void gcd(ll a, ll b, ll &x, ll &y)
{
     if (!b)
        x = 1, y = 0;
     else
     {
         ll x0, y0;
         gcd(b, a % b, x0, y0);
         x = y0;
         y = x0 - (a / b) * y0;
     }
}

int main()
{
    ll a, n, inv, y;
    in >> a >> n;
    gcd(a, n, inv, y);
    if (inv < 0)
        inv = n + inv % n;
    out << inv;
    return 0;
}