Cod sursa(job #3357636)

Utilizator serban-andrei.cristeaCristea Serban-Andrei serban-andrei.cristea Data 12 iunie 2026 12:54:34
Problema Algoritmul lui Euclid extins Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.56 kb
#include <stdio.h>
#include <stdlib.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 x0, y0;
        euclid_extins(b, a % b, d, &x0, &y0);
        *x = y0;
        *y = x0 - (a / b) * y0;
    }
}

int main() {
    FILE *fin = fopen("euclid3.in", "r");
    FILE *fout = fopen("euclid3.out", "w");
    
    int T;
    if (fscanf(fin, "%d", &T) != 1) return 0;
    
    while (T > 0) {
        long long a, b, c, d, x, y;
        fscanf(fin, "%lld %lld %lld", &a, &b, &c);
        
        if (a == 0 && b == 0) {
            fprintf(fout, "0 0\n");
            T--;
            continue;
        }

        long long abs_a = llabs(a);
        long long abs_b = llabs(b);
        
        euclid_extins(abs_a, abs_b, &d, &x, &y);
        
        if (c % d != 0) {
            fprintf(fout, "0 0\n");
        } else {
            long long factor = c / d;
            long long sol_x = x * factor;
            long long sol_y = y * factor;
            
            if (a < 0) sol_x = -sol_x;
            if (b < 0) sol_y = -sol_y;

            if (a != 0 && b != 0) {
                long long pas_x = abs_b / d;
                
                sol_x = sol_x % pas_x;
                
                sol_y = (c - a * sol_x) / b;
            }
            
            fprintf(fout, "%lld %lld\n", sol_x, sol_y);
        }
        T--;
    }
    
    fclose(fin);
    fclose(fout);
    return 0;
}