結果

問題 No.1595 The Final Digit
ユーザー simkarensimkaren
提出日時 2021-11-15 11:13:57
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,417 bytes
コンパイル時間 2,608 ms
コンパイル使用メモリ 195,952 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 01:45:06
合計ジャッジ時間 4,011 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

template<int mod>
struct Matrix {
    int n;
    vector<vector<int>> element;

    Matrix(int n) : n(n) {
        element.resize(n);
        for (int i = 0; i < n; ++i)
            element[i].resize(n, 0);
    }

    Matrix operator *(Matrix a) {
        assert(n == a.n);
        Matrix res(n);
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                int tmp = 0;
                for (int k = 0; k < n; ++k) {
                    tmp += element[i][k] * a.element[k][j] % mod;
                    tmp %= mod;
                }
                res.element[i][j] = tmp;
            }
        }
        return res;
    }

    Matrix pow(long long k) {
        if (k == 0) {
            Matrix res(n);
            for (int i = 0; i < n; ++i)
                res.element[i][i] = 1;
            return res;
        }
        Matrix tmp = pow(k / 2);
        if (k % 2 == 1)
            return tmp * tmp * (*this);
        return tmp * tmp;
    }
};

int main(void){
    int p, q, r;
    long long K;
    cin >> p >> q >> r >> K;
    p %= 10; q %= 10; r %= 10;
    Matrix<10> m(3);
    m.element = {{1, 1, 1}, {1, 0, 0}, {0, 1, 0}};
    m = m.pow(K - 3);
    int ans = m.element[0][0] * r + m.element[0][1] * q + m.element[0][2] * p;
    ans %= 10;
    cout << ans << endl;
    return 0;
}
0