Cod sursa(job #3236077)

Utilizator AndreasAntoniuAntoniu Andreas AndreasAntoniu Data 25 iunie 2024 23:15:41
Problema Invers modular Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <stdio.h>

void euclid_extins(int a, int b, int *x, int *y)
{
    int r0 = a, r1 = b, x0 = 1, x1 = 0, y0 = 0, y1 = 1;

    if (!b)
    {
        *x = x0;
        *y = y0;
    }
    else
    {
        while (r1)
        {
            int q = r0 / r1, aux;
            aux = r1;
            r1 = r0 - q * r1;
            r0 = aux;

            aux = x1;
            x1 = x0 - q * x1;
            x0 = aux;

            aux = y1;
            y1 = y0 - q * y1;
            y0 = aux;
        }
        *x = x0;
        *y = y0;
    }
}

int main()
{
    FILE *fin = fopen("inversmodular.in", "rb");
    FILE *fout = fopen("inversmodular.out", "wb");
    int a, b, x, y;

    fscanf(fin, "%d", &a);
    fscanf(fin, "%d", &b);

    euclid_extins(a, b, &x, &y);

    while (x < 0)
    {
        x += b;
    }
    fprintf(fout, "%d\n", x);
    fclose(fin);
    fclose(fout);
    return 0;
}