結果
問題 | No.2447 行列累乗根 |
ユーザー | Aeren |
提出日時 | 2023-08-26 16:43:01 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 312 ms / 2,000 ms |
コード長 | 12,504 bytes |
コンパイル時間 | 4,514 ms |
コンパイル使用メモリ | 361,440 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-07 12:19:04 |
合計ジャッジ時間 | 10,650 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 3 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 3 ms
5,376 KB |
testcase_07 | AC | 3 ms
5,376 KB |
testcase_08 | AC | 3 ms
5,376 KB |
testcase_09 | AC | 3 ms
5,376 KB |
testcase_10 | AC | 2 ms
5,376 KB |
testcase_11 | AC | 2 ms
5,376 KB |
testcase_12 | AC | 3 ms
5,376 KB |
testcase_13 | AC | 3 ms
5,376 KB |
testcase_14 | AC | 3 ms
5,376 KB |
testcase_15 | AC | 2 ms
5,376 KB |
testcase_16 | AC | 2 ms
5,376 KB |
testcase_17 | AC | 3 ms
5,376 KB |
testcase_18 | AC | 4 ms
5,376 KB |
testcase_19 | AC | 31 ms
5,376 KB |
testcase_20 | AC | 286 ms
5,376 KB |
testcase_21 | AC | 302 ms
5,376 KB |
testcase_22 | AC | 276 ms
5,376 KB |
testcase_23 | AC | 286 ms
5,376 KB |
testcase_24 | AC | 312 ms
5,376 KB |
testcase_25 | AC | 299 ms
5,376 KB |
testcase_26 | AC | 287 ms
5,376 KB |
testcase_27 | AC | 296 ms
5,376 KB |
ソースコード
#include <bits/stdc++.h> #include <x86intrin.h> using namespace std; // T must support +=, -=, *, *=, ==, and != template<class T, size_t N, size_t M> struct matrix_fixed_base{ using ring_t = T; using domain_t = array<T, M>; using range_t = array<T, N>; static constexpr int n = N, m = M; array<array<T, M>, N> data; array<T, M> &operator()(int i){ assert(0 <= i && i < n); return data[i]; } const array<T, M> &operator()(int i) const{ assert(0 <= i && i < n); return data[i]; } T &operator()(int i, int j){ assert(0 <= i && i < n && 0 <= j && j < m); return data[i][j]; } const T &operator()(int i, int j) const{ assert(0 <= i && i < n && 0 <= j && j < m); return data[i][j]; } operator vector<vector<T>>() const{ return data; } bool operator==(const matrix_fixed_base &a) const{ assert(n == a.n && m == a.m); return data == a.data; } bool operator!=(const matrix_fixed_base &a) const{ assert(n == a.n && m == a.m); return data != a.data; } matrix_fixed_base &operator+=(const matrix_fixed_base &a){ assert(n == a.n && m == a.m); for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) data[i][j] += a(i, j); return *this; } matrix_fixed_base operator+(const matrix_fixed_base &a) const{ assert(n == a.n && m == a.m); return matrix_fixed_base(*this) += a; } matrix_fixed_base &operator-=(const matrix_fixed_base &a){ assert(n == a.n && m == a.m); for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) data[i][j] += a(i, j); return *this; } matrix_fixed_base operator-(const matrix_fixed_base &a) const{ assert(n == a.n && m == a.m); return matrix_fixed_base(*this) += a; } template<size_t N2, size_t M2> matrix_fixed_base<T, N, M2> operator*(const matrix_fixed_base<T, N2, M2> &a) const{ assert(m == a.n); int l = M2; matrix_fixed_base<T, N, M2> res; for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) for(auto k = 0; k < l; ++ k) res(i, k) += data[i][j] * a(j, k); return res; } template<size_t N2, size_t M2> matrix_fixed_base &operator*=(const matrix_fixed_base<T, N2, M2> &a){ return *this = *this * a; } matrix_fixed_base &operator*=(T c){ for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) data[i][j] *= c; return *this; } matrix_fixed_base operator*(T c) const{ return matrix_fixed_base(*this) *= c; } template<class U, typename enable_if<is_integral<U>::value>::type* = nullptr> matrix_fixed_base &inplace_power(U e){ assert(n == m && e >= 0); matrix_fixed_base res(1, 0); for(; e; *this *= *this, e >>= 1) if(e & 1) res *= *this; return *this = res; } template<class U> matrix_fixed_base power(U e) const{ return matrix_fixed_base(*this).inplace_power(e); } matrix_fixed_base &inplace_transpose(){ assert(n == m); for(auto i = 0; i < n; ++ i) for(auto j = i + 1; j < n; ++ j) swap(data[i][j], data[j][i]); return *this; } matrix_fixed_base transpose() const{ if(n == m) return matrix_fixed_base(*this).inplace_transpose(); matrix_fixed_base<T, M, N> res; for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) res(j, i) = data[i][j]; return res; } // Multiply a column vector v on the right range_t operator*(const domain_t &v) const{ range_t res; res.fill(T(0)); for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) res[i] += data[i][j] * v[j]; return res; } // Assumes T is either a floating, integral, or a modular type. // If T is a floating type, O(n) divisions with O(n^3) additions, subtractions, and multiplications. // Otherwise, O(n^2 * log(size)) divisions with O(n^3) additions, subtractions, and multiplications. // Returns {REF matrix, determinant, rank} tuple<matrix_fixed_base &, T, int> inplace_REF(){ if(n == 0) return {*this, T(1), 0}; T det = 1; int rank = 0; for(auto j = 0; j < m; ++ j){ if constexpr(is_floating_point_v<T>){ static const T eps = 1e-9; int pivot = rank; for(auto i = rank + 1; i < n; ++ i) if(abs(data[pivot][j]) < abs(data[i][j])) pivot = i; if(rank != pivot){ swap(data[rank], data[pivot]); det *= -1; } if(abs(data[rank][j]) <= eps) continue; det *= data[rank][j]; T inv = 1 / data[rank][j]; for(auto i = rank + 1; i < n; ++ i) if(abs(data[i][j]) > eps){ T coef = data[i][j] * inv; for(auto k = j; k < m; ++ k) data[i][k] -= coef * data[rank][k]; } ++ rank; } else{ for(auto i = rank + 1; i < n; ++ i) while(data[i][j]){ T q; if constexpr(is_integral_v<T> || is_same_v<T, __int128_t> || is_same_v<T, __uint128_t>) q = data[rank][j] / data[i][j]; else q = data[rank][j].data / data[i][j].data; if(q) for(auto k = j; k < m; ++ k) data[rank][k] -= q * data[i][k]; swap(data[rank], data[i]); det *= -1; } if(rank == j) det *= data[rank][j]; else det = T(0); if(data[rank][j]) ++ rank; } if(rank == n) break; } return {*this, det, rank}; } // Assumes T is either a floating, integral, or a modular type. // If T is a floating type, O(n) divisions with O(n^3) additions, subtractions, and multiplications. // Otherwise, O(n^2 * log(size)) divisions with O(n^3) additions, subtractions, and multiplications. // Returns {REF matrix, determinant, rank} tuple<matrix_fixed_base, T, int> REF() const{ return matrix_fixed_base(*this).inplace_REF(); } // Assumes T is a field. // O(n) divisions with O(n^3) additions, subtractions, and multiplications. // Returns {REF matrix, determinant, rank} tuple<matrix_fixed_base &, T, int> inplace_REF_field(){ if(n == 0) return {*this, T(1), 0}; T det = T(1); int rank = 0; for(auto j = 0; j < m; ++ j){ int pivot = -1; for(auto i = rank; i < n; ++ i) if(data[i][j] != T(0)){ pivot = i; break; } if(!~pivot){ det = T(0); continue; } if(rank != pivot){ swap(data[rank], data[pivot]); det *= -1; } det *= data[rank][j]; T inv = 1 / data[rank][j]; for(auto i = rank + 1; i < n; ++ i) if(data[i][j] != T(0)){ T coef = data[i][j] * inv; for(auto k = j; k < m; ++ k) data[i][k] -= coef * data[j][k]; } ++ rank; if(rank == n) break; } return {*this, det, rank}; } // Assumes T is a field. // O(n) divisions with O(n^3) additions, subtractions, and multiplications. // Returns {REF matrix, determinant, rank} tuple<matrix_fixed_base, T, int> REF_field() const{ return matrix_fixed_base(*this).inplace_REF_field(); } // Assumes T is a field. // O(n) divisions with O(n^3) additions, subtractions, and multiplications. optional<matrix_fixed_base> inverse() const{ assert(n == m); if(n == 0) return *this; auto a = data; array<array<T, N>, N> res{}; for(auto i = 0; i < n; ++ i) res[i][i] = T(1); for(auto j = 0; j < n; ++ j){ int rank = j, pivot = -1; if(is_floating_point_v<T>){ static const T eps = 1e-9; pivot = rank; for(auto i = rank + 1; i < n; ++ i) if(abs(a[pivot][j]) < abs(a[i][j])) pivot = i; if(abs(a[pivot][j]) <= eps) return {}; } else{ for(auto i = rank; i < n; ++ i) if(a[i][j] != T(0)){ pivot = i; break; } if(!~pivot) return {}; } swap(a[rank], a[pivot]), swap(res[rank], res[pivot]); T inv = 1 / a[rank][j]; for(auto k = 0; k < n; ++ k) a[rank][k] *= inv, res[rank][k] *= inv; for(auto i = 0; i < n; ++ i){ if constexpr(is_floating_point_v<T>){ static const T eps = 1e-9; if(i != rank && abs(a[i][j]) > eps){ T d = a[i][j]; for(auto k = 0; k < n; ++ k) a[i][k] -= d * a[rank][k], res[i][k] -= d * res[rank][k]; } } else if(i != rank && a[i][j] != T(0)){ T d = a[i][j]; for(auto k = 0; k < n; ++ k) a[i][k] -= d * a[rank][k], res[i][k] -= d * res[rank][k]; } } } return res; } // Assumes T is either a floating, integral, or a modular type. // If T is a floating type, O(n) divisions with O(n^3) additions, subtractions, and multiplications. // Otherwise, O(n^2 * log(size)) divisions with O(n^3) additions, subtractions, and multiplications. T determinant() const{ assert(n == m); return get<1>(REF()); } // Assumes T is a field. // O(n) divisions with O(n^3) additions, subtractions, and multiplications. T determinant_field() const{ assert(n == m); return get<1>(REF_field()); } // Assumes T is either a floating, integral, or a modular type. // If T is a floating type, O(n) divisions with O(n^3) additions, subtractions, and multiplications. // Otherwise, O(n^2 * log(size)) divisions with O(n^3) additions, subtractions, and multiplications. int rank() const{ return get<2>(REF()); } // Assumes T is a field. // O(n) divisions with O(n^3) additions, subtractions, and multiplications. int rank_field() const{ return get<2>(REF_field()); } // O(n * 2^n) T permanent() const{ static_assert(n <= 30 && n == m); T perm = n ? 0 : 1; array<T, N> sum{}; for(auto order = 1; order < 1 << n; ++ order){ int j = __lg(order ^ order >> 1 ^ order - 1 ^ order - 1 >> 1), sign = (order ^ order >> 1) & 1 << j ? 1 : -1; T prod = order & 1 ? -1 : 1; if((order ^ order >> 1) & 1 << j) for(auto i = 0; i < n; ++ i) prod *= sum[i] += data[i][j]; else for(auto i = 0; i < n; ++ i) prod *= sum[i] -= data[i][j]; perm += prod; } return perm * (n & 1 ? -1 : 1); } template<class output_stream> friend output_stream &operator<<(output_stream &out, const matrix_fixed_base &a){ out << "{"; for(auto i = 0; i < a.n; ++ i){ out << "{"; for(auto j = 0; j < a.m; ++ j){ out << a(i, j); if(j != a.m - 1) out << ", "; } out << "}"; if(i != a.n - 1) out << ", "; } return out << "}"; } matrix_fixed_base(): matrix_fixed_base(T(0), T(0)){ } matrix_fixed_base(const T &init_diagonal, const T &init_off_diagonal){ for(auto i = 0; i < n; ++ i) for(auto j = 0; j < m; ++ j) data[i][j] = i == j ? init_diagonal : init_off_diagonal; } matrix_fixed_base(const array<array<T, M>, N> &arr): data(arr){ } static matrix_fixed_base additive_identity(){ return matrix_fixed_base(T(0), T(0)); } static matrix_fixed_base multiplicative_identity(){ return matrix_fixed_base(T(1), T(0)); } }; template<class T, size_t N, size_t M> matrix_fixed_base<T, N, M> operator*(T c, matrix_fixed_base<T, N, M> a){ for(auto i = 0; i < a.n; ++ i) for(auto j = 0; j < a.m; ++ j) a(i, j) = c * a(i, j); return a; } // Multiply a row vector v on the left template<class T, size_t N, size_t M> typename matrix_fixed_base<T, N, M>::domain_t operator*(const typename matrix_fixed_base<T, N, M>::range_t &v, const matrix_fixed_base<T, N, M> &a){ typename matrix_fixed_base<T, N, M>::domain_t res; res.fill(T(0)); for(auto i = 0; i < a.n; ++ i) for(auto j = 0; j < a.m; ++ j) res[j] += v[i] * a(i, j); return res; } template<class T> using matrix = matrix_fixed_base<T, 2, 2>; int main(){ cin.tie(0)->sync_with_stdio(0); cin.exceptions(ios::badbit | ios::failbit); cout << fixed << setprecision(5); auto __solve_tc = [&](auto __tc_num)->int{ array<array<double, 2>, 2> a; for(auto i = 0; i < 2; ++ i){ for(auto j = 0; j < 2; ++ j){ cin >> a[i][j]; } } double det = max(0.0, (a[0][0] - a[1][1]) * (a[0][0] - a[1][1]) + 4 * a[0][1] * a[1][0]); if(det <= 1e-11){ for(auto i = 0; i < 2; ++ i){ for(auto j = 0; j < 2; ++ j){ cout << cbrt(a[i][j]) << " \n"[j]; } } } else{ double alpha = ((a[0][0] + a[1][1]) + sqrt(det)) / 2; double beta = ((a[0][0] + a[1][1]) - sqrt(det)) / 2; matrix<double> basis, mat; double coef = 0.31231; basis(0, 0) = a[0][1] + coef * (a[1][1] - alpha); basis(0, 1) = a[0][1] + coef * (a[1][1] - beta); basis(1, 0) = alpha - a[0][0] - coef * a[1][0], basis(1, 1) = beta - a[0][0] - coef * a[1][0]; mat(0, 0) = cbrt(alpha), mat(1, 1) = cbrt(beta); mat = basis * mat * *basis.inverse(); for(auto i = 0; i < 2; ++ i){ for(auto j = 0; j < 2; ++ j){ cout << mat(i, j) << " \n"[j]; } } } return 0; }; int __tc_cnt; cin >> __tc_cnt; for(auto __tc_num = 0; __tc_num < __tc_cnt; ++ __tc_num){ __solve_tc(__tc_num); } return 0; } /* */ //////////////////////////////////////////////////////////////////////////////////////// // // // Coded by Aeren // // // ////////////////////////////////////////////////////////////////////////////////////////