Cod sursa(job #2061041)

Utilizator LeVladzCiuperceanu Vlad LeVladz Data 8 noiembrie 2017 21:27:18
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.56 kb
#include <fstream>

using namespace std;

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

long long a,n,x,y;

void inversModular(long long a, long long b, long long &x, long long &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
    }
    else
    {
        long long xx, yy;
        inversModular(b, a%b, xx, yy);
        x = yy;
        y = xx-(a/b)*yy;
    }
}

int main()
{
    fin >> a >> n;
    inversModular(a, n, x, y);
    if (x < 0)
        x = (x+n*((-x)/n+1))%n;
    fout << x;
    return 0;
}