Cod sursa(job #2909515)

Utilizator BadBoyRADULICEA Constantin Dumitru Petre BadBoy Data 14 iunie 2022 01:50:03
Problema Algoritmul lui Euclid extins Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.65 kb
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <math.h>

// https://infoarena.ro/problema/euclid3


void euclid_ext(int a, int b, int* d, int* x, int* y)
{
    if (b == 0) {
        *d = a;
        *x = 1;
        *y = 0;
    }
    else {
        int x0, y0;
        euclid_ext(b, a % b, d, &x0, &y0);
        *x = y0;
        *y = x0 - (a / b) * y0;
    }
}


int maigsn() {
	std::ifstream fin("euclid3.in");
	std::ofstream fout("euclid3.out");
	int linii, a, b, c, d, x, y;

	fin >> linii;
	for (int i = 0; i < linii; i++)
	{
        x = 0;
        y = 0;
        d = 0;
        fin >> a >> b >> c;
        euclid_ext(a, b, &d, &x, &y);   // a * X + b * Y = c
        if (c % d == 0) {
            x = x * c / d;
            y = y * c / d;
        }
        else {
            x = 0;
            y = 0;
        }
        fout << x << " " << y << "\n";
	}



	fin.close();
	fout.close();

	return 0;
}


#define LONG int

using namespace std;

ifstream fin("euclid3.in");
ofstream fout("euclid3.out");

LONG x, y, d;

void cmmdc(LONG a, LONG b, LONG& d, LONG& x, LONG& y)
{
    if (b == 0)
    {
        x = 1; y = 0; d = a;
    }
    else
    {
        LONG x0, y0;
        cmmdc(b, a % b, d, x0, y0);
        x = y0;
        y = x0 - (a / b) * y0;
    }
}

int main()
{
    LONG n, a, b, c;

    fin >> n;

    for (LONG i = 1; i <= n; i++)
    {
        fin >> a >> b >> c;
        euclid_ext(a, b, &d, &x, &y);
        if (c % d == 0)
            fout << x * c / d << " " << y * c / d << '\n';
        else
            fout << "0 0" << "\n";
    }

    return 0;
}