Cod sursa(job #2413447)

Utilizator robx12lnLinca Robert robx12ln Data 23 aprilie 2019 13:43:38
Problema Ecuatie Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.06 kb
#include<bits/stdc++.h>
using namespace std;
ifstream fin("ecuatie.in");
ofstream fout("ecuatie.out");

int A, B, C, K, rd;
inline int modul( int x ){
    return (x >= 0) ? x : -x;
}

vector< pair< pair<int,int>, pair<int,int> > > S;
inline void add( int P1, int P2 ){
    int Q1, Q2;
    ///caz1
    if( (1LL * (B + rd) * P1) % (2 * A) == 0 && (1LL * (B - rd) * P2) % (2 * A) == 0 ){
        Q1 = 1LL * (B + rd) * P1 / (2 * A);
        Q2 = 1LL * (B - rd) * P2 / (2 * A);
        S.push_back( { {P1, Q1}, {P2, Q2} } );
    }
    ///caz2
    if( (1LL * (B - rd) * P1) % (2 * A) == 0 && (1LL * (B + rd) * P2) % (2 * A) == 0 ){
        Q1 = 1LL * (B - rd) * P1 / (2 * A);
        Q2 = 1LL * (B + rd) * P2 / (2 * A);
        S.push_back( { {P1, Q1}, {P2, Q2} } );
    }
}

inline void afisare( int K ){
    fout << "(";
    if( S[K - 1].first.first < 0 )
        fout << "-";
    if( modul( S[K - 1].first.first ) != 1 )
        fout << modul( S[K - 1].first.first );
    fout << "x";
    if( S[K - 1].first.second >= 0 )
        fout << "+";
    fout << S[K - 1].first.second;
    fout << ")";

    fout << "(";
    if( S[K - 1].second.first < 0 )
        fout << "-";
    if( modul( S[K - 1].second.first ) != 1 )
        fout << modul( S[K - 1].second.first );
    fout << "x";
    if( S[K - 1].second.second >= 0 )
        fout << "+";
    fout << S[K - 1].second.second;
    fout << ")";
}

int main(){

    fin >> A >> B >> C >> K;
    long long delta = 1LL * B * B - 4LL * A * C;
    rd = (int)sqrt( 1.0 * delta );

    if( 1LL * rd * rd != delta ){
        fout << "-1\n";
        return 0;
    }

    for( int d = 1; d * d <= modul(A); d++ ){
        if( modul(A) % d == 0 ){
            add( d, A / d );
            add( A / d, d );
            add( -d, A / (-d) );
            add( A / (-d), -d );
        }
    }

    sort( S.begin(), S.end() );
    S.resize( distance( S.begin(), unique( S.begin(), S.end() ) ) );
    if( S.size() < K ){
        fout << "-1\n";
        return 0;
    }

    afisare( K );
    return 0;
}