Cod sursa(job #831198)

Utilizator sebii_cSebastian Claici sebii_c Data 8 decembrie 2012 11:41:57
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.52 kb
#include <cstdio>

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 main()
{   
    freopen("inversmodular.in", "r", stdin);
    freopen("inversmodular.out", "w", stdout);

    int a, n;
    scanf("%d %d", &a, &n);
    int d, x, y;
    euclid(a, n, &d, &x, &y);
    while (x < 0) x += n;
    printf("%d\n", x);

    return 0;
}