Cod sursa(job #2069824)
Utilizator | Agache Mihai MikeStrike | Data | 18 noiembrie 2017 20:46:59 |
---|---|---|---|
Problema | Invers modular | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.5 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("inversmodular.in");
ofstream out("inversmodular.out");
int A,N;
void euclid(long long &x, long long &y,int a,int b)
{
if(b==0)
{
x=1;
y=0;
}
else
{
euclid(x,y,b,a%b);
long long aux=x;
x=y;
y=aux-y*(a/b);
}
}
int main()
{ long long inv=0,ins;
in>>A>>N;
euclid(inv,ins,A,N);
if(inv<=0)
inv=N+inv%N;
out<<inv;
return 0;
}