結果

問題 No.1595 The Final Digit
ユーザー bokusunnybokusunny
提出日時 2022-01-20 11:43:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,940 bytes
コンパイル時間 2,493 ms
コンパイル使用メモリ 206,204 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-15 08:39:17
合計ジャッジ時間 4,039 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define bokusunny ios::sync_with_stdio(false), cin.tie(nullptr);

template <class T>
struct Matrix {
 private:
  vector<vector<T>> A;

 public:
  Matrix(const int &r, const int &c) : A(r, vector<T>(c, 0)) {}
  Matrix(const int &n) : A(n, vector<T>(n, 0)) {}

  int row_size() const { return (int)A.size(); }
  int column_size() const { return (int)A[0].size(); }

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

  inline const vector<T> &operator[](int r) const {
    assert(0 <= r && r < row_size());
    return A[r];
  }
  vector<T> &operator[](int r) {
    assert(0 <= r && r < row_size());
    return A[r];
  }
  Matrix &operator+=(const Matrix &B) {
    int ra = row_size(), ca = column_size();
    int rb = B.row_size(), cb = B.column_size();
    assert(ra == rb && ca == cb);

    for (int i = 0; i < ra; i++) {
      for (int j = 0; j < ca; j++) {
        (*this)[i][j] += B[i][j];
      }
    }
    return (*this);
  }
  Matrix &operator-=(const Matrix &B) {
    int ra = row_size(), ca = column_size();
    int rb = B.row_size(), cb = B.column_size();
    assert(ra == rb && ca == cb);

    for (int i = 0; i < ra; i++) {
      for (int j = 0; j < ca; j++) {
        (*this)[i][j] -= B[i][j];
      }
    }
    return (*this);
  }
  Matrix &operator*=(const Matrix &B) {
    int ra = row_size(), ca = column_size();
    int rb = B.row_size(), cb = B.column_size();
    assert(ca == rb);

    vector C(ra, vector<T>(cb, 0));
    for (int i = 0; i < ra; i++) {
      for (int j = 0; j < cb; j++) {
        for (int k = 0; k < ca; k++) {
          C[i][j] += (*this)[i][k] * B[k][j];
          C[i][j] %= 10;
        }
      }
    }
    A.swap(C);
    return (*this);
  }
  Matrix &operator^=(long long k) {
    Matrix B = Matrix::I(row_size());
    while (k > 0) {
      if (k & 1) B *= *this;
      *this *= *this;
      k >>= 1;
    }
    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 &Mat) {
    int r = Mat.row_size(), c = Mat.column_size();
    for (int i = 0; i < r; i++) {
      os << "[";
      for (int j = 0; j < c; j++) {
        os << Mat[i][j] << (j + 1 == c ? "]\n" : ",");
      }
    }
    return (os);
  }
};

void solve() {
  int p, q, r;
  long long K;
  cin >> p >> q >> r >> K;

  Matrix<int> A(3, 1);
  A[0][0] = p % 10;
  A[1][0] = q % 10;
  A[2][0] = r % 10;
  Matrix<int> B(3, 3);
  B[0][1] = B[1][2] = B[2][0] = B[2][1] = B[2][2] = 1;
  K -= 3;
  B ^= K;
  B *= A;

  cout << B[2][0] << endl;
}

int main() {
  bokusunny;
  solve();

  return 0;
}
0