結果

問題 No.1595 The Final Digit
ユーザー JupytorJupytor
提出日時 2021-07-09 22:33:46
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,596 bytes
コンパイル時間 1,036 ms
コンパイル使用メモリ 101,724 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 09:51:29
合計ジャッジ時間 1,870 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <tuple>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#define flush fflush(stdout)
#define endl '\n'
#define all(v) v.begin(), v.end()
#define pf(dans) printf("%.12lf", dans)
#define pfn(dans) pf(dans); cout << endl;
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;
const int mod1 = (int)1e9 + 7, mod2 = (int)998244353;
const int INF = (int)1e9 + 10;
const ll LINF = (ll)1e18 + 10;
const int di[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dj[8] = {0, 1, 0, -1, -1, 1, -1, 1};

#define rep0(i, n) for (i = 0; i < n; i++)
#define rep1(i, a, b) for (i = a; i < b; i++)
#define reprev(i, n) for (i = n - 1; i >= 0; i--)

template <typename T>
T my_abs(T x){
    return (x >= 0)? x : -x;
}

template <typename T>
void chmax(T &a, T b){
    a = max(a, b);
}

template <typename T>
void chmin(T &a, T b){
    a = min(a, b);
}

ll gcd(ll a, ll b){
    if (a > b) return gcd(b, a);
    if (a == 0) return b;
    return gcd(b % a, a);
}

bool incld_bit(ll bit, int i){
    return ((bit>>i) & 1) == 1;
}

// --------------------------------------------------------------------------------

typedef vector<vector<ll>> mat;

int wa(ll a, ll b){
    return (a + b) % 10;
}

int seki(ll a, ll b){
    return a * b % 10;
}

mat unit_mat(int n){
    int i;
    mat res(n, vector<ll>(n, 0));
    rep0(i, n){
        res[i][i] = 1;
    }
    return res;
}

void multiply_matrix(int n, mat &sbj, mat oprnd){
    int i, j, k;
    mat res(n, vector<ll>(n, 0));
    rep0(i, n){
        rep0(j, n){
            res[i][j] = 0;
            rep0(k, n){
                res[i][j] = wa(res[i][j], seki(sbj[i][k], oprnd[k][j]));
            }
        }
    }
    sbj = res;
}

mat mat_doubling(int n, mat am, ll k){
    mat dm(n, vector<ll>(n, 0));
    mat res(n, vector<ll>(n, 0));
    res = unit_mat(n);
    dm = am;
    while (k > 0){
        if (k % 2 == 1){
            multiply_matrix(n, res, dm);
        }
        multiply_matrix(n, dm, dm);
        k >>= 1;
    }
    return res;
}

int main(void){
    int i, j;
    int p[3];
    ll k;

    cin >> p[0] >> p[1] >> p[2] >> k;

    mat vm(3, vector<ll>(3, 0));
    vm[0][0] = vm[0][1] = vm[0][2] = 1;
    vm[1][0] = 1;
    vm[2][1] = 1;

    auto res = mat_doubling(3, vm, k - 3);
    int ans;
    ans = 0;
    rep0(j, 3){
        ans += res[0][j] * p[2 - j] % 10;
    }

    cout << ans % 10 << endl;

    return 0;
}
0