Cod sursa(job #1952579)
Utilizator | Data | 4 aprilie 2017 11:14:21 | |
---|---|---|---|
Problema | Invers modular | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.48 kb |
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
int a, n;
long long inv, ins;
void gcd(int a, int b, long long& x, long long& y)
{
if(!b)
x=1, y=0;
else
{
gcd(b, a%b, x, y);
long long aux=x;
x=y;
y=aux-y*(a/b);
}
}
int main()
{
fin>>a>>n;
gcd(a, n, inv, ins);
if(inv<=0)
inv=n+inv%n;
fout<<inv;
return 0;
}