Cod sursa(job #1815803)
| Utilizator | Data | 25 noiembrie 2016 19:45:12 | |
|---|---|---|---|
| Problema | Invers modular | Scor | 100 |
| Compilator | c | Status | done |
| Runda | Arhiva educationala | Marime | 0.6 kb |
#include <stdio.h>
void Euclid(int a, int b, int *x, int *y)
{
if (b == 0) {
*x = 1;
*y = 0;
return;
}
int xp, yp;
Euclid(b, a % b, &xp, &yp);
*x = yp;
*y = xp - (a / b) * yp;
}
int InversModular(int a, int b)
{
int x, y;
Euclid(a, b, &x, &y);
while (x < 0)
x += b;
return x;
}
int main()
{
FILE *fin = fopen("inversmodular.in", "r");
FILE *fout = fopen("inversmodular.out", "w");
int a, b;
fscanf(fin, "%d%d", &a, &b);
fprintf(fout, "%d\n", InversModular(a, b));
return 0;
}
