#include using namespace std; #define bokusunny ios::sync_with_stdio(false), cin.tie(nullptr); template struct Matrix { private: vector> A; public: Matrix(const int &r, const int &c) : A(r, vector(c, 0)) {} Matrix(const int &n) : A(n, vector(n, 0)) {} int row_size() const { return (int)A.size(); } int column_size() const { return (int)A[0].size(); } static Matrix I(int n) { Matrix res(n); for (int i = 0; i < n; i++) res[i][i] = 1; return res; } inline const vector &operator[](int r) const { assert(0 <= r && r < row_size()); return A[r]; } vector &operator[](int r) { assert(0 <= r && r < row_size()); return A[r]; } Matrix &operator+=(const Matrix &B) { int ra = row_size(), ca = column_size(); int rb = B.row_size(), cb = B.column_size(); assert(ra == rb && ca == cb); for (int i = 0; i < ra; i++) { for (int j = 0; j < ca; j++) { (*this)[i][j] += B[i][j]; } } return (*this); } Matrix &operator-=(const Matrix &B) { int ra = row_size(), ca = column_size(); int rb = B.row_size(), cb = B.column_size(); assert(ra == rb && ca == cb); for (int i = 0; i < ra; i++) { for (int j = 0; j < ca; j++) { (*this)[i][j] -= B[i][j]; } } return (*this); } Matrix &operator*=(const Matrix &B) { int ra = row_size(), ca = column_size(); int rb = B.row_size(), cb = B.column_size(); assert(ca == rb); vector C(ra, vector(cb, 0)); for (int i = 0; i < ra; i++) { for (int j = 0; j < cb; j++) { for (int k = 0; k < ca; k++) { C[i][j] += (*this)[i][k] * B[k][j]; C[i][j] %= 10; } } } A.swap(C); return (*this); } Matrix &operator^=(long long k) { Matrix B = Matrix::I(row_size()); while (k > 0) { if (k & 1) B *= *this; *this *= *this; k >>= 1; } A.swap(B.A); return (*this); } Matrix operator+(const Matrix &B) const { return (Matrix(*this) += B); } Matrix operator-(const Matrix &B) const { return (Matrix(*this) -= B); } Matrix operator*(const Matrix &B) const { return (Matrix(*this) *= B); } Matrix operator^(const long long k) const { return (Matrix(*this) ^= k); } friend ostream &operator<<(ostream &os, Matrix &Mat) { int r = Mat.row_size(), c = Mat.column_size(); for (int i = 0; i < r; i++) { os << "["; for (int j = 0; j < c; j++) { os << Mat[i][j] << (j + 1 == c ? "]\n" : ","); } } return (os); } }; void solve() { int p, q, r; long long K; cin >> p >> q >> r >> K; Matrix A(3, 1); A[0][0] = p % 10; A[1][0] = q % 10; A[2][0] = r % 10; Matrix B(3, 3); B[0][1] = B[1][2] = B[2][0] = B[2][1] = B[2][2] = 1; K -= 3; B ^= K; B *= A; cout << B[2][0] << endl; } int main() { bokusunny; solve(); return 0; }