Cod sursa(job #2745557)

Utilizator ptlsebiptl sebi ptlsebi Data 26 aprilie 2021 18:55:34
Problema Algoritmul lui Euclid extins Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.79 kb
#include <stdio.h>
#include <stdint.h>

void read_int32_t(FILE *__restrict stream, int32_t *__restrict nr) {
    uint8_t ch;
    *nr = 0;
    uint8_t neg = 0;
    ch = fgetc(stream);
    if (ch == '-') {
        neg = 1;
    } else if ('0' <= ch && ch <= '9') {
        *nr = ch - '0';
    } else {
        return;
    }
    while ((ch = fgetc(stream)) && ('0' <= ch && ch <= '9')) {
        *nr *= 10;
        *nr += ch - '0';
    }
    if (neg) {
        *nr = -*nr;
    }
    if (ch == '\r') {
        fgetc(stream);
    }
}

void read_uint32_t(FILE *__restrict stream, uint32_t *__restrict nr) {
    uint8_t ch;
    *nr = 0;
    while ((ch = fgetc(stream)) && ('0' <= ch && ch <= '9')) {
        *nr *= 10;
        *nr += ch - '0';
    }
    if (ch == '\r') {
        fgetc(stream);
    }
}

void euclid_exins(int32_t a, int32_t b, int32_t *x, int32_t *y, int32_t *d) {
    if (b == 0) {
        *d = a;
        *x = 1;
        *y = 0;
        return;
    }
    int xx, yy, q = a / b;
    euclid_exins(b, a % b, &xx, &yy, d);
    *x = yy;
    *y = xx - yy * q;
}

int main() {
    uint32_t n;
    {
        int32_t a = 0, b = 0, c = 0, d = 0;
        int32_t x = 0, y = 0;
        FILE *__restrict in = fopen("euclid3.in", "r");
        FILE *__restrict out = fopen("euclid3.out", "w");
        read_uint32_t(in, &n);

        {
            int32_t i;
            for (i = 0; i < n; ++i) {
                read_int32_t(in, &a);
                read_int32_t(in, &b);
                read_int32_t(in, &c);

                euclid_exins(a, b, &x, &y, &d);
                if (c % d == 0) {
                    fprintf(out, "%d %d\n", x * (c / d), y * (c / d));
                } else {
                    fputs("0 0\n", out);
                }
            }
        }

        fclose(in);
        fclose(out);
    }
    return 0;
}