Cod sursa(job #2376460)

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

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

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;
}