#ifdef __LOCAL #define _GLIBCXX_DEBUG #endif #include using namespace std; using ll = long long; using P = pair; using PIL = pair; using PLI = pair; using PLL = pair; template bool chmin(T &a, T b) {if(a>b){a=b;return 1;}return 0;} template bool chmax(T &a, T b) {if(a void show_vec(T v) {for (int i=0;i void show_pair(T p) {cout< bool judge_digit(T bit,T i) {return (((bit&(1LL< h_idx4 = {-1, 0,0,1}; const vector w_idx4 = { 0,-1,1,0}; const vector h_idx8 = {-1,-1,-1, 0,0, 1,1,1}; const vector w_idx8 = {-1, 0, 1,-1,1,-1,0,1}; // (an * am)行列A と (bn * bm)行列B の積を求める // am == bn のとき, (an * bm)行列を返す vector> Matrix_product(vector> &A,vector> &B){ int an = A.size(),bn = B.size(); int am = A[0].size(),bm = B[0].size(); assert(am == bn); vector> res(an, vector(bm,0)); for (int i = 0; i < an; i++){ for (int j = 0; j < bm; j++){ for (int k = 0; k < am; k++){ res[i][j] += A[i][k] * B[k][j]; res[i][j] %= 10; } } } return res; } // 行列Mに対し,M^nを返す (n >= 0) vector> Matrix_pow(vector> &M,ll n){ int x = M.size(); int y = M[0].size(); assert(n >= 0 && x == y); vector> res(x, vector(x,0)); // 単位行列 for (int i = 0; i < x; i++){ res[i][i] = 1; } vector> a = M; while (n > 0){ if (n & 1) res = Matrix_product(res,a); a = Matrix_product(a,a); n >>= 1; } return res; } void show_Matrix(vector> &M){ for (int i = 0; i < M.size(); i++){ for (int j = 0; j < M[i].size(); j++){ cout << M[i][j] << " "; } cout << endl; } return; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); ll p,q,r,k; cin >> p >> q >> r >> k; p %= 10; q %= 10; r %= 10; vector> M(3, vector(3,0)); M[0][2] = 1; M[1][0] = 1; M[1][2] = 1; M[2][1] = 1; M[2][2] = 1; M = Matrix_pow(M,(k - 3)); vector> x(1, vector(3)); x[0][0] = p; x[0][1] = q; x[0][2] = r; vector> L = Matrix_product(x,M); cout << L[0][2] << endl; }