Cod sursa(job #2553546)
| Utilizator | Data | 22 februarie 2020 09:35:03 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.57 kb |
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("inversmodular.in");
ofstream fout ("inversmodular.out");
int a, p;
void Euclid (int a, int b, int&d, int &x, int &y)
{
if (b==0) d=a, x=1, y=0;
else
{
int x0, y0;
Euclid(b, a%b, d, x0, y0);
x=y0;
y=x0-(a/b)*y0;
}
}
int InversMod(int a, int p)
{
int d, x, y;
Euclid(a, p, d, x, y);
if (x<0) x+=p;
return x;
}
int main()
{
fin>>a>>p;
fin.close();
fout<<InversMod(a, p);
fout.close();
return 0;
}
