結果
| 問題 | No.550 夏休みの思い出(1) |
| コンテスト | |
| ユーザー |
minami
|
| 提出日時 | 2019-04-01 18:01:18 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,776 bytes |
| 記録 | |
| コンパイル時間 | 1,855 ms |
| コンパイル使用メモリ | 181,352 KB |
| 実行使用メモリ | 16,848 KB |
| 最終ジャッジ日時 | 2024-11-25 10:29:24 |
| 合計ジャッジ時間 | 180,793 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | TLE * 3 |
| other | TLE * 55 |
コンパイルメッセージ
main.cpp: In function 'long double bisectionMethod(long double, long double, std::function<long double(long double)>)':
main.cpp:60:1: warning: control reaches end of non-void function [-Wreturn-type]
60 | }
| ^
ソースコード
#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }
#define double long double
const double EPS = 1e-8;
// 二次方程式を解の公式を用いて解く
// 計算量: O(sqrt()の計算量)
// 重解は1つにせず返す 1つにしたいなら変えること
// Verified:
// https://www.urionlinejudge.com.br/judge/en/runs/code/13486899
// http://codeforces.com/contest/233/submission/52143945
vector<double> quadraticEquation(double a, double b, double c) {
// 1次方程式
if (abs(a) < EPS)
return abs(b) < EPS ? vector<double>() : vector<double>{ -c / b };
// 判別式
double D = b * b - 4 * a*c;
// 解無し
if (D < -EPS)
return vector<double>();
// 重解
if (D < EPS)
return vector<double>{-b / (2 * a), -b / (2 * a)};
double d = sqrt(D);
double x1 = (-b + d) / (2 * a);
double x2 = (-b - d) / (2 * a);
if (x1 > x2)swap(x1, x2);
return vector<double>{x1, x2};
}
// 二分法
// 方程式の求根アルゴリズム
// f(l) < 0 かつ 0 < f(r) の場合に限る
double bisectionMethod(double l, double r, function<double(double)> f) {
double yl = f(l), yr = f(r);
if (yl > yr)swap(l, r);
assert((yl - EPS) * (yr + EPS) <= 0);
for (int i = 0; i < 200; i++) {
double m = (l + r) / 2;
if (f(m) > 0)
r = m;
else
l = m;
}
}
// 三次方程式
// 二分法でまず1つの解pを求める
// a(x-p)(x^2+sx+t) と式変形して二次方程式で残りの解を求める
vector<double> cubicEquation(double a, double b, double c, double d) {
if (abs(a) < EPS)
return quadraticEquation(b, c, d);
double p = bisectionMethod(-INF, INF, [&](int x) {return a * x*x*x + b * x*x + c * x + d; });
double t = -d / a / p, s = (c / a - t) / (-p);
dump(s, t);
auto ret = quadraticEquation(1, s, t);
ret.push_back(p);
sort(ret.begin(), ret.end());
dump(ret);
return ret;
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int a, b, c; cin >> a >> b >> c;
auto w = cubicEquation(1, a, b, c);
sort(all(w));
vector<int> v;
rep(i, -1, 2) {
rep(j, 0, 3) {
int x = w[j] + i;
if (x*x*x + a * x*x + b * x + c == 0)
v.push_back(x);
}
}
sort(all(v));
v.erase(unique(v.begin(), v.end()), v.end());
cout << v[0]; rep(i, 1, v.size()) { cout << " " << v[i]; } cout << endl;
return 0;
}
minami