結果
| 問題 | No.1595 The Final Digit | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-07-09 21:35:18 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 3 ms / 2,000 ms | 
| コード長 | 869 bytes | 
| コンパイル時間 | 2,503 ms | 
| コンパイル使用メモリ | 202,536 KB | 
| 最終ジャッジ日時 | 2025-01-22 21:02:07 | 
| ジャッジサーバーID (参考情報) | judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 17 | 
ソースコード
#include<bits/stdc++.h>
#define int long long
using namespace std;
using ll=long long;
using Mat=std::vector<std::vector<ll>>;
constexpr ll mod=10;
Mat operator*(Mat a,Mat b){
    Mat res(a.size(),std::vector<ll>(b[0].size()));
    for(int i=0;i<(int)a.size();i++){
        for(int j=0;j<(int)b[0].size();j++){
            for(int k=0;k<(int)b.size();k++)(res[i][j]+=a[i][k]*b[k][j])%=mod;
        }
    }
    return res;
}
Mat mat_e(unsigned int sz){
    Mat res(sz,std::vector<ll>(sz));
    for(int i=0;i<(int)sz;i++)res[i][i]=1;
    return res;
}
Mat mat_pow(Mat a,ll b){
    if(!b)return mat_e(a.size());
    if(b&1)return mat_pow(a,b-1)*a;
    return mat_pow(a*a,b/2);
}
signed main(){
    int p,q,r,K; cin>>p>>q>>r>>K;
    vector<vector<int>> A{{1,1,1},{1,0,0},{0,1,0}};
    vector<vector<int>> B{{r%10},{q%10},{p%10}};
    cout<<(mat_pow(A,K-1)*B)[2][0]<<endl;
}
            
            
            
        