結果

問題 No.731 等差数列がだいすき
ユーザー 👑 emthrmemthrm
提出日時 2020-12-06 18:07:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,759 bytes
コンパイル時間 2,333 ms
コンパイル使用メモリ 209,452 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-17 13:08:50
合計ジャッジ時間 2,958 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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] += 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 += a[i] * a[i];
  cout << b1 << ' ' << d << '\n' << c << '\n';
  return 0;
}
0