Cod sursa(job #1686714)

Utilizator gabib97Gabriel Boroghina gabib97 Data 12 aprilie 2016 13:12:26
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 kb
#include <fstream>
#define long long long
using namespace std;
int a,n;
long x,y;
void inversmodular(int a,int b,long &x,long &y)
{
    if (!b) x=1,y=0;
    else
    {
        long x0,y0;
        inversmodular(b,a%b,x0,y0);
        x=y0;
        y=x0-(a/b)*y0;
    }
}
int main()
{
    ifstream fin ("inversmodular.in");
    ofstream fout ("inversmodular.out");
    fin>>a>>n;
    inversmodular(a,n,x,y);
    if (x<=0) x=n+x%n;
    fout<<x;
    fin.close();
    fout.close();
    return 0;
}