結果
問題 | No.731 等差数列がだいすき |
ユーザー | 👑 emthrm |
提出日時 | 2020-12-06 18:10:25 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 2 ms / 1,500 ms |
コード長 | 4,771 bytes |
コンパイル時間 | 1,952 ms |
コンパイル使用メモリ | 211,212 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-09-17 13:09:02 |
合計ジャッジ時間 | 2,603 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,944 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 2 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 2 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,940 KB |
testcase_10 | AC | 2 ms
6,944 KB |
testcase_11 | AC | 1 ms
6,940 KB |
testcase_12 | AC | 1 ms
6,940 KB |
testcase_13 | AC | 2 ms
6,940 KB |
testcase_14 | AC | 2 ms
6,944 KB |
testcase_15 | AC | 2 ms
6,944 KB |
testcase_16 | AC | 2 ms
6,940 KB |
testcase_17 | AC | 2 ms
6,944 KB |
testcase_18 | AC | 2 ms
6,940 KB |
testcase_19 | AC | 2 ms
6,948 KB |
testcase_20 | AC | 2 ms
6,940 KB |
ソースコード
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; template <typename T> struct Matrix { Matrix(int m, int n, T val = 0) : dat(m, std::vector<T>(n, val)) {} int height() const { return dat.size(); } int width() const { return dat.front().size(); } Matrix pow(long long exponent) const { int n = height(); Matrix<T> tmp = *this, res(n, n, 0); for (int i = 0; i < n; ++i) res[i][i] = 1; while (exponent > 0) { if (exponent & 1) res *= tmp; tmp *= tmp; exponent >>= 1; } return res; } inline const std::vector<T> &operator[](const int idx) const { return dat[idx]; } inline std::vector<T> &operator[](const int idx) { return dat[idx]; } Matrix &operator=(const Matrix &x) { int m = x.height(), n = x.width(); dat.resize(m, std::vector<T>(n)); for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) dat[i][j] = x[i][j]; return *this; } Matrix &operator+=(const Matrix &x) { int m = height(), n = width(); for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) dat[i][j] += x[i][j]; return *this; } Matrix &operator-=(const Matrix &x) { int m = height(), n = width(); for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) dat[i][j] -= x[i][j]; return *this; } Matrix &operator*=(const Matrix &x) { int m = height(), n = x.width(), l = width(); std::vector<std::vector<T>> res(m, std::vector<T>(n, 0)); for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) { for (int k = 0; k < l; ++k) res[i][j] += dat[i][k] * x[k][j]; } std::swap(dat, res); return *this; } Matrix operator+(const Matrix &x) const { return Matrix(*this) += x; } Matrix operator-(const Matrix &x) const { return Matrix(*this) -= x; } Matrix operator*(const Matrix &x) const { return Matrix(*this) *= x; } private: std::vector<std::vector<T>> dat; }; template <typename T> int gauss_jordan(Matrix<T> &mat, const T EPS = 1e-8, bool is_extended = false) { int m = mat.height(), n = mat.width(), rank = 0; for (int col = 0; col < n; ++col) { if (is_extended && col == n - 1) break; int pivot = -1; T mx = EPS; for (int row = rank; row < m; ++row) { if (std::abs(mat[row][col]) > mx) { pivot = row; mx = std::abs(mat[row][col]); } } if (pivot == -1) continue; std::swap(mat[rank], mat[pivot]); T tmp = mat[rank][col]; for (int col2 = 0; col2 < n; ++col2) mat[rank][col2] /= tmp; for (int row = 0; row < m; ++row) { if (row != rank && std::abs(mat[row][col]) > EPS) { tmp = mat[row][col]; for (int col2 = 0; col2 < n; ++col2) mat[row][col2] -= mat[rank][col2] * tmp; } } ++rank; } return rank; } template <typename T, typename U = double> std::vector<U> linear_equation(const Matrix<T> &a, const std::vector<T> &b, const U EPS = 1e-8) { int m = a.height(), n = a.width(); Matrix<U> matrix(m, n + 1); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) matrix[i][j] = a[i][j]; matrix[i][n] = b[i]; } int rank = gauss_jordan(matrix, EPS, true); std::vector<U> res; for (int row = rank; row < m; ++row) { if (std::abs(matrix[row][n]) > EPS) return res; } res.assign(n, 0); for (int i = 0; i < rank; ++i) res[i] = matrix[i][n]; return res; } int main() { int n; cin >> n; vector<int> a(n); REP(i, n) cin >> a[i]; Matrix<double> m(2, 2); vector<double> b(2, 0); m[0][0] = n * 2; m[0][1] = (n - 1) * n; m[1][0] = (n - 1) * n; m[1][1] = (n - 1) * n * (n * 2 - 1) / 3; b[0] = accumulate(ALL(a), 0) * 2; REP(i, n) b[1] += 1LL * a[i] * i; b[1] *= 2; vector<double> b1d = linear_equation(m, b); double b1 = b1d[0], d = b1d[1]; double c = n * b1 * b1 + (n - 1) * n * b1 * d - b1 * b[0] - d * b[1] + (n - 1) * n * (n * 2 - 1) / 6 * d * d; REP(i, n) c += 1LL * a[i] * a[i]; cout << b1 << ' ' << d << '\n' << c << '\n'; return 0; }