結果
問題 | No.1595 The Final Digit |
ユーザー |
|
提出日時 | 2021-07-18 17:49:49 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,558 bytes |
コンパイル時間 | 1,887 ms |
コンパイル使用メモリ | 175,816 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-08 11:13:29 |
合計ジャッジ時間 | 2,299 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 17 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; ll solve() { ll P, Q, R, K; cin >> P >> Q >> R >> K; using Int = ll; auto matmul = [&](vector<vector<Int>>& t, vector<vector<Int>>& m) -> vector<vector<Int> > { vector<vector<Int>> ret(t.size(), vector<Int>(m[0].size())); for ( int r = 0; r < t.size(); r++ ) { for ( int c = 0; c < m[0].size(); c++ ) { for ( int k = 0; k < m.size(); k++ ) { ret[r][c] += t[r][k] * m[k][c]; } ret[r][c] %= 10; } } return ret; }; auto matpow = [&](vector<vector<Int>>& tt, ll n) -> vector<vector<Int>> { ll d = tt.size(); vector<vector<Int>> ret(d, vector<Int>(d)); vector<vector<Int>> t(d, vector<Int>(d)); for ( int i = 0; i < d; i++ ) { ret[i][i] = 1; } for ( int i = 0; i < d; i++ ) { for ( int j = 0; j < d; j++ ) { t[i][j] = tt[i][j]; } } while ( n > 0 ) { if ( n & 1 ) ret = matmul(ret,t); t = matmul(t,t); n >>= 1; } return ret; }; vector<vector<ll>> t(3, vector<ll>(3)); t[0][1] = 1; t[1][2] = 1; t[2][0] = 1; t[2][1] = 1; t[2][2] = 1; auto tt = matpow(t, K-1); vector<vector<ll>> v = {{P%10}, {Q%10}, {R%10}}; auto ans = matmul(tt, v); return ans[0][0]; } int main() { auto ans = solve(); cout << ans << "\n"; return 0; }