Cod sursa(job #3358086)

Utilizator titus.ticusanTitusTicusan titus.ticusan Data 14 iunie 2026 16:01:58
Problema Invers modular Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <stdio.h>

long long ext_gcd(long long a, long long b, long long *x, long long *y)
{
    if (b == 0)
    {
        *x = 1;
        *y = 0;
        return a;
    }

    long long x1, y1;
    long long d = ext_gcd(b, a % b, &x1, &y1);

    *x = y1;
    *y = x1 - (a / b) * y1;

    return d;
}

int main()
{
    FILE *fin = fopen("inversmodular.in", "r");
    FILE *fout = fopen("inversmodular.out", "w");

    long long A, N;
    fscanf(fin, "%lld %lld", &A, &N);

    long long x, y;
    ext_gcd(A, N, &x, &y);

    x %= N;
    if (x < 0)
        x += N;

    fprintf(fout, "%lld", x);

    fclose(fin);
    fclose(fout);

    return 0;
}