Cod sursa(job #3199492)
| Utilizator | Data | 1 februarie 2024 18:03:14 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | cpp-64 | Status | done |
| Runda | Arhiva educationala | Marime | 0.48 kb |
#include <fstream>
using namespace std;
ifstream f("inversmodular.in");
ofstream g("inversmodular.out");
int a, M, inv, ins;
void euclid_ext(int a, int b, int &x, int &y)
{
if(!b)
x = 1, y = 0;
else
{
euclid_ext(b, a % b, x, y);
int p = x; x = y;
y = p - y * (a / b);
}
}
int main()
{
f >> a >> M;
euclid_ext(a, M, inv, ins);
if(inv <= 0)
inv = M + inv % M;
g << inv;
return 0;
}
