結果

問題 No.1595 The Final Digit
ユーザー 629e^-b629e^-b
提出日時 2021-07-09 21:42:00
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,037 bytes
コンパイル時間 4,401 ms
コンパイル使用メモリ 263,244 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 08:14:09
合計ジャッジ時間 5,147 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
template <typename T>
using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
const ld PI = acos(-1);

template <typename T>
struct Matrix {
    int H, W;
    vector<vector<T>> M;
    Matrix(int H_, int W_, T val = 0) : H(H_), W(W_) {
        M = vector<vector<T>>(H, vector<T>(W, val));
    }
};

template <typename T>
Matrix<T> operator*(Matrix<T> &A, Matrix<T> &B) {
    assert(A.W == B.H);

    Matrix<T> res(A.H, B.W, 0);
    for (int i = 0; i < A.H; i++) {
        for (int k = 0; k < A.W; k++) {
            for (int j = 0; j < B.W; j++) {
                (res.M[i][j] += A.M[i][k] * B.M[k][j]) %= 10;
            }
        }
    }

    return res;
}

template <typename T>
Matrix<T> operator+(Matrix<T> &A, Matrix<T> &B) {
    assert(A.H == B.H);
    assert(A.W == B.W);

    Matrix<T> res(A.H, B.W, 0);
    for (int i = 0; i < A.H; i++) {
        for (int j = 0; j < B.W; j++) {
            res.M[i][j] = (A.M[i][j] + B.M[i][j]) % 10;
        }
    }

    return res;
}

template <typename T>
Matrix<T> operator^(Matrix<T> A, ll n) {
    assert(A.H == A.W);

    Matrix<T> res(A.H, A.W, 0);
    for (int i = 0; i < A.H; ++i) {
        res.M[i][i] = 1;
    }
    while (n) {
        if (n & 1) {
            res = res * A;
        }
        A = A * A;
        n >>= 1;
    }

    return res;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int p, q, r;
    ll K;
    cin >> p >> q >> r >> K;
    p %= 10;
    q %= 10;
    r %= 10;

    K -= 3;
    Matrix<int> A(3, 3);
    A.M[0][0] = 1;
    A.M[0][1] = 1;
    A.M[0][2] = 1;
    A.M[1][0] = 1;
    A.M[2][1] = 1;

    A = A ^ K;

    int ans = (A.M[0][0] * r + A.M[0][1] * q + A.M[0][2] * p) % 10;
    cout << ans << endl;

    return 0;
}
0