Cod sursa(job #2412979)

Utilizator MihaiB729Bucur Mihai MihaiB729 Data 22 aprilie 2019 18:48:11
Problema Invers modular Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.5 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int a,n;
void invers(long long &x,long long &y,int a,int b)
{
    if(b==0)
    {
        x=1;
        y=0;
    }
    else{
        invers(x,y,b,a%b);
        long long aux=x;
        x=y;
        y=aux-y*(a/b);
    }
}
int main()
{
   fin>>a>>n;
   long long inv=0,ins;
   invers(inv,ins,a,n);
   if(inv<=0)
    inv=n+inv%n;
   fout<<inv;
    return 0;
}