Cod sursa(job #2088897)

Utilizator PRGCRRaul Cotar PRGCR Data 15 decembrie 2017 23:46:20
Problema Algoritmul lui Euclid extins Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include <iostream>
#include <fstream>
using namespace std;

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

// nu e gata inca.

long long euclid(long long a, long long b)
{
    long long sec;
    while (b!=0) {
        sec=a%b;
        a=b;
        b=sec;
    }
    return a;
}
int main()
{
    long long T,a,x,b,y,c,d,n,m,M;
    int i;
    fin>>T;
    for (i=1; i<=T; i++) {
        fin>>a>>b>>c;
        if (b==0||a==0) {
            y=1;
            d=a+b;
            if (d!=0&&c%d==0)
                fout<<c/d<<" "<<c/d;
            else
                fout<<0<<" "<<0;
        }
        else {
            d=euclid(a,b);
            n=a/d;
            m=b/d;
            M=c/d;
            if (c%d==0) {
                if (n>m) {/**/
                    x=0;
                    while ((1-n*x)%m!=0)
                        x++;
                    y=(1-n*x)/m;
                    fout<<x*M<<" "<<y*M;
                }
                if ((n<=m&&n>=0)||(n<=m&&n<0)) {
                    y=0;
                    while ((1-m*y)%n!=0)
                        y++;
                    x=(1-m*y)/n;
                    fout<<x*M<<" "<<y*M;
                }

            }
            else
                fout<<0<<" "<<0;
        }
        fout<<"\n";
    }
    return 0;
}