#pragma GCC optimize("Ofast", "unroll-loops") #include using namespace std; template struct Matrix { int n; vector> element; Matrix(int n) : n(n) { element.resize(n); for (int i = 0; i < n; ++i) element[i].resize(n, 0); } Matrix operator *(Matrix a) { assert(n == a.n); Matrix res(n); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { int tmp = 0; for (int k = 0; k < n; ++k) { tmp += element[i][k] * a.element[k][j] % mod; tmp %= mod; } res.element[i][j] = tmp; } } return res; } Matrix pow(long long k) { if (k == 0) { Matrix res(n); for (int i = 0; i < n; ++i) res.element[i][i] = 1; return res; } Matrix tmp = pow(k / 2); if (k % 2 == 1) return tmp * tmp * (*this); return tmp * tmp; } }; int main(void){ int p, q, r; long long K; cin >> p >> q >> r >> K; p %= 10; q %= 10; r %= 10; Matrix<10> m(3); m.element = {{1, 1, 1}, {1, 0, 0}, {0, 1, 0}}; m = m.pow(K - 3); int ans = m.element[0][0] * r + m.element[0][1] * q + m.element[0][2] * p; ans %= 10; cout << ans << endl; return 0; }