Cod sursa(job #2762896)
| Utilizator | Data | 9 iulie 2021 20:41:09 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.51 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("inversmodular.in");
ofstream fout("inversmodular.out");
void euclid_extins(int a,int b,int *d, int *x, int *y)
{
if(b==0)
{
*d=a;
*x=1;
*y=0;
}
else
{
int x0,y0;
euclid_extins(b,a%b,d,&x0,&y0);
*x=y0;
*y=x0-(a/b)*y0;
}
}
int d,x,y,a,b;
int main()
{
fin>>a>>b;
a=a%b;
euclid_extins(a,b,&d,&x,&y);
if(x<0)
x+=b;
fout<<x%b;
return 0;
}