結果
問題 |
No.3226 2×2行列累乗
|
ユーザー |
|
提出日時 | 2025-08-08 21:38:10 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 3,138 bytes |
コンパイル時間 | 3,959 ms |
コンパイル使用メモリ | 255,836 KB |
実行使用メモリ | 7,720 KB |
最終ジャッジ日時 | 2025-08-08 21:38:15 |
合計ジャッジ時間 | 5,038 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
#include <bits/stdc++.h> #include <atcoder/all> using namespace std; using mint = atcoder::modint; using ll = long long; template <class T, size_t N> struct Matrix { std::array<std::array<T, N>, N> A{}; Matrix() {} Matrix(const std::array<std::array<T, N>, N> &M) : A(M){} Matrix(const std::vector<std::vector<T>> &M) { for(size_t i = 0; i < N; i++){ for(size_t j = 0; j < N; j++){ A[i][j] = M[i][j]; } } } const std::array<T, N>& operator[](int i) const { return A[i]; } std::array<T, N>& operator[](int i) { return A[i]; } Matrix& operator+=(const Matrix& rhs) { for(size_t i = 0; i < N; i++){ for(size_t j = 0; j < N; j++){ A[i][j] += rhs[i][j]; } } return *this; } Matrix& operator-=(const Matrix& rhs) { for(size_t i = 0; i < N; i++){ for(size_t j = 0; j < N; j++){ A[i][j] -= rhs[i][j]; } } return *this; } Matrix& operator*=(const Matrix& rhs) { std::array<std::array<T, N>, N> res{}; for(size_t i = 0; i < N; i++){ for(size_t j = 0; j < N; j++){ for(size_t k = 0; k < N; k++){ res[i][j] += A[i][k] * rhs[k][j]; } } } swap(A, res); return *this; } Matrix& operator+() const { return *this; } Matrix& operator-() const { return Matrix() - *this; } friend Matrix operator+(const Matrix& lhs, const Matrix& rhs) { return Matrix(lhs) += rhs; } friend Matrix operator-(const Matrix& lhs, const Matrix& rhs) { return Matrix(lhs) -= rhs; } friend Matrix operator*(const Matrix& lhs, const Matrix& rhs) { return Matrix(lhs) *= rhs; } friend bool operator==(const Matrix& lhs, const Matrix& rhs) { return (lhs.A == rhs.A); } friend bool operator!=(const Matrix& lhs, const Matrix& rhs) { return (lhs.A != rhs.A); } Matrix pow(long long v){ Matrix res, temp = A; for(size_t i = 0; i < N; i++)res[i][i] = 1; while(v){ if(v & 1)res *= temp; temp *= temp; v >>= 1; } return res; } friend std::ostream& operator << (std::ostream &os, const Matrix& rhs) noexcept { for(size_t i = 0; i < N; i++){ if(i) os << '\n'; for(size_t j = 0; j < N; j++){ os << (j ? " " : "") << rhs[i][j]; } } return os; } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); Matrix<int, 2> A; for(int y = 0; y < 2; y++){ for(int x = 0; x < 2; x++){ cin >> A[y][x]; } } ll s, t, n, k; cin >> s >> t >> n >> k; mint::set_mod(k); Matrix<mint, 2> B; for(int y = 0; y < 2; y++){ for(int x = 0; x < 2; x++){ B[y][x] = A[y][x]; } } auto C = B.pow(n); cout << mint(C[0][0] * s + C[0][1] * t).val() << ' ' << mint(C[1][0] * s + C[1][1] * t).val() << '\n'; }