Cod sursa(job #3357437)

Utilizator bree.vtxKohl Briana bree.vtx Data 9 iunie 2026 19:07:28
Problema Algoritmul lui Euclid extins Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include <stdio.h>
#include <stdlib.h>

long long euclid_extins(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 cmmdc = euclid_extins(b, a % b, &x1, &y1);
    *x = y1;
    *y = x1 - (a / b) * y1;
    return cmmdc;
}

void rezolva_ecuatie(long long a, long long b, long long c, FILE *fout) {
    int semn_a = (a < 0) ? -1 : 1;
    int semn_b = (b < 0) ? -1 : 1;

    long long abs_a = labs(a);
    long long abs_b = labs(b);

    long long x0, y0;
    long long d = euclid_extins(abs_a, abs_b, &x0, &y0);

    if (c % d != 0) {
        fprintf(fout, "0 0\n");
        return;
    }

    long long x = x0 * (c / d);
    long long y = y0 * (c / d);

    x *= semn_a;
    y *= semn_b;

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

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

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

    int T;
    if (fscanf(fin, "%d", &T) != 1) {
        fclose(fin);
        fclose(fout);
        return 1;
    }

    while (T--) {
        long long a, b, c;
        if (fscanf(fin, "%lld %lld %lld", &a, &b, &c) == 3) {
            rezolva_ecuatie(a, b, c, fout);
        }
    }

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