結果
問題 |
No.3226 2×2行列累乗
|
ユーザー |
|
提出日時 | 2025-08-08 22:03:37 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 2,502 bytes |
コンパイル時間 | 2,494 ms |
コンパイル使用メモリ | 278,388 KB |
実行使用メモリ | 7,720 KB |
最終ジャッジ日時 | 2025-08-08 22:03:41 |
合計ジャッジ時間 | 3,524 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
#include <bits/stdc++.h> static inline constexpr void mul(std::array<std::array<uint_fast32_t, 2>, 2>& result, const std::array<std::array<uint_fast32_t, 2>, 2>& a, const std::array<std::array<uint_fast32_t, 2>, 2>& b, const uint_fast32_t K) noexcept { for (uint_fast32_t i = 0; i != 2; ++i) for (uint_fast32_t j = 0; j != 2; ++j) result[i][j] = (static_cast<uint_fast64_t>(a[i][0]) * b[0][j] + static_cast<uint_fast64_t>(a[i][1]) * b[1][j]) % K; } static inline constexpr std::array<std::array<uint_fast32_t, 2>, 2> prepare(const std::array<std::array<int_fast32_t, 2>, 2>& M, const uint_fast32_t K) noexcept { std::array<std::array<uint_fast32_t, 2>, 2> new_M = { { 0, }, }; for (uint_fast32_t i = 0; i != 2; ++i) for (uint_fast32_t j = 0; j != 2; ++j) new_M[i][j] = (M[i][j] + K * UINT64_C(50'000'000)) % K; return new_M; } static inline constexpr std::pair<uint_fast32_t, uint_fast32_t> solve(const std::array<std::array<uint_fast32_t, 2>, 2>& M, const uint_fast32_t S, const uint_fast32_t T, const uint_fast64_t N, const uint_fast32_t K) noexcept { std::array<std::array<std::array<uint_fast32_t, 2>, 2>, 2> ans_dp = { std::array<std::array<uint_fast32_t, 2>, 2>{ std::array<uint_fast32_t, 2>{ 1, 0 }, std::array<uint_fast32_t, 2>{ 0, 1 } }, std::array<std::array<uint_fast32_t, 2>, 2>{ std::array<uint_fast32_t, 2>{ 1, 0 }, std::array<uint_fast32_t, 2>{ 0, 1 } } }; std::array<std::array<std::array<uint_fast32_t, 2>, 2>, 2> base_dp = { M, { { 0, }, } }; for (uint_fast32_t i = 0; i != 60; ++i) { if ((N >> i) & 1) mul(ans_dp[(i & 1) ^ 1], ans_dp[i & 1], base_dp[i & 1], K); else ans_dp[(i & 1) ^ 1] = ans_dp[i & 1]; mul(base_dp[(i & 1) ^ 1], base_dp[i & 1], base_dp[i & 1], K); } return { (static_cast<uint_fast64_t>(ans_dp[0][0][0]) * S + static_cast<uint_fast64_t>(ans_dp[0][0][1]) * T) % K, (static_cast<uint_fast64_t>(ans_dp[0][1][0]) * S + static_cast<uint_fast64_t>(ans_dp[0][1][1]) * T) % K }; } static inline void output(const std::pair<uint_fast32_t, uint_fast32_t>& ans) noexcept { std::cout << ans.first << ' ' << ans.second << '\n'; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::array<std::array<int_fast32_t, 2>, 2> M; int_fast32_t S, T; uint_fast64_t N; uint_fast32_t K, i, j; for (i = 0; i != 2; ++i) for (j = 0; j != 2; ++j) std::cin >> M[i][j]; std::cin >> S >> T >> N >> K; output(solve(prepare(M, K), (S + K * UINT64_C(50'000'000)) % K, (T + K * UINT64_C(50'000'000)) % K, N, K)); return 0; }