UVa : 11565 (Simple Equations)


  1. x * y * z = B. That means, x, y and z are divisors of B
  2. values of x, y and z can be negative
  3. problem statement says, we have 3 different integer x, y, and z. so, (x != y && y != z && z != x)

// http://uva.onlinejudge.org/external/115/11565.html
// Runtime : 0.008s
// Tag: math


#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

int main ()
{
    int testCase;
    scanf ("%d", &testCase);

    while ( testCase-- ) {
        int a, b, c;
        scanf ("%d %d %d", &a, &b, &c);

        vector <int> values;
        values.push_back (1);
        values.push_back (-1);

        for ( int i = 2; i <= b / 2; i++ ) {
            if ( b % i == 0 ) {
                values.push_back (i);
                values.push_back (b / i);
                values.push_back (i * -1);
                values.push_back (b / i * -1);
            }
        }

        sort (values.begin (), values.end ());

        bool frq [10000 + 10];
        memset (frq, false, sizeof (frq));
        bool printed = false;

        for ( unsigned int i = 0; i < values.size (); i++ ) frq [values [i]] = true;

        for ( unsigned int i = 0; i < values.size (); i++ ) {
            for ( unsigned int j = 0; j < values.size (); j++ ) {
                int x = values [i];
                int y = values [j];
                int z = a - ( x + y);
                if ( frq [z] && x * y * z == b && x*x + y*y + z*z == c && x != y && y != z && z != x ) {
                    printf("%d %d %d\n", x, y, z);
                    printed = true;
                    i = j = values.size ();
                }
            }
        }

        if ( !printed ) printf ("No solution.\n");
    }

    return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s