Cod sursa(job #978384)

Utilizator FlameingoAiordachioaei Marius Flameingo Data 28 iulie 2013 18:56:57
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.51 kb
#include <cstdio>
using namespace std;

void euclid (int a, int b, int &x, int &y) {

    if (!b) {
        x = 1;
        y = 0;
        return;
    }
    int x0, y0;
    euclid (b, a % b, x0, y0);
    x = y0;
    y = x0 - a / b * y0;

}

int main () {

    freopen ("inversmodular.in", "r", stdin);
    freopen ("inversmodular.out", "w", stdout);
    int a, b, x, y;
    scanf ("%d%d", &a, &b);
    euclid (a, b, x, y);
    if (x < 0)
        x += (-x / b + 1) * b;
    printf ("%d", x);

}