結果

問題 No.955 ax^2+bx+c=0
ユーザー risujiroh
提出日時 2019-12-18 03:14:00
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,008 bytes
コンパイル時間 1,609 ms
コンパイル使用メモリ 172,236 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-18 22:07:02
合計ジャッジ時間 4,217 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 122
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using R = long double;
constexpr R eps = 1e-15;

vector<R> solve_quadratic_equation(R a, R b, R c) {
  R d = b * b - 4 * a * c;
  if (d < -eps) {
    return {};
  }
  if (d < eps) {
    return {-b / (2 * a)};
  }
  if (b < 0) {
    return {
      (b * b - d) / (-b + sqrt(d)) /  (2 * a),
      (-b + sqrt(d)) / (2 * a)
    };
  } else {
    return {
      (-b - sqrt(d)) / (2 * a),
      (b * b - d) / (-b - sqrt(d)) / (2 * a)
    };
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  long long a, b, c;
  cin >> a >> b >> c;
  if (a == 0) {
    if (b == 0) {
      if (c == 0) {
        cout << "-1\n";
      } else {
        cout << "0\n";
      }
    } else {
      cout << "1\n";
      cout << (R)-c / b;
    }
  } else {
    auto res = solve_quadratic_equation(a, b, c);
    sort(begin(res), end(res));
    cout << res.size() << '\n';
    for (auto e : res) {
      cout << e << '\n';
    }
  }
}
0