結果

問題 No.1595 The Final Digit
ユーザー yamakeyamake
提出日時 2021-07-09 21:41:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,566 bytes
コンパイル時間 2,316 ms
コンパイル使用メモリ 176,588 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-14 08:12:29
合計ジャッジ時間 2,827 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 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,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 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 <bits/stdc++.h>

using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define debug(output) if(debugFlag)cout<<#output<<"= "<<output<<endl
using lint = long long;
typedef pair<int,int> P;
const bool debugFlag=true;
const lint linf=1e18+7;
const lint inf=1e9+7;
const int MOD=1000000007;
template<typename T>
class Matrix{
    public:
    int size;
    vector<vector<T>> mat;
    Matrix(int n){
        size=n;
        mat.resize(n,vector<T>(n,0));
    }
    void in(T value,int i,int j){
        mat[i][j]+=value;
    }
    T out(int i,int j){
        return mat[i][j];
    }
    Matrix<T> mul(Matrix<T> A,Matrix<T> B){
        int n=A.size;
        Matrix<T> res(n);
        for(int i=0;i<n;++i){
            for(int j=0;j<n;++j){
                T sum=0;
                for(int k=0;k<n;++k){
                    sum+=A.out(i,k)*B.out(k,j);
                    sum%=10;
                }
                res.in(sum,i,j);
            }
        }
        return res;
    }
    Matrix<T> pow(long long k){
        Matrix<T> res(size);
        Matrix<T> A(*this);
        for(int i=0;i<size;++i)res.in(1,i,i);
        while(k>0){
            if(k&1)res=mul(res,A);
            k/=2;
            A=mul(A,A);
        }
        return res;
    }
};
signed main(){
  lint p,q,r,k;cin>>p>>q>>r>>k;
  p%=10;q%=10;r%=10;
  Matrix<lint> mat(3);
  rep(i,3)mat.in(1,0,i);
  mat.in(1,1,0);mat.in(1,2,1);
  mat=mat.pow(k-3);
  lint res=mat.out(0,0)*r+mat.out(0,1)*q+mat.out(0,2)*p;
  res%=10;
  cout<<res<<"\n";
  return 0;
}
0