結果

問題 No.1105 Many Triplets
ユーザー tonyu0tonyu0
提出日時 2020-08-02 20:52:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,786 bytes
コンパイル時間 997 ms
コンパイル使用メモリ 109,508 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-26 13:12:34
合計ジャッジ時間 2,937 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 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,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
constexpr ll MOD = 1000000007;
template <typename T>
class Matrix {
public:
  vector<vector<T>> mat;
  Matrix(int h, int w) : mat(h, vector<T>(w)) {}
  vector<T>& operator[](int i) { return mat[i]; }
  const vector<T>& operator[](int i) const { return mat[i]; }
  const size_t height() const { return mat.size(); }
  const size_t width() const { return mat[0].size(); }

  // only square matrix
  static Matrix identity(int n) {
    Matrix E(n, n);
    for (int i = 0; i < n; ++i) E[i][i] = 1;
    return E;
  }

  // O(N^3)
  Matrix& operator*=(const Matrix& m) {
    int h = m.height(), w = m.width();
    vector<vector<T>> res(height(), vector<T>(w));
    for (int i = 0; i < height(); ++i)
      for (int k = 0; k < h; ++k)
        for (int j = 0; j < w; ++j)
          (res[i][j] += (*this)[i][k] * m[k][j] % MOD) %= MOD;
    mat.swap(res);
    return *this;
  }

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

  // O(N^3logK)
  Matrix operator^(T exponent) {
    Matrix res = Matrix::identity(height());
    Matrix product = *this;

    while (exponent) {
      if (exponent & 1) res *= product;
      product *= product;
      exponent >>= 1;
    }
    return res;
  }
};

int main() {
  ll n, a, b, c;
  cin >> n >> a >> b >> c;
  Matrix<ll> mat(3, 3);
  Matrix<ll> abc(3, 1);
  mat[0] = {1, MOD - 1, 0};
  mat[1] = {0, 1, MOD - 1};
  mat[2] = {MOD - 1, 0, 1};
  abc[0] = {a};
  abc[1] = {b};
  abc[2] = {c};
  mat = mat ^ (n - 1);
  mat *= abc;

  cout << mat[0][0] << " " << mat[1][0] << " " << mat[2][0] << endl;
  return 0;
}
0