結果

問題 No.1595 The Final Digit
ユーザー HaaHaa
提出日時 2021-07-09 21:52:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,414 bytes
コンパイル時間 1,626 ms
コンパイル使用メモリ 172,584 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-14 08:47:48
合計ジャッジ時間 2,724 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
template<typename T> bool chmax(T &x, const T &y) {return (x<y)?(x=y,true):false;};
template<typename T> bool chmin(T &x, const T &y) {return (x>y)?(x=y,true):false;};
constexpr ll MOD=10;
constexpr ll INF=2e18;

VVI matmult(VVI a, VVI b){
    int p=a.size(), q=b[0].size(), r=a[0].size();
    VVI ret(p,VI(q));
    REP(i,p)REP(j,q){
        ll sum=0;
        REP(k,r){
            sum=(sum+a[i][k]*b[k][j]%MOD)%MOD;
        }
        ret[i][j]=sum;
    }
    return ret;
}

VVI matpower(VVI x, ll y){
    int n=x.size();
    VVI ret(n,VI(n,0));
    REP(i,n) ret[i][i]=1;
    while(y){
        if(y&1) ret=matmult(ret,x);
        x=matmult(x,x);
        y>>=1;
    }
    return ret;
}

int main(){
    ll p, q, r, k; cin >> p >> q >> r >> k;
    p%=10;
    q%=10;
    r%=10;
    if(k<=3){
        if(k==1)
            cout << p << endl;
        else if(k==2)
            cout << q << endl;
        else
            cout << r << endl;
        return 0;
    }
    VVI x(3,VI(3,0));
    x[0][2]=1;
    x[1][2]=1;
    x[2][2]=1;
    x[2][1]=1;
    x[1][0]=1;
    VVI a(1,VI(3,0));
    a[0][0]=p;
    a[0][1]=q;
    a[0][2]=r;
    x=matpower(x,k-3);
    cout << matmult(a,x)[0][2] << endl;
    return 0;
}
0