結果

問題 No.1595 The Final Digit
ユーザー sawfishsawfish
提出日時 2022-10-23 22:59:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,145 bytes
コンパイル時間 495 ms
コンパイル使用メモリ 64,880 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-15 07:12:28
合計ジャッジ時間 1,672 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>

using namespace std;
using LL = long long;
using ULL = unsigned long long;

constexpr int MOD = 10;

struct M {
    LL p[3][3] = {
            {1, 1, 1},
            {1, 0, 0},
            {0, 1, 0},
    };

    M mul(M b, LL m) {
        M c;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                c.p[i][j] = 0;
                for (int k = 0; k < 3; k++) {
                    c.p[i][j] += (this->p[i][k] * b.p[k][j]) % m;
                }
            }
        }
        return c;
    }
};

struct E : M {
    LL p[3][3] = {
            {1, 0, 0},
            {0, 1, 0},
            {0, 0, 1},
    };
};

M pow(M, LL, LL);

int main() {
    LL p, q, r, K;
    cin >> p >> q >> r >> K;

    M A, B = pow(A, K - 3, MOD);

    LL ans = 0;
    ans = (ans + B.p[0][0] * r) % MOD;
    ans = (ans + B.p[0][1] * q) % MOD;
    ans = (ans + B.p[0][2] * p) % MOD;

    cout << ans << endl;
}

M pow(M a, LL b, LL m) {
    if (b == 0) return E();
    if (b == 1) return M();
    M ret = pow(a, b / 2, m);
    ret = ret.mul(ret, m);
    if (b % 2 == 1) ret = ret.mul(a, m);
    return ret;
}
0