Cod sursa(job #3358338)

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

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 x0 = 1, x1 = 0;
        long long b0 = a, b1 = n;

        while (b1 != 0) {
            long long q = b0 / b1;
            long long tmpr = b0 % b1;
            b0 = b1;
            b1 = tmpr;

            long long tmpx = x0 - q * x1;
            x0 = x1;
            x1 = tmpx;
        }

        while (x0 < 0) {
            x0 += n;
        }
        x0 %= n;

        fprintf(fout, "%lld\n", x0);
    }

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