結果

問題 No.444 旨味の相乗効果
ユーザー ei1333333ei1333333
提出日時 2016-11-23 00:11:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 232 ms / 2,500 ms
コード長 3,000 bytes
コンパイル時間 1,487 ms
コンパイル使用メモリ 159,372 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-07 02:18:25
合計ジャッジ時間 3,672 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 232 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 10 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 118 ms
4,376 KB
testcase_13 AC 59 ms
4,504 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 231 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 24 ms
4,376 KB
testcase_20 AC 182 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

typedef long long int64;
const int mod = 1e9 + 7;

inline int64 modPow(int64 x, int64 n)
{
  if(n == 0) return (1);
  int64 ret = modPow(x, n / 2);
  (ret *= ret) %= mod;
  if(n & 1) (ret *= x) %= mod;
  return (ret);
}

struct Matrix
{
  vector< vector< int > > A;

  Matrix() {}

  Matrix(size_t n, size_t m) : A(n, vector< int >(m, 0)) {}

  Matrix(size_t n) : A(n, vector< int >(n, 0)) {};

  size_t height() const
  {
    return (A.size());
  }

  size_t width() const
  {
    return (A[0].size());
  }

  inline const vector< int > &operator[](int k) const
  {
    return (A.at(k));
  }

  inline vector< int > &operator[](int k)
  {
    return (A.at(k));
  }

  static Matrix I(size_t n)
  {
    Matrix mat(n);
    for(int i = 0; i < n; i++) mat[i][i] = 1;
    return (mat);
  }

  Matrix &operator+=(const Matrix &B)
  {
    size_t n = height(), m = width();
    assert(n == B.height() && m == B.width());
    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        ((*this)[i][j] += B[i][j]) %= mod;
    return (*this);
  }

  Matrix &operator-=(const Matrix &B)
  {
    size_t n = height(), m = width();
    assert(n == B.height() && m == B.width());
    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        ((*this)[i][j] += (mod - B[i][j])) %= mod;
    return (*this);
  }

  Matrix &operator*=(const Matrix &B)
  {
    size_t n = height(), m = B.width(), p = width();
    assert(p == B.height());
    vector< vector< int > > C(n, vector< int >(m, 0));
    for(int i = 0; i < n; i++)
      for(int j = 0; j < m; j++)
        for(int k = 0; k < p; k++)
          C[i][j] = (C[i][j] + 1LL * (*this)[i][k] * B[k][j]) % mod;
    A.swap(C);
    return (*this);
  }

  Matrix &operator^=(long long k)
  {
    Matrix B = Matrix::I(height());
    while(k > 0) {
      if(k & 1) B *= *this;
      *this *= *this;
      k >>= 1LL;
    }
    A.swap(B.A);
    return (*this);
  }

  Matrix operator+(const Matrix &B) const
  {
    return (Matrix(*this) += B);
  }

  Matrix operator-(const Matrix &B) const
  {
    return (Matrix(*this) -= B);
  }

  Matrix operator*(const Matrix &B) const
  {
    return (Matrix(*this) *= B);
  }

  Matrix operator^(const long long k) const
  {
    return (Matrix(*this) ^= k);
  }

  friend ostream &operator<<(ostream &os, Matrix &p)
  {
    size_t n = p.height(), m = p.width();
    for(int i = 0; i < n; i++) {
      os << "[";
      for(int j = 0; j < m; j++) {
        os << p[i][j] << (j + 1 == m ? "]\n" : ",");
      }
    }
    return (os);
  }
};

int main()
{
  int64 n, c;
  cin >> n >> c;
  int64 ret = 0;
  Matrix v(n, 1);
  for(int i = 0; i < n; i++) {
    int a;
    cin >> a;
    v[i][0] = a;
    (ret += mod - modPow(v[i][0], c)) %= mod;
  }
  Matrix m(n);
  for(int i = 0; i < n; i++) {
    for(int j = 0; j <= i; j++) {
      m[i][j] = v[i][0];
    }
  }
  v = (m ^ (c - 1)) * v;
  for(int i = 0; i < n; i++) {
    (ret += v[i][0]) %= mod;
  }
  cout << ret << endl;
}
0