Cod sursa(job #2376456)

Utilizator danhHarangus Dan danh Data 8 martie 2019 15:47:40
Problema Invers modular Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.51 kb
#include <iostream>
#include <fstream>
using namespace std;

int modInv(int a, int b)
{
    long long r, x1=1, x2=0, q, aux, auxb = b;

    while(b > 0)
    {
        q = a/b;
        aux = a;
        a = b;
        b = aux - q * b;
        aux = x1;
        x1 = x2;
        x2 = aux - q * x2;
    }
    if(x1 < 0)
    {
        x1 += auxb;
    }
    return x1;
}

int main()
{
    int a, n;

    fin>>a>>n;

    fout<<modInv(a, n);

    fin.close();
    fout.close();
    return 0;
}