#include #include using namespace std; using namespace atcoder; using ll = long long; using ull = unsigned long long; using pll = std::pair; #define rep(i, n) for (long long i = 0LL; i < (long long)(n); i++) #define rep2(i, s, n) for (long long i = (s); i < (long long)(n); i++) #define repr(i, n) for (long long i = (long long)(n) - 1; i >= 0LL; i--) #define repr2(i, n, s) for (long long i = (long long)(n) - 1; i >= (long long)(s); i--) template bool chmin(T &a, const T& b) { if (a > b) { a = b; return true; } return false; } template bool chmax(T &a, const T& b) { if (a < b) { a = b; return true; } return false; } const long long INF = 2e18; int main(void) { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); ll p, q, r, K; cin >> p >> q >> r >> K; vector A{p % 10, q % 10, r % 10}; map, ll> m; ll idx = 0, start = -1; while (true) { if (idx + 3 == K) { cout << A.at(idx + 2) << "\n"; exit(0); } tuple t{A.at(idx), A.at(idx + 1), A.at(idx + 2)}; if (m.count(t)) { start = m.at(t); break; } m[t] = idx; A.push_back((A.at(idx) + A.at(idx + 1) + A.at(idx + 2)) % 10); idx++; } ll cycle_size = m.size() - start; ll u = start + (K - 1 - start) % cycle_size; ll ans = A.at(u) % 10; cout << ans << "\n"; }