Cod sursa(job #2040864)

Utilizator Eduard24Eduard Scaueru Eduard24 Data 16 octombrie 2017 17:08:13
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.61 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");

long long a,n,x,y,p,k;

long long euclid(long long a, long long n, long long &x, long long &y)
{
    long long x1,y1;
    if(n==0)
    {
        x=1;
        y=0;
        return a;
    }
    long long d;
    d=euclid(n,a%n,x1,y1);
    x=y1;
    y=x1-(a/n)*y1;
    return d;
}

int main()
{
    fin>>a>>n;
    p=euclid(a,n,x,y);
    if(x<=0)
    {
        k=(-x)/n+1;
        x=x+k*n;
    }
    fout<<x;
    fin.close();
    fout.close();
    return 0;
}