Skip to content

Instantly share code, notes, and snippets.

@hankhsu1996
Created October 11, 2017 17:11
Show Gist options
  • Save hankhsu1996/6207e3fbaa21dc5ebe0e0c3e2f1d7ca8 to your computer and use it in GitHub Desktop.
Save hankhsu1996/6207e3fbaa21dc5ebe0e0c3e2f1d7ca8 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// x3 + ax2 + bx + c; x2 + gx + h
long long int a, b, c, d, e, f, g, h, j, temp, z;
cin >> a >> b >> c;
// cout << "a = " << a << ", b = " << b << ", c = " << c << endl;
if(a==0 && b==0 && c==0){
cout << "0 0 0";
return 0;
}
else if(b==0 && c==0){
d = 0; e = 0; f = a;
}
else if(c==0){
f = 0;
g = a;
h = b;
for(int j=abs(b/2); j>=-abs(b/2); j--){
z = pow(-j, 2) + g * (-j) + h;
// cout << z << endl;
if(z == 0){
d = j;
e = b / d;
// cout << "d = " << d << ", e = " << e << endl;
}
}
}
else{
// cout << "Yes";
for(int i=abs(c/2); i>=-abs(c/2); i--){
if(i==0 || c%i == 0){
// cout << "here" << endl;
z = pow(-i, 3) + a * pow(-i, 2) + b * (-i) + c;
// cout << "z = " << z << ", i = " << i << endl;
if(z == 0){
// cout << "f = " << i << endl;
f = i;
g = a - f;
h = c / f;
// cout << g << ' ' << h << endl;
// 現在 f 已經確定,用長除法變成二次式
for(int j=i; j>=-abs(c/2); j--){
z = pow(-j, 2) + g * (-j) + h;
// cout << "z = " << z << ", j = " << j << endl;
if(z == 0){
e = j;
d = c / i / j;
goto end;
}
}
}
}
}
}
end:
if(d>e){
temp = d;
d = e;
e = temp;
}
if(e>f){
temp = f;
f = e;
e = temp;
}
if(d>e){
temp = d;
d = e;
e = temp;
}
cout << d << ' ' << e << ' ' << f;
}
@haochunchang
Copy link

Nee How ban!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment