Cod sursa(job #1499800)

Utilizator fluture.godlikeGafton Mihnea Alexandru fluture.godlike Data 11 octombrie 2015 10:29:31
Problema Invers modular Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.58 kb
#include <cstdio>

#define LL long long

using namespace std;
int a, mod;
LL inv = 0, ins;

void cmmdc(LL &x, LL &y, int a, int b)
{
     if (!b)
         x = 1, y = 0;
     else
     {
         cmmdc(x, y, b, a % b);
         LL aux = x;
         x = y;
         y = aux - y * (a / b);
     }
}

int main()
{

    freopen("inversmodular.in", "r", stdin);
    freopen("inversmodular.out", "w", stdout);

    scanf("%d %d", &a, &mod);
    cmmdc(inv, ins, a, mod);

    if (inv <= 0)
       inv = mod + inv % mod;

    printf("%lld\n", inv);

    return 0;
}