結果

問題 No.1595 The Final Digit
ユーザー h2929h2929
提出日時 2021-07-09 21:47:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,475 bytes
コンパイル時間 4,520 ms
コンパイル使用メモリ 233,024 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 08:26:31
合計ジャッジ時間 4,753 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
#define ll long long int
#define INF 1000000000000000000
using namespace atcoder;
using namespace std;
template<typename T>
struct matrix {
  public:
    matrix() {}
    matrix(int n, int m) : mat(n, std::vector<T>(m, 0)) {}
    matrix(int n) : mat(n, std::vector<T>(n, 0)) {}

  int height() const {
    return mat.size();
  }
  int width() const {
    return mat[0].size();
  }
  inline const std::vector<T> &operator[](int k) const {
    return (mat.at(k));
  }

  inline std::vector<T> &operator[](int k) {
    return (mat.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) {
    int 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];
    return (*this);
  }
  matrix &operator-=(const matrix b) {
    int 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];
    return (*this);
  }
  matrix &operator*=(const matrix b) {
    int n = height(), m = width(), p = b.width();
    assert(m == b.height());
    std::vector< std::vector< T > > c(n, std::vector< T >(p, 0));
    for(int i = 0; i < n; i++)
      for(int j = 0; j < p; j++)
        for(int k = 0; k < m; k++)
          c[i][j] = (c[i][j] + (*this)[i][k] * b[k][j]);

    mat.swap(c);
    return (*this);
  }

 
  matrix &operator^=(long long int k) {
    matrix b = matrix::I(height());
    while(k > 0) {
      if(k & 1) b *= *this;
      *this *= *this;
      k >>= 1LL;
    }
    mat.swap(b.mat);
    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 int k) const {
    return (matrix(*this) ^= k);
  }
  friend std::ostream &operator<<(std::ostream &os, matrix p) {
    int n = p.height();
    int 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);
  }
  T determinant() {
    matrix b(*this);
    assert(width() == height());
    T ret = 1;
    for(int i = 0; i < width(); i++) {
      int idx = -1;
      for(int j = i; j < width(); j++) {
        if(b[j][i] != 0) idx = j;
      }
      if(idx == -1) return (0);
      if(i != idx) {
        ret *= -1;
        swap(b[i], b[idx]);
      }
      ret *= b[i][i];
      T vv = b[i][i];
      for(int j = 0; j < width(); j++) {
        b[i][j] /= vv;
      }
      for(int j = i + 1; j < width(); j++) {
        T a = b[j][i];
        for(int k = 0; k < width(); k++) {
          b[j][k] -= b[i][k] * a;
        }
      }
    }
    return (ret);
  }
    
  private:
    std::vector<std::vector<T>> mat;
};

using mint = modint;

int main(void){
  mint::set_mod(10);
  ll p, q, r, k;
  cin >> p >> q >> r >> k;

  matrix<mint> mat(3);
  mat[0][0] = 1;
  mat[0][1] = 1;
  mat[0][2] = 1;
  mat[1][0] = 1;
  mat[2][1] = 1;
  mat ^= (k-3);
  matrix<mint> v(3, 1);
  v[0][0] = r; 
  v[1][0] = q; 
  v[2][0] = p; 
  mat *= v;
  cout << mat[0][0].val() << endl;
  

  return 0;
}
0