結果

問題 No.1595 The Final Digit
ユーザー aradarad
提出日時 2023-06-03 13:15:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,688 bytes
コンパイル時間 4,402 ms
コンパイル使用メモリ 265,236 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 07:48:11
合計ジャッジ時間 5,646 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using VS = vector<string>;
using P = pair<ll,ll>;
using VP = vector<P>;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define out(x) cout << x << endl
#define dout(x) cout << fixed << setprecision(10) << x << endl
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define sz(x) (int)(x.size())
#define re0 return 0
#define pcnt __builtin_popcountll
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr int inf = 1e9;
constexpr ll INF = 1e18;
//using mint = modint1000000007;
//using mint = modint998244353;
int di[4] = {1,0,-1,0};
int dj[4] = {0,1,0,-1};

using mint = modint;

// https://youtu.be/ylWYSurx10A?t=2352
template<typename T>
struct Matrix {
  int h, w;
  vector<vector<T>> d;
  Matrix() {}
  Matrix(int h, int w, T val=0): h(h), w(w), d(h, vector<T>(w,val)) {}
  Matrix& unit() {
    assert(h == w);
    rep(i,h) d[i][i] = 1;
    return *this;
  }
  const vector<T>& operator[](int i) const { return d[i];}
  vector<T>& operator[](int i) { return d[i];}
  Matrix operator*(const Matrix& a) const {
    assert(w == a.h);
    Matrix r(h, a.w);
    rep(i,h)rep(k,w)rep(j,a.w) {
      r[i][j] += d[i][k]*a[k][j];
    }
    return r;
  }
  Matrix pow(long long t) const {
    assert(h == w);
    if (!t) return Matrix(h,h).unit();
    if (t == 1) return *this;
    Matrix r = pow(t>>1);
    r = r*r;
    if (t&1) r = r*(*this);
    return r;
  }
  // https://youtu.be/-j02o6__jgs?t=11273
  /* mint only
  mint det() {
    assert(h == w);
    mint res = 1;
    rep(k,h) {
      for (int i = k; i < h; ++i) {
        if (d[i][k] == 0) continue;
        if (i != k) {
          swap(d[i],d[k]);
          res = -res;
        }
      }
      if (d[k][k] == 0) return 0;
      res *= d[k][k];
      mint inv = mint(1)/d[k][k];
      rep(j,h) d[k][j] *= inv;
      for (int i = k+1; i < h; ++i) {
        mint c = d[i][k];
        for (int j = k; j < h; ++j) d[i][j] -= d[k][j]*c;
      }
    }
    return res;
  }
  //*/
};

int main(){
    ll p,q,r,k;
    cin >> p >> q >> r >> k;
    mint::set_mod(10);
    Matrix<mint> mat(3,3,0);
    mat[0][0] = 1;
    mat[0][1] = 1;
    mat[0][2] = 1;
    mat[1][0] = 1;
    mat[2][1] = 1;
    mat = mat.pow(k-3);
    mint ans = r*mat[0][0];
    ans += q*mat[0][1];
    ans += p*mat[0][2];
    out(ans.val());
}
0