Cod sursa(job #996228)

Utilizator gbi250Gabriela Moldovan gbi250 Data 11 septembrie 2013 14:51:32
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.52 kb
#include <cstdio>

using namespace std;

int a, n;
long long x, y;

void GCD (int a, int b, long long &x, long long &y)
{
    if( !b )
    {
        x = 1;
        y = 0;
    }
    else
    {
        long long x0, y0;
        GCD(b, a%b, x0, y0);
        x = y0;
        y = (x0 - a/b*y0);
    }
}

int main()
{
    freopen("inversmodular.in", "r", stdin);
    freopen("inversmodular.out", "w", stdout);
    scanf("%d %d", &a, &n);
    GCD (a, n, x, y);

    if( x<0 )
        x = n + x%n;
    printf("%lld\n", x);
    return 0;
}