結果

問題 No.1595 The Final Digit
ユーザー qqq xxaqqq xxa
提出日時 2021-07-18 17:49:49
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,558 bytes
コンパイル時間 1,675 ms
コンパイル使用メモリ 174,592 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 20:34:31
合計ジャッジ時間 2,668 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 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 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 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>
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;
}
0