Cod sursa(job #2726247)
Utilizator | Bogdan Sever-Cristian severut | Data | 20 martie 2021 15:43:58 |
---|---|---|---|
Problema | Invers modular | Scor | 100 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 0.48 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("inversmodular.in");
ofstream out("inversmodular.out");
void euclid(int a,int b,int &x,int &y,int d)
{
if(b==0){
d=a;
x=1,y=0;
}
else{
int x1,y1;
euclid(b,a%b,x1,y1,d);
x=y1;
y=x1-a/b*y1;
}
}
int main()
{
int a,n,x=0,y=0,d=1;
in>>a>>n;
euclid(a,n,x,y,d);
while(x<0)
x+=n;
out<<x;
return 0;
}