結果

問題 No.1105 Many Triplets
ユーザー tonyu0tonyu0
提出日時 2020-08-02 20:52:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,786 bytes
コンパイル時間 1,498 ms
コンパイル使用メモリ 110,160 KB
最終ジャッジ日時 2025-01-12 13:31:34
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

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