#include #include #define ll long long int #define INF 1000000000000000000 using namespace atcoder; using namespace std; template struct matrix { public: matrix() {} matrix(int n, int m) : mat(n, std::vector(m, 0)) {} matrix(int n) : mat(n, std::vector(n, 0)) {} int height() const { return mat.size(); } int width() const { return mat[0].size(); } inline const std::vector &operator[](int k) const { return (mat.at(k)); } inline std::vector &operator[](int k) { return (mat.at(k)); } static matrix I(size_t n) { matrix mat(n); for (int i = 0; i < n; i++) mat[i][i] = 1; return (mat); } matrix &operator+=(const matrix b) { int n = height(), m = width(); assert(n == b.height() && m == b.width()); for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) (*this)[i][j] += b[i][j]; return (*this); } matrix &operator-=(const matrix b) { int n = height(), m = width(); assert(n == b.height() && m == b.width()); for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) (*this)[i][j] -= b[i][j]; return (*this); } matrix &operator*=(const matrix b) { int n = height(), m = width(), p = b.width(); assert(m == b.height()); std::vector< std::vector< T > > c(n, std::vector< T >(p, 0)); for(int i = 0; i < n; i++) for(int j = 0; j < p; j++) for(int k = 0; k < m; k++) c[i][j] = (c[i][j] + (*this)[i][k] * b[k][j]); mat.swap(c); return (*this); } matrix &operator^=(long long int k) { matrix b = matrix::I(height()); while(k > 0) { if(k & 1) b *= *this; *this *= *this; k >>= 1LL; } mat.swap(b.mat); 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 int k) const { return (matrix(*this) ^= k); } friend std::ostream &operator<<(std::ostream &os, matrix p) { int n = p.height(); int m = p.width(); for(int i = 0; i < n; i++) { os << "["; for(int j = 0; j < m; j++) { os << p[i][j] << (j + 1 == m ? "]\n" : ","); } } return (os); } T determinant() { matrix b(*this); assert(width() == height()); T ret = 1; for(int i = 0; i < width(); i++) { int idx = -1; for(int j = i; j < width(); j++) { if(b[j][i] != 0) idx = j; } if(idx == -1) return (0); if(i != idx) { ret *= -1; swap(b[i], b[idx]); } ret *= b[i][i]; T vv = b[i][i]; for(int j = 0; j < width(); j++) { b[i][j] /= vv; } for(int j = i + 1; j < width(); j++) { T a = b[j][i]; for(int k = 0; k < width(); k++) { b[j][k] -= b[i][k] * a; } } } return (ret); } private: std::vector> mat; }; using mint = modint; int main(void){ mint::set_mod(10); ll p, q, r, k; cin >> p >> q >> r >> k; matrix mat(3); mat[0][0] = 1; mat[0][1] = 1; mat[0][2] = 1; mat[1][0] = 1; mat[2][1] = 1; mat ^= (k-3); matrix v(3, 1); v[0][0] = r; v[1][0] = q; v[2][0] = p; mat *= v; cout << mat[0][0].val() << endl; return 0; }