結果

問題 No.955 ax^2+bx+c=0
ユーザー trineutrontrineutron
提出日時 2019-12-07 18:38:26
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,343 bytes
コンパイル時間 1,269 ms
コンパイル使用メモリ 168,084 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-06 23:30:20
合計ジャッジ時間 3,433 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 120 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int64_t a, b, c, det;

double f(double x) {
    double plus = 0.0, minus = 0.0, ax2 = a * x * x, bx = b * x;
    if (ax2 >= 0) {
        plus += ax2;
    } else {
        minus += ax2;
    }
    if (bx >= 0) {
        plus += bx;
    } else {
        minus += bx;
    }
    if (c >= 0) {
        plus += c;
    } else {
        minus += c;
    }
    return plus + minus;
}

int main() {
    cin >> a >> b >> c;
    assert(-1000000000 <= a && a <= 1000000000);
    assert(-1000000000 <= b && b <= 1000000000);
    assert(-1000000000 <= c && c <= 1000000000);
    det = b * b - 4 * a * c;
    cout << setprecision(17);
    if (a == 0) {
        if (b == 0) {
            if (c == 0) {
                cout << -1 << endl;
            } else {
                cout << 0 << endl;
            }
        } else {
            cout << 1 << endl;
            cout << (double) -c / b << endl;
        }
    } else {
        if (det < 0) {
            cout << 0 << endl;
        } else if (det == 0) {
            cout << 1 << endl;
            cout << (double) -b / (2 * a) << endl;
        } else {
            cout << 2 << endl;
            double lower_bound = -10000000000, upper_bound = (double) -b / (2 * a);
            while (lower_bound < upper_bound) {
                double half = (lower_bound + upper_bound) / 2;
                if (f(lower_bound) == 0) break;
                if (f(lower_bound) * f(half) < 0) {
                    if (upper_bound == half) break;
                    upper_bound = half;
                } else {
                    if (lower_bound == half) break;
                    lower_bound = half;
                }
            }
            cout << lower_bound << endl;
            lower_bound = (double) -b / (2 * a);
            upper_bound = 10000000000;
            while (lower_bound < upper_bound) {
                double half = (lower_bound + upper_bound) / 2;
                if (f(lower_bound) == 0) break;
                if (f(lower_bound) * f(half) < 0) {
                    if (upper_bound == half) break;
                    upper_bound = half;
                } else {
                    if (lower_bound == half) break;
                    lower_bound = half;
                }
            }
            cout << lower_bound << endl;
        }
    }
}
0