#pragma region template // clang-format off #ifdef LOCAL_NDEBUG #include #endif // #define USE_EXTERNAL_CONTAINERS // #define NO_PRINT_INF #ifdef USE_EXTERNAL_CONTAINERS #include #include #include #include template using rb_tree = __gnu_pbds::tree, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>; template using hash_table = __gnu_pbds::gp_hash_table; #endif #ifdef LOCAL_DEBUG #if (defined USE_EXTERNAL_CONTAINERS) || (defined NO_PRINT_INF) #include <../src/debugger.hpp> #else #include #endif #endif // #define PROPER #define _USE_MATH_DEFINES #if (defined __INTELLISENSE__) && (!defined PROPER) #define NDEBUG namespace std { #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if (defined __INTELLISENSE__) && (!defined PROPER) using namespace std; } #endif #ifdef LOCAL_DEBUG #define STR(x) #x #define STRINGIFY(x) STR(x) #define FILE_LINE "[Debug] ./" __FILE__ ":" STRINGIFY(__LINE__) #define see(...) debugger::multi_print(#__VA_ARGS__, __VA_ARGS__) #define see2(arg) arg.debug_print(#arg) #define here(...) debugger::os << FILE_LINE << " in " << __func__ << ": \033[32mReached\033[39m\n" #define com(msg) debugger::os << FILE_LINE << " in " << __func__ << ":\n \033[36mComment:\033[39m " << msg << "\n" #define err(msg) debugger::os << FILE_LINE << " in " << __func__ << ":\n \033[31mError:\033[39m " << msg << "\n" #define local(...) do { __VA_ARGS__ } while (0) #define alter(x, y) x #else #define see(...) (static_cast(0)) #define see2(arg) (static_cast(0)) #define here(...) (static_cast(0)) #define com(msg) (static_cast(0)) #define err(msg) (static_cast(0)) #define local(...) (static_cast(0)) #define alter(x, y) y #endif #if (defined LOCAL_DEBUG) && (!defined NOWARN) #define warn(msg) debugger::os << FILE_LINE << " in " << __func__ << ":\n \033[33mWarning:\033[39m " << msg << "\n" #else #define warn(msg) (static_cast(0)) #endif #if (defined LOCAL_DEBUG) || (defined LOCAL_NDEBUG) || (defined __INTELLISENSE__) #define NOEXCEPT #define M_assert(expr) assert(expr) #define O_assert(expr) assert(expr) #else #define NOEXCEPT noexcept #define M_assert(expr) do {if(__builtin_expect(!(expr), 0)) {auto p = static_cast(std::malloc(1 << 30)); for (int i = 0; i < (1 << 27); p[i] = 1, i += (1 << 9)); std::cerr << (*p);}} while (0) #define O_assert(expr) do {if(__builtin_expect(!(expr), 0)) {const auto X = std::string(1000, '-'); for(int i = 0; i < (1 << 18); i++) std::cout << X;}} while (0) #endif #define as(type, val) static_cast>(val) #define INDIRECT(...) __VA_ARGS__ #define rep(loop_var, loop_end) \ for ( \ auto loop_var = as(std::make_signed_t, 0); \ loop_var < as(decltype(loop_var), loop_end); \ ++loop_var \ ) #define rng(loop_var, loop_start, loop_end, loop_increment) \ for ( \ auto loop_var = as(INDIRECT(std::make_signed_t>), loop_start); \ ((loop_increment) > 0) ? (loop_var < as(decltype(loop_var), loop_end)) : (loop_var > as(decltype(loop_var), loop_end)); \ loop_var += (loop_increment) \ ) #define erng(loop_var, loop_start, loop_end, loop_increment) \ for ( \ auto loop_var = as(INDIRECT(std::make_signed_t>), loop_start); \ ((loop_increment) > 0) ? (loop_var <= as(decltype(loop_var), loop_end)) : (loop_var >= as(decltype(loop_var), loop_end)); \ loop_var += (loop_increment) \ ) [[maybe_unused]] constexpr int INF = 1000000005; [[maybe_unused]] constexpr long long LINF = 1000000000000000005LL; [[maybe_unused]] constexpr double EPS = 1e-9; [[maybe_unused]] constexpr long double LEPS = 1e-14L; [[maybe_unused]] constexpr int dy[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; [[maybe_unused]] constexpr int dx[9] = {0, 1, 0, -1, -1, 1, 1, -1, 0}; template constexpr auto Min(const Ts... args) { return std::min({ as(std::common_type_t, args) ... }); } template constexpr auto Max(const Ts... args) { return std::max({ as(std::common_type_t, args) ... }); } // clang-format on #pragma endregion #pragma region lib_static_matrix #define lib_matrix #define lib_static_matrix #define static_loop(i, R, C, code) \ [&](std::index_sequence) constexpr { ((code), ...); } \ (std::make_index_sequence {}) namespace lib { /* prototype declarations */ template class static_matrix; template constexpr static_matrix zero_matrix(); template constexpr static_matrix id_matrix(); template class static_matrix { std::array, R> data; public: constexpr static_matrix(void) {} constexpr static_matrix(const std::initializer_list& src) : data() { auto iter = src.begin(); static_loop(i, R, C, std::get(std::get(data)) = *(iter++)); } constexpr static_matrix(std::initializer_list&& src) : data() { auto iter = src.begin(); static_loop(i, R, C, std::get(std::get(data)) = std::move(*(iter++))); } static constexpr int rows(void) noexcept { return R; } static constexpr int cols(void) noexcept { return C; } static constexpr int elems(void) noexcept { return R * C; } static constexpr bool is_square(void) noexcept { return R == C; } constexpr Tp& operator()(const std::size_t i, const std::size_t j) { return data[i][j]; } constexpr Tp operator()(const std::size_t i, const std::size_t j) const { return data[i][j]; } template constexpr Tp& get(void) { return std::get(std::get(data)); } template constexpr Tp get(void) const { return std::get(std::get(data)); } template constexpr Tp& get(void) { return std::get(std::get(data)); } template constexpr Tp get(void) const { return std::get(std::get(data)); } template static constexpr static_matrix elemwise_op(static_matrix lhs, const static_matrix& rhs) { static_loop(i, R, C, lhs.template get() = op {}(lhs.template get(), rhs.template get())); return lhs; } constexpr static_matrix& operator+(void) { return *this; } constexpr static_matrix operator+(void) const { return *this; } constexpr static_matrix operator-(void) const { auto res(*this); static_loop(i, R, C, res.template get() = -res.template get()); return res; } constexpr static_matrix operator+(const static_matrix& rhs) const { return elemwise_op>(*this, rhs); } constexpr static_matrix operator-(const static_matrix& rhs) const { return elemwise_op>(*this, rhs); } constexpr static_matrix& operator+=(const static_matrix& rhs) { return (*this) = (*this) + rhs; } constexpr static_matrix& operator-=(const static_matrix& rhs) { return (*this) = (*this) - rhs; } template constexpr auto operator*(const static_matrix& rhs) const { auto res = zero_matrix(); [&](std::index_sequence) constexpr { ((res.template get() += get() * rhs.template get<(i / C2) % C, i % C2>()), ...); } (std::make_index_sequence {}); return res; } constexpr decltype(auto) operator*=(const static_matrix& rhs) { return (*this) = (*this) * rhs; } static_matrix pow(long long index) const { static_assert(is_square()); auto res = id_matrix(), base = (*this); while (index) { if (index & 1) res *= base; base *= base; index >>= 1; } return res; } void pow_self(long long index) { static_assert(is_square()); if (index == 0) { *this = id_matrix(); return; } index--; auto base = (*this); while (index) { if (index & 1) (*this) *= base; base *= base; index >>= 1; } } template constexpr ReturnType total(void) const { ReturnType res = 0; [&](std::index_sequence) { ((res += std::reduce(std::get(data).cbegin(), std::get(data).cend(), (ReturnType) 0)), ...); } (std::make_index_sequence {}); return res; } constexpr static_matrix transposed(void) const noexcept { static_matrix res; static_loop(i, R, C, INDIRECT(res.template get() = get())); return res; } #ifdef LOCAL_DEBUG void debug_print(std::string_view name = "static_matrix") const { debugger::os << name << ": "; const auto padding = std::string(name.size() + 2, ' '); for (int i = 0; i < R; i++) { if (i > 0) debugger::os << padding; debugger::os << "[ "; for (int j = 0; j < C; j++) debugger::os << get(i, j) << " "; debugger::os << "]\n"; } } #else void debug_print(std::string_view) const noexcept {} #endif }; namespace internal { template constexpr bool is_static_matrix_v = false; template constexpr bool is_static_matrix_v> = true; } // namespace internal template , std::nullptr_t> = nullptr> constexpr static_matrix operator*(const LhsType lhs, static_matrix rhs) { static_loop(i, R, C, rhs.template get() = lhs * rhs.template get()); return rhs; } template , std::nullptr_t> = nullptr> constexpr static_matrix operator*(static_matrix lhs, const RhsType rhs) { static_loop(i, R, C, lhs.template get() *= rhs); return lhs; } template , std::nullptr_t> = nullptr> constexpr static_matrix& operator*=(static_matrix& lhs, const RhsType rhs) { static_loop(i, R, C, lhs.template get() *= rhs); return lhs; } template constexpr static_matrix zero_matrix() { static_matrix res; static_loop(i, R, C, res.template get() = 0); return res; } template constexpr static_matrix id_matrix() { static_matrix res; static_loop(i, R, R, res.template get() = ((i / R) == (i % R))); return res; } template using static_vector = static_matrix; template constexpr Tp dot(const static_vector& lhs, const static_vector& rhs) { Tp res = 0; [&](std::index_sequence) constexpr { ((res += lhs.template get() * rhs.template get()), ...); } (std::make_index_sequence {}); return res; } template constexpr Tp cross(const static_vector& lhs, const static_vector& rhs) { return lhs.template get<0, 0>() * rhs.template get<1, 0>() - lhs.template get<1, 0>() * rhs.template get<0, 0>(); } template constexpr static_vector cross(const static_vector& lhs, const static_vector& rhs) { return static_vector({ lhs.template get<1, 0>() * rhs.template get<2, 0>() - lhs.template get<2, 0>() * rhs.template get<1, 0>(), lhs.template get<2, 0>() * rhs.template get<0, 0>() - lhs.template get<0, 0>() * rhs.template get<2, 0>(), lhs.template get<0, 0>() * rhs.template get<1, 0>() - lhs.template get<1, 0>() * rhs.template get<0, 0>(), }); } } // namespace lib #undef static_loop #define at(i, ...) template get() #pragma endregion struct Int { int val; Int() {} Int(const int x) : val(x % 10) {} Int operator+(Int rhs) const { rhs.val += val; if (rhs.val >= 10) rhs.val -= 10; return rhs; } Int operator-(Int rhs) const { rhs.val -= val; if (rhs.val < 0) rhs.val += 10; return rhs; } Int operator*(Int rhs) const { rhs.val *= val; rhs.val %= 10; return rhs; } Int operator+=(Int rhs) { return (*this) = ((*this) + rhs); } Int operator-=(Int rhs) { return (*this) = ((*this) - rhs); } Int operator*=(Int rhs) { return (*this) = ((*this) * rhs); } }; void solve() { int p, q, r; long long K; std::cin >> p >> q >> r >> K; auto M = lib::zero_matrix(); auto V = lib::static_vector(); V.at(0) = p; V.at(1) = q; V.at(2) = r; M.at(0, 1) = M.at(1, 2) = M.at(2, 0) = M.at(2, 1) = M.at(2, 2) = 1; M.pow_self(K - 1); std::cout << (M * V).at(0).val << "\n"; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); solve(); }