Cod sursa(job #3358337)

Utilizator Radulescu_BiancaRadulescu Bianca-Larisa Radulescu_Bianca Data 16 iunie 2026 13:21:07
Problema Invers modular Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <stdio.h>

void euclid_extins(long long a, long long b, long long *d, long long *x, long long *y) {
    if (b == 0) {
        *d = a;
        *x = 1;
        *y = 0;
    } else {
        long long x1, y1;
        euclid_extins(b, a % b, d, &x1, &y1);
        *x = y1;
        *y = x1 - (a / b) * y1;
    }
}

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

    if (fin == NULL || fout == NULL) {
        return 0;
    }

    long long A, N;
    if (fscanf(fin, "%lld %lld", &A, &N) == 2) {
        long long d, x, y;
        euclid_extins(A, N, &d, &x, &y);

        x = (x % N + N) % N;
        fprintf(fout, "%lld\n", x);
    }

    fclose(fin);
    fclose(fout);
    return 0;
}