結果

問題 No.1595 The Final Digit
ユーザー kobakoba_issakobakoba_issa
提出日時 2021-07-09 22:38:03
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,753 bytes
コンパイル時間 3,534 ms
コンパイル使用メモリ 135,064 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 09:56:55
合計ジャッジ時間 4,586 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

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

typedef long long ll;

const int INF = (1<<30)-1;
const ll LINF = (1LL<<60)-1;

#define rep(i, n) for (int i = 0; i < n; i++)
#define sz(a) (int)(a.size())

template<class T> 
bool chmax(T &a, T b) {if (a < b) {a = b;return true;}else return false;}

template<class T> 
bool chmin(T &a, T b) {if (a > b) {a = b;return true;}else return false;}

vector<vector<ll> > mod_prod_matrix(const vector<vector<ll> >& A, const vector<vector<ll> >& B, ll mod) {
    int l = A.size(), m = B.size(), n = B[0].size();
    vector<vector<ll> > res(l, vector<ll>(n, 0));
    for (int i = 0; i < l; i++) {
        for (int j = 0; j < n; j++) {
            for (int k = 0; k < m; k++)
                res[i][j] = (res[i][j] + A[i][k]*B[k][j]%mod)%mod;
        }
    }
    return res;
}

vector<vector<ll> > mod_matrix_pow(const vector<vector<ll> >& A, ll n, ll mod) {
    int m = A.size();
    vector<vector<ll> > res(m, vector<ll>(m, 0));
    for (int i = 0; i < m; i++) res[i][i] = 1;
    if (n == 0)
        return res;
    else if (n % 2) {
        res = mod_matrix_pow(A, n-1, mod);
        return mod_prod_matrix(A, res, mod);
    }
    else {
        res = mod_matrix_pow(A, n/2, mod);
        return mod_prod_matrix(res, res, mod);
    }
}

//コーナーケースに気をつけろ!
int main() {
    vector<ll> p(3);  ll k;
    rep(i, 3)   cin >> p[i];
    cin >> k;
    vector<vector<ll> > B(3, vector<ll>(3, 0));
    rep(i, 3)   B[0][i] = 1;
    B[1][0] = 1;
    B[2][1] = 1;
    
    B = mod_matrix_pow(B, k-3, 10LL);
    int ans = 0;
    rep(i, 3)   ans = (ans + p[2-i] * B[0][i] % 10) % 10;
    cout << ans << endl;
    return 0;   
}

//小数点精度
//cout << fixed << std::setprecision(15) << y << endl;
0