Problem:
Factorization of Quadratic Formula
As the first step in algebra, students learn quadratic formulas and
their factorization.  Often, the factorization is a severe burden
for them.  A large number of students cannot master the factorization;
such students cannot be aware of the elegance of advanced algebra.
It might be the case that the factorization increases the number of people
who hate mathematics.
Your job here is to write a program which helps students of
an algebra course.  Given a quadratic formula, your program should report
how the formula can be factorized into two linear formulas.
All coefficients of quadratic formulas and those of resultant
linear formulas are integers in this problem.
The coefficients
a
,
b
and
c
of a quadratic formula
ax
2
+
bx
+
c
are given.
The values of
a
,
b
and
c
are integers,
and their absolute values do not exceed 10000.
From these values, your program is requested to find four integers
p
,
q
,
r
and
s
, such that
ax
2
+
bx
+
c
=
(
px
+
q
)(
rx
+
s
).
Since we are considering integer coefficients only, it is not
always possible to factorize a quadratic formula into linear formulas.
If the factorization of the given formula is impossible,
your program should report that fact.
Input
The input is a sequence of lines, each representing
a quadratic formula.
An input line is given in the following format.
a
b
c
Each of
a
,
b
and
c
is an integer.
They satisfy the following inequalities.
0 <
a
<= 10000
-10000 <=
b
<= 10000
-10000 <=
c
<= 10000
The greatest common divisor of
a
,
b
and
c
is 1.
That is, there is no integer
k
, greater than 1,
such that all of
a
,
b
and
c
are divisible by
k
.
The end of input is indicated by a line consisting of three 0's.
Output
For each input line, your program should output the four integers
p
,
q
,
r
and
s
in a line, if they exist.
These integers should be output in this order, separated by
one or more white spaces.
If the factorization is impossible, your program should output a line
which contains the string "Impossible" only.
The following relations should hold between the values
of the four coefficients.
p
> 0
r
> 0
(
p
>
r
) or (
p
=
r
and
q
>=
s
)
These relations, together with the fact that the greatest common
divisor of
a
,
b
and
c
is 1,
assure the uniqueness of the solution.
If you find a way to factorize the formula, it is not necessary
to seek another way to factorize it.
Sample Input
2 5 2
1 1 1
10 -7 0
1 0 -3
0 0 0
Output for the Sample Input
2 1 1 2
Impossible
10 -7 1 0
Impossible

Submitted solutions:

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int a, b, c;
    while (cin >> a >> b >> c, a) {
        if (c == 0) {
            int p = a, q = b, r = 1, s = 0;
            if (p == r and q < s) swap(q, s);
            cout << p << " " << q << " " << r << " " << s << "\n";
        } else {
            vector<int> fa, fc;
            for (int i = 1; i <= a; i++) {
                if (a % i == 0) fa.push_back(i);
            }
            for (int i = 1; i <= abs(c); i++) {
                if (c % i == 0) {
                    fc.push_back(-i);
                    fc.push_back(i);
                }
            }
            int p = -1, q = -1, r = -1, s = -1;
            for (int pp : fa) {
                for (int qq : fc) {
                    int rr = a / pp;
                    int ss = c / qq;
                    if (pp * ss + qq * rr == b) {
                        p = pp, q = qq, r = rr, s = ss;
                    }
                }
            }
            if (p < r) {
                swap(p, r);
                swap(q, s);
            } else if (p == r) {
                if (q < s) swap(q, s);
            }
            if (p != -1) {
                cout << p << " " << q << " " << r << " " << s << "\n";
            } else {
                cout << "Impossible\n";
            }
        }
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3156
final_rank: 12

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    while(1){
        int a,b,c;
        cin >> a >> b >> c;
        if(a == 0) break;

        vector<int> factor_a;
        for(int i=1; i<=a; i++){
            if(a%i == 0) factor_a.push_back(i);
        }
        vector<int> factor_c;
        for(int i=1; i<=abs(c); i++){
            if(abs(c)%i == 0){
                factor_c.push_back(i);
                factor_c.push_back(-i);
            }
        }
        sort(factor_c.begin(), factor_c.end());

        bool found = false;
        for(auto r: factor_a){
            for(auto s: factor_c){
                int p = a/r;
                int q = c/s;    
                if(p*s +r*q == b){
                    cout << p << " " << q << " " << r << " " << s << endl;
                    found = true;
                    break;
                }
            }
            if(found) break;
        }
        if(c == 0){
            if(a == 1 and b < 0){
                cout << "1 0 1 " << b << endl;
            }else{
                cout << a << " " << b << " 1 0" << endl;
            }
            found = true;
        }
        if(!found){
            cout << "Impossible" << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3136
final_rank: 13

code_snippet: #include<iostream>
using namespace std;

int main(){
    int a, b, c;
    while(cin >> a >> b >> c, a > 0){
        int p, q, r, s;
        bool found = false;
        for(p = a; p*p >= a; p--){
            if(a%p) continue;
            r = a/p;
            for(q = -abs(c); q <= abs(c); q++){
                if(q == 0){
                    if(c != 0)  continue;
                    if(b%p == 0){
                        found = true;
                        s = b/p;
                        break;
                    }
                    if(b%r == 0){
                        found = true;
                        s = 0;
                        q = b/r;
                        break;
                    }
                }
                if(c%abs(q))    continue;
                s = c/q;
                if(p*s+q*r == b){
                    found = true;
                    break;
                }
            }
            if(found)   break;
        }
        if(found){
            if(p == r && q < s) swap(q, s);
            cout << p << " " << q << " " << r << " " << s << endl;
        }else{
            cout << "Impossible" << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 3044
final_rank: 14

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int A,B,C;

void func(){

	int left = -1*max(abs(B),abs(C));
	int right = max(abs(B),abs(C));
	int p,q;

	for(int r = 1; r*r <= A; r++){
		if(A%r != 0)continue;
		p = A/r;
		for(int s = left; s <= right; s++){
			if(s != 0 && C%s != 0)continue;
			if(s != 0)q = C/s;
			else{
				q = B/r;
			}
			if(q != 0 && C%q != 0)continue;
			if(p ==r && s > q)continue;

			if(p*r == A && (p*s+q*r) == B && q*s == C){
				printf("%d %d %d %d\n",p,q,r,s);
				return;
			}
		}
	}
	printf("Impossible\n");
}

int main(){

	while(true){
		scanf("%d %d %d",&A,&B,&C);
		if(A == 0 && B == 0 && C == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 3092
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
  long long a,b,c;
  while(cin>>a>>b>>c,a){
    if(c==0){
      if(a==1&&b<0)cout<<a<<' '<<0<<' '<<1<<' '<<b<<endl;
      else cout<<a<<' '<<b<<' '<<1<<' '<<0<<endl;
    }
    else{
      for(int j=1;j<=a;j++){
        if(a%j==0){
          if(a/j>=j)
            for(int i=10000;i>=-10000;i--){
              long long t,p=0;
              if(i*j+a/j*p==b&&p*i==c){
                cout<<a/j<<' '<<i<<' '<<j<<' '<<p<<endl;
                goto L;
              }
              if(i!=0){
                if(c%abs(i)==0)t=c/i;
                else continue;
              }
              else continue;
              if(i*j+a/j*t==b&&t*i==c){
                if(j*j==a&&i<t)cout<<a/j<<' '<<t<<' '<<j<<' '<<i<<endl;
                else cout<<a/j<<' '<<i<<' '<<j<<' '<<t<<endl;
                goto L;
              }
            }
          else break;
        }
      }
      cout<<"Impossible"<<endl;L:;
    }
  }
}
accuracy: 1
time_ms: 790
memory_kb: 3092
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;

int main()
{
  int a, b, c;
  while( cin >> a >> b >> c ) {
    if( a == 0 && b == 0 && c == 0 ) break;
    int p = 2*a, r = 2*a;
    int q = -(-b+(sqrt((double)(b*b-4*a*c))));
    int s = -(-b-(sqrt((double)(b*b-4*a*c))));
    //int tmp = (int)sqrt((double)(b*b-4*a*c));
    //cout << b*b-4*a*c << endl;
    //cout << sqrt((double)b*b-4*a*c) << endl;
    //cout << q << " " << s << endl;
    int tmp = (int)sqrt((double)(b*b-4*a*c));
    //cout << tmp << endl;
    if( abs(sqrt((double)(b*b-4*a*c)) - (double)tmp) != 0 || b*b-4*a*c < 0 ) {
      cout << "Impossible" << endl;
    } else { 
      //cout << sqrt((double)(b*b-4*a*c)) << endl;
      //cout << q << " " << s << endl;
      for(int i=100000; i>1; i--) {
	if( !(p % i) && !(q % i) ) {
	  p /= i; q /= i;
	}
	if( !(r % i) && !(s % i) ) {
	  r /= i; s /= i;
	}
	
	
      }

      if( p < r ) {
	swap(p, r);
	swap(q, s);
      }
      if( p == r ) {
	if( q < s ) {
	  swap(q, s);
	}
      }
      cout << p << " " << q << " " << r << " " << s << endl;
    }
  }
}
accuracy: 1
time_ms: 400
memory_kb: 3140
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
  long long a,b,c;
  while(cin>>a>>b>>c,a){
    if(c==0){
      if(a==1&&b<0)cout<<a<<' '<<0<<' '<<1<<' '<<b<<endl;
      else cout<<a<<' '<<b<<' '<<1<<' '<<0<<endl;
    }
    else{for(int j=1;j<=a;j++){
	if(a%j==0){
	  if(a/j>=j)
	    for(int i=10000;i>=-10000;i--){
	      long long t,p=0;
	      if(i*j+a/j*p==b&&p*i==c){
		cout<<a/j<<' '<<i<<' '<<j<<' '<<p<<endl;
		goto L;
	      }
	      if(i!=0){
		if(c%abs(i)==0)t=c/i;
		else continue;
	      }
	      else continue;
	      if(i*j+a/j*t==b&&t*i==c){
		if(j*j==a&&i<t)	  cout<<a/j<<' '<<t<<' '<<j<<' '<<i<<endl;
		else cout<<a/j<<' '<<i<<' '<<j<<' '<<t<<endl;
		goto L;
	      }
	    }
	  else break;
	}
      }
      cout<<"Impossible"<<endl;L:;
    }

  }
}
accuracy: 1
time_ms: 770
memory_kb: 3076
final_rank: 19

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-5;
const int inf=1e8;
int main(){
	int a,b,c;
	while(cin>>a>>b>>c,a||b||c){
		for(int sa=1;sa*sa<=a;sa++)if(a%sa==0){
			int la=a/sa;
			for(int sb=-10000;sb<=10000;sb++){
				if(sb&&c%sb)continue;
				int lb=(sb?c/sb:0);
				rep(i,2){
					rep(j,2){
						if(sa*lb+la*sb==b){
							if(la==sa&&lb<sb)swap(lb,sb);
							cout<<la<<" "<<lb<<" "<<sa<<" "<<sb<<endl;
							goto end;
						}
						sb*=-1;
						lb*=-1;
					}
					swap(sb,lb);
				}
			}
		}
		cout<<"Impossible"<<endl;
		end:;
	}
}
accuracy: 1
time_ms: 250
memory_kb: 3076
final_rank: 17

code_snippet: #include<iostream>
#include<vector>
using namespace std;
int a, b, c; vector<int>p, q;
int main() {
	while (true) {
		cin >> a >> b >> c; p.clear(); q.clear(); if (a == 0)break;
		if (c == 0) {
			if (b < 0 && a == 1)cout << a << " " << "0" << " " << "1" << " " << b << endl;
			else { cout << a << ' ' << b << ' ' << "1" << ' ' << "0" << endl; }
			goto E;
		}
		for (int i = -10000; i <= 10000; i++) {
			if (i == 0)continue;
			if (a%i == 0)p.push_back(i);
			if (c%i == 0)q.push_back(i);
		}
		for (int i = 0; i < p.size(); i++) {
			for (int j = 0; j < q.size(); j++) {
				int sum = p[i] * (c / q[j]) + (a / p[i])*q[j];
				if (p[i] < a / p[i] || p[i] < 0 || a / p[i] < 0)continue;
				if (p[i] == a / p[i] && q[j] < c / q[j])continue;
				if (sum == b) { cout << p[i] << ' ' << q[j] << ' ' << a / p[i] << ' ' << c / q[j] << endl; goto E; }
			}
		}
		cout << "Impossible" << endl; E:;
	}
	return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 1224
final_rank: 6

code_snippet: #include <bits/stdc++.h>
 
using namespace std;
 
#define MAX 10000
typedef pair<int,int> pii;
int a,b,c;
 
bool input(){
    cin >> a >> b >> c;
    return a;
}
 
int main(){
    while(input()){
	bool found = false;
	vector<pii> pr,qs;
	for(int p = 1 ; p <= a ; p++){
	    int r = a/p;
	    if(a % p != 0 || p < r) continue;
	    pr.push_back(pii(p,r));
	}
	int cc = c < 0 ? -c : c;
	for(int q = -cc ; q <= cc ; q++){
	    if(q == 0) continue;
	    int s = 0;
	    if(c < 0){
		if(q < 0 && abs(c) % abs(q) == 0){
		    if(abs(c) % abs(q) == 0){
			s = abs(c)/abs(q);
		    }
		}else{
		    if(abs(c) % q == 0){
			s = abs(c)/q;
			s = -s;
		    }
		}
	    }else{
		if(q < 0){
		    if(c % abs(q) == 0){
			s = c/abs(q);
			s = -s;
		    }
		}else{
		    if(c % q == 0){
			s = c/q;
		    }
		}
	    }
	    if(s == 0) continue;
	    qs.push_back(pii(q,s));
	}
	if(c == 0){
	    for(int i = -MAX ; i <= MAX ; i++){
		qs.push_back(pii(0,i));
		qs.push_back(pii(i,0));
	    }
	}
	for(int i = 0 ; i < (int)pr.size() ; i++){
	    for(int j = 0 ; j < (int)qs.size() ; j++){
		int p = pr[i].first,r = pr[i].second;
		int q = qs[j].first,s = qs[j].second;
		if(p == r && q < s) continue;
		if(p*s+q*r == b){
		    cout << p << " " << q << " " << r << " " << s << endl;
		    found = true;
		    goto END;
		}
	    }
	}
	if(!found){
	    cout << "Impossible" << endl;
	}
    END:;
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3536
final_rank: 16

code_snippet: #include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;

int a, b, c;

void solve(){
  FOR(p, 1, 10001){
    if(a % p) continue;
    int r = a / p;
    FOR(q, -10001, 10001){
      int s;
      if(q == 0) s = b / p;
      else s = c / q;
      if(p < r || (r == p && q < s)) continue;
      if(p * r == a && p * s + r * q == b && q * s == c){
        cout <<p <<" " <<q <<" " <<r <<" " <<s <<endl;
        return ;
      }
    }
  }
  cout <<"Impossible" <<endl;
}

int main() {
  while(cin >>a >>b >>c && a) solve();
  return 0;
}
accuracy: 1
time_ms: 600
memory_kb: 1160
final_rank: 11

code_snippet: #include <iostream>
#include <algorithm>

using namespace std;

void output(int p, int q, int r, int s){
	if(p < r){
		swap(p, r);
		swap(q, s);
	}
	if(p == r && q < s){
		swap(q,s);
	}
	cout << p << " " << q << " " << r << " " << s << endl;
}
bool test(int a, int b, int c){
	if(b == 0 && c == 0){
		output(a, 0, 1, 0);
		return true;
	}
	for(int i = 1; i <= a; ++i){
		if(a%i == 0){
			int r = a/i;
			for(int j = -10000; j <= 10000; ++j){
				if(j == 0){
					continue;
				}
				if(c%j == 0){
					int s = c/j;
					if(i*s+j*r == b){
						output(i, j, r, s);
						return true;
					}
				}
			}
		}
	}
	return false;
}

int main(){
	int a, b, c;
	cin >> a >> b >> c;
	while(a | b |c){
		if(!test(a,b,c)){
			cout << "Impossible" << endl;
		}
		cin >> a >> b >> c;
	}
}
accuracy: 1
time_ms: 420
memory_kb: 1164
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
 
int main() {
  int a,b,c;
  while(cin >> a >> b >> c && (a||b||c)) {
    if(c==0) {
      if(a!=1)cout << a << " " << b << " " << 1 << " " << 0 << endl;
      else cout << a << " " << max(b,0) << " " << 1 << " " << min(b,0) << endl;
      continue;
    }
    for(int r=1; r<=a; r++) {
      if(a%r) continue;
      int p=a/r;
      for(int s=-10000; s<=10000; s++) {
    if(s==0) continue;
    if(c%s) continue;
    int q=c/s;
    if(p>0 && r>0 && (p>r || (p==r && q>=s))) {
      if(p*s+q*r==b) {
        cout << p << " " << q << " " << r << " " << s << endl;
        goto end;
      }
    }
      }
    }
    cout << "Impossible" << endl;
    end:;
  }
  return 0;
}
accuracy: 1
time_ms: 420
memory_kb: 1164
final_rank: 9

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define MAX 10000
typedef pair<int,int> pii;
int a,b,c;

bool input(){
  cin >> a >> b >> c;
  return a;
}

int main(){
  while(input()){
    bool found = false;
    vector<pii> pr,qs;
    for(int p = 1 ; p <= a ; p++){
      int r = a/p;
      if(a % p != 0 || p < r) continue;
      pr.push_back(pii(p,r));
    }
    int cc = c < 0 ? -c : c;
    for(int q = -cc ; q <= cc ; q++){
      if(q == 0) continue;
      int s = 0;
      if(c < 0){
        if(q < 0 && abs(c) % abs(q) == 0){
          if(abs(c) % abs(q) == 0){
            s = abs(c)/abs(q);
          }
        }else{
          if(abs(c) % q == 0){
            s = abs(c)/q;
            s = -s;
          }
        }
      }else{
        if(q < 0){
          if(c % abs(q) == 0){
            s = c/abs(q);
            s = -s;
          }
        }else{
          if(c % q == 0){
            s = c/q;
          }
        }
      }
      if(s == 0) continue;
      qs.push_back(pii(q,s));
    }
    if(c == 0){
      for(int i = -MAX ; i <= MAX ; i++){
        qs.push_back(pii(0,i));
        qs.push_back(pii(i,0));
      }
    }
    for(int i = 0 ; i < (int)pr.size() ; i++){
      for(int j = 0 ; j < (int)qs.size() ; j++){
        int p = pr[i].first,r = pr[i].second;
        int q = qs[j].first,s = qs[j].second;
        if(p == r && q < s) continue;
        if(p*s+q*r == b){
          cout << p << " " << q << " " << r << " " << s << endl;
          found = true;
          goto END;
        }
      }
    }
    if(!found){
      cout << "Impossible" << endl;
    }
  END:;
  }
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 1844
final_rank: 8

code_snippet: #include <iostream>
using namespace std;

int a, b, c;

int gcd(int x, int y){
    int t;
    if( x < y ){
        t = y;
        y = x;
        x = t;
    }
    while( y ){
        t = x%y;
        x = y;
        y = t;
    }
    return x;
}

int abs(int x){
    return x<0?-x:x;
}

int main(){
    cin >> a >> b >> c;
    while( a || b || c ){
        bool possible = true;
        int d = 0, x1, x2, u1, u2, s;
        while( d*d < b*b - 4*a*c ) d++;
        possible = (d*d == b*b - 4*a*c);
        if( possible ){
            s = (- b - d)/abs(- b - d);
            x1 = abs(- b - d);
            if( x1 ){
                u1 = 2*a/gcd(x1, 2*a);
                x1 = -s*x1/gcd(x1, 2*a);
                u2 = a/u1;
                x2 = c/x1;
            }else{
                u1 = 1;
                u2 = a;
                x2 = b;
            }
            if( u1 < u2 || (u1 == u2 && x1 < x2) ){
                int t = x1;
                x1 = x2;
                x2 = t;
                t = u1;
                u1 = u2;
                u2 = t;
            }
            cout << u1 << ' ' << x1 << ' ' << u2 << ' ' << x2 << endl;
        }else{
            cout << "Impossible" << endl;
        }
        cin >> a >> b >> c;
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1156
final_rank: 2

code_snippet: #include<iostream>
#include<cmath>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

void output(int p, int q, int r, int s) {
  if (p > r || (p == r && q >= s)) cout << p << " " << q << " " << r << " " << s << endl;
  else cout << r << " " << s << " " << p << " " << q << endl;
}

int main() {
  while (true) {
    int a, b, c;
    cin >> a >> b >> c;
    if (a == 0) break;
    rep (i, a + 1) if (i > 0 && a % i == 0) for (int j = -abs(c); j <= abs(c); ++j) if (j != 0 && c % j == 0) {
	  int ii = a / i;
	  int jj = c / j;
	  if (i * jj + ii * j != b) continue;
	  output(i, j, ii, jj);
	  goto next;
	}
    if (c == 0) {
      output(a, b, 1, 0);
      goto next;
    }
    cout << "Impossible" << endl;
  next:;
  }
}
accuracy: 1
time_ms: 190
memory_kb: 1164
final_rank: 7

code_snippet: #include <iostream>
#include <cmath>
using namespace std;
double gc(double x,double y){
  if(x<0) x=-x;
  if(y<0) y=-y;
  if(x==0) return y;
  if(y==0) return x;
  while(x!=y){
    // cout <<x<<"tttt "<<y<<endl;
    if(x>y)
      x=x-y;
    if(y>x)
      y=y-x;

  }
  return x;
}
int test(int a,int b,int c){
  int t=b*b-4*a*c;
  for(int i=0; i*i<=t; i++)
    if(i*i==t) 
      return 0;
  
  return 1;
}
int main(){
  double a,b,c,p,q,r,s,qq,ww,ee,rr;
  int hanbetu;
  
  while(1){
    cin >>a>>b>>c;
    // cin >>qq>>ww>>ee>>rr;
    //a=qq*ee;b=qq*rr+ww*ee;c=ww*rr;
    if(a==0&&b==0&&c==0) break;
    hanbetu=b*b-4*a*c;
    //    if(hanbetu<0||sqrt(hanbetu)!=(double)((int)sqrt(hanbetu)) ) {
    if(hanbetu<0||test(a,b,c) ) {     
      cout<<"Impossible"<<endl;
     
      continue;
    }
    q=b-sqrt(hanbetu);
    s=b+sqrt(hanbetu);
    //cout<<q<<' '<<s<<endl;
    p=gc(2*a,q);    r=gc(2*a,s);
    q=q/p; s=s/r;
    p=2*a/p; r=2*a/r;
    if(p>r||(p==r&&q>=s))
      cout <<p<<' '<<q<<' '<<r<<' '<<s<<endl;
    else cout<<r<<' '<<s<<' ' <<p<<' '<<q<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1280
final_rank: 3

code_snippet: #include <cstdio>
#include <algorithm>

using namespace std;

int a, b, c, r, p, q, s, x, y, t, i, f;

int solve(){
  t = -1;
  f = b * b - 4 * a * c;
  for(i=0;i*i<=f;i++) if(i*i == f) t = i;
  if(t >= 0){
    x = t - b;
    y = -(t + b);
    for(r=1;r<=a;r++){
      if(!(a%r)){
        p = a / r;
        for(i=0;i<2;i++){
          q = -p * x / (2 * a);
          s = -r * y / (2 * a);
          if((-p * x) % (2 * a) == 0 && (-r * y) % (2 * a) == 0 && (p > r || p == r && q >= s)) return 1;
          swap(x, y);
        }
      }
    }
  }
  return 0;
}

main(){
  while(scanf("%d%d%d",&a,&b,&c) && (a|b|c)){
    if(solve()) printf("%d %d %d %d\n",p,q,r,s);
    else printf("Impossible\n");
  }
}
accuracy: 1
time_ms: 10
memory_kb: 1040
final_rank: 1

code_snippet: #include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long lli;
 
void solve(lli a, lli b, lli c) {
  vector<lli> fa, fc;
  for(lli i = 1; i <= a; ++i) {
    if(a % i == 0) fa.push_back(i);
  }
  for(lli i = 1; i <= labs(c); ++i) {
    if(c % i == 0) {
      fc.push_back(i);
      fc.push_back(-i);
    }
  }
 
  if(c == 0) {
 
    for(lli i = 0; i < fa.size(); ++i) {
      for(lli k = 0; k < 2; ++k) {
    lli p = fa[i];
    lli r = a / fa[i];
    lli q, s;
    if(k == 0) {
      if(b % p != 0) continue;
      q = 0;
      s = b / p;
    } else {
      if(b % r != 0) continue;
      s = 0;
      q = b / r;
    }
    if((p > r || (p == r && q >= s)) && p*s + q*r == b) {
      cout << p << " " << q << " " << r << " " << s << endl;
      return;
    }
      }
    }
 
  } else {
 
    for(lli i = 0; i < fa.size(); ++i) {
      for(lli j = 0; j < fc.size(); ++j) {
    lli p = fa[i];
    lli r = a / fa[i];
    lli q = fc[j];
    lli s = c / fc[j];
    if((p > r || (p == r && q >= s)) && p*s + q*r == b) {
      cout << p << " " << q << " " << r << " " << s << endl;
      return;
    }
      }
    }
 
  }
  cout << "Impossible" << endl;
}
 
int main() {
  lli a, b, c;
  while(cin >> a >> b >> c && (a|b|c)) {
    solve(a,b,c);
  }
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 1216
final_rank: 4

code_snippet: #include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long lli;

void solve(lli a, lli b, lli c) {
  vector<lli> fa, fc;
  for(lli i = 1; i <= a; ++i) {
    if(a % i == 0) fa.push_back(i);
  }
  for(lli i = 1; i <= labs(c); ++i) {
    if(c % i == 0) {
      fc.push_back(i);
      fc.push_back(-i);
    }
  }

  if(c == 0) {

    for(lli i = 0; i < fa.size(); ++i) {
      for(lli k = 0; k < 2; ++k) {
	lli p = fa[i];
	lli r = a / fa[i];
	lli q, s;
	if(k == 0) {
	  if(b % p != 0) continue;
	  q = 0;
	  s = b / p;
	} else {
	  if(b % r != 0) continue;
	  s = 0;
	  q = b / r;
	}
	if((p > r || (p == r && q >= s)) && p*s + q*r == b) {
	  cout << p << " " << q << " " << r << " " << s << endl;
	  return;
	}
      }
    }

  } else {

    for(lli i = 0; i < fa.size(); ++i) {
      for(lli j = 0; j < fc.size(); ++j) {
	lli p = fa[i];
	lli r = a / fa[i];
	lli q = fc[j];
	lli s = c / fc[j];
	if((p > r || (p == r && q >= s)) && p*s + q*r == b) {
	  cout << p << " " << q << " " << r << " " << s << endl; 
	  return;
	}
      }
    }

  }
  cout << "Impossible" << endl;
}

int main() {
  lli a, b, c;
  while(cin >> a >> b >> c && (a|b|c)) {
    solve(a,b,c);
  }
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 1216
final_rank: 4