Cod sursa(job #3299648)

Utilizator AxkyroMiron Victor Eusebiu Axkyro Data 8 iunie 2025 20:00:38
Problema Algoritmul lui Euclid extins Scor 0
Compilator c-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#include <stdio.h>
#include <stdlib.h>

long exeuclid(long a, long b, long *x, long *y) {
    if (b == 0) {
        *x = 1;
        *y = 0;
        return a;
    }
    long x1, y1;
    long g = exeuclid(b, a % b, &x1, &y1);
    *x = y1;
    *y = x1 - (a / b) * y1;
    return g;
}

int main() {
    long a, b, c, x, y;
    FILE *input = fopen("euclid3.in", "r");
    FILE *output = fopen("euclid3.out", "w");

    if (!input || !output) {
        fprintf(stderr, "Eroare deschidere fisiere!");
        exit(-1);
    }

    if (fscanf(input, "%ld %ld %ld", &a, &b, &c) != 3) {
        fprintf(stderr, "Eroare la citire a, b, c!\n");
        exit(-1);
    }

    long g = exeuclid(a, b, &x, &y);
    if (g % c != 0) {
        fprintf(output, "Nu exista solutie\n");
    } else {
        fprintf(output, "%ld %ld\n", x * (c / g), y * (c / g));
    }

    fclose(input);
    fclose(output);
    return 0;
}