Cod sursa(job #1152506)

Utilizator SpiriFlaviuBerbecariu Flaviu SpiriFlaviu Data 24 martie 2014 19:29:28
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.5 kb
#include <fstream>

using namespace std;

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



void euclid(int a, int b, long long &x, long long &y)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
        return;
    }
    euclid(b,a%b,x,y);
    long long aux = x;
    x = y;
    y = aux - y*(a/b);
}

int main()
{
    int a, n;
    long long x,y;
    fin>>a>>n;
    euclid(a,n,x,y);
    if(x < 0)
        x = n + x%n;
    fout<<x;




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