library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub yuruhi/library

:heavy_check_mark: test/QuadraticEquation.test.cpp

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/no/1179"
#define ERROR "1e-4"
#include "./../math/QuadraticEquation.cpp"
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;

int main() {
	int a, b, c;
	cin >> a >> b >> c;
	auto ans = *QuadraticEquation<long double>(a, b, c);
	sort(ans.begin(), ans.end());
	if (ans.empty()) {
		puts("imaginary");
	} else if (ans.size() == 1) {
		printf("%.6Lf\n", ans[0]);
	} else if (ans.size() == 2) {
		printf("%.6Lf %.6Lf\n", ans[0], ans[1]);
	} else {
		assert(false);
	}
}
#line 1 "test/QuadraticEquation.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/1179"
#define ERROR "1e-4"
#line 2 "math/QuadraticEquation.cpp"
#include <vector>
#include <optional>
#include <cmath>

template <class T>
std::optional<std::vector<T>> QuadraticEquation(long long a, long long b, long long c) {
	T A = a, B = b, C = c;
	if (a == 0 && b == 0 && c == 0) {
		return std::nullopt;
	} else if (a == 0 && b == 0) {
		return std::vector<T>();
	} else if (a == 0) {
		return std::vector{-(C / B)};
	} else {
		long long d = b * b - 4 * a * c;
		T D = static_cast<T>(d);
		if (d < 0) {
			return std::vector<T>();
		} else if (d == 0) {
			return std::vector{-B / (2 * A)};
		} else {
			T ans1 = 0, ans2 = 0;
			if (b > 0) {
				ans1 = (-B - std::sqrt(D)) / (2 * A);
			} else {
				ans1 = (-B + std::sqrt(D)) / (2 * A);
			}
			ans2 = (C / A) / ans1;
			return std::vector{std::min(ans1, ans2), std::max(ans1, ans2)};
		}
	}
}
#line 4 "test/QuadraticEquation.test.cpp"
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;

int main() {
	int a, b, c;
	cin >> a >> b >> c;
	auto ans = *QuadraticEquation<long double>(a, b, c);
	sort(ans.begin(), ans.end());
	if (ans.empty()) {
		puts("imaginary");
	} else if (ans.size() == 1) {
		printf("%.6Lf\n", ans[0]);
	} else if (ans.size() == 2) {
		printf("%.6Lf %.6Lf\n", ans[0], ans[1]);
	} else {
		assert(false);
	}
}
Back to top page