#include using namespace std; using ll = long long; #define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++) #include using mint = atcoder::static_modint<10>; namespace mat { template using matrix = vector>; template matrix idMat(int n) { matrix res(n, vector(n, 0)); rep(i, 0, n) res[i][i] = 1; return res; } template matrix matMul(matrix a, matrix b) { assert(a.size() && b.size()); assert(a.size() == b[0].size() && a[0].size() == b.size()); matrix res(a.size(), vector(b.size(), 0)); rep(i, 0, a.size()) { rep(j, 0, b.size()) { rep(k, 0, b.size()) { res[i][j] += a[i][k] * b[k][j]; } } } return res; } template vector vecMul(matrix a, vector b) { assert(a.size() && b.size()); assert(a[0].size() == b.size()); vector res(b.size(), 0); rep(i, 0, a.size()) { rep(j, 0, b.size()) { res[i] += a[i][j] * b[j]; } } return res; } template matrix matPow(matrix a, ll p) { assert(a.size() && a.size() == a[0].size()); matrix res(a.size(), vector(a.size(), 0)); rep(i, 0, a.size()) res[i][i] = 1; while(p) { if(p & 1) res = matMul(res, a); a = matMul(a, a); p /= 2; } return res; } } void solve() { ll p, q, r, k; cin >> p >> q >> r >> k; vector v = {p, q, r}; vector> x = {{0, 1, 0}, {0, 0, 1}, {1, 1, 1}}; x = mat::matPow(x, k-3); auto ans = mat::vecMul(x, v); cout << ans[2].val() << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); return 0; }