Cod sursa(job #2526392)

Utilizator Vinti03Vintilescu Andrei Florin Vinti03 Data 18 ianuarie 2020 15:58:10
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.5 kb
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
ll inv = 0,ins,a,n;
void inv_m(ll &x,ll &y, ll a, ll b)
{
    if(!b)
    {
        x = 1;
        y = 0;
    }
    else
    {
        inv_m(x,y,b,a%b);
        ll aux = x;
        x = y;
        y = aux-y*(a/b);
    }
}
int main()
{
    fin >> a >> n;
    inv_m(inv,ins,a,n);
    if(inv <= 0)
        inv = n+inv%n;
    fout << inv;
    return 0;
}