結果

問題 No.3226 2×2行列累乗
ユーザー wasd314
提出日時 2025-08-08 22:01:23
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,970 bytes
コンパイル時間 2,825 ms
コンパイル使用メモリ 177,532 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-08-08 22:01:27
合計ジャッジ時間 3,815 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <iostream>
#include <vector>

#include <atcoder/all>
// using namespace std;
// using namespace atcoder;
// using mint = modint998244353;


template <typename T>
struct mat22 {
    std::array<T, 4> a;

    mat22() : a{} {}
    mat22(const std::array<T, 4> &a_) : a(a_) {}
    // mat22(const Vec &v) : h(v.size()), w(1)
    // {
    //     for (auto x : v) {
    //         a.push_back({x});
    //     }
    // }

    mat22 &unit()
    {
        a[0] = 1;
        a[3] = 1;
        return *this;
    }

    T &operator[](int i, int j) { return a[i * 2 + j]; }
    const T &operator[](int i, int j) const { return a[i * 2 + j]; }

    // Vec as_vec() const
    // {
    //     assert(w == 1);
    //     Vec ans;
    //     for (int i = 0; i < h; i++) {
    //         ans.push_back(a[i][0]);
    //     }
    //     return ans;
    // }

    mat22 operator*(const mat22 &b) const
    {
        mat22 ans{};
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 2; k++) {
                    // ans.a[i][k] += a[i][j] * b.a[j][k];
                    ans[i, j] += (*this)[i, k] * b[k, j];
                }
            }
        }
        return ans;
    }

    // void print() const
    // {
    //     cout << "[\n";
    //     for (auto &ai : a) {
    //         cout << "  ( ";
    //         for (auto &b : ai) {
    //             cout << b.val() << " ";
    //         }
    //         cout << ")" << endl;
    //     }
    //     cout << "]" << endl;
    // }

    mat22 pow(long long n) const
    {
        if (!n) return mat22().unit();
        if (n == 1) return *this;
        mat22 h = pow(n >> 1);
        h = h * h;
        if (n & 1) h = h * (*this);
        return h;
        // mat22 x = *this, r = mat22(h, h).unit();
        // while (n) {
        //     if (n & 1) r = r * x;
        //     x = x * x;
        //     n >>= 1;
        // }
        // return r;
    }
};


template <typename Mint>
struct Binomial {
    std::vector<Mint> fact, invfact;
    Binomial(int nn) : fact(nn, 1), invfact(nn, 1)
    {
        for (int i = 0; i < nn - 1; i++) fact[i + 1] = fact[i] * (i + 1);
        invfact[nn - 1] = fact[nn - 1].inv();
        for (int i = nn - 2; i >= 0; i--) invfact[i] = invfact[i + 1] * (i + 1);
    }
    Mint operator()(int x, int y) const
    {
        if (x < 0 || y < 0 || x - y < 0) return 0;
        return fact[x] * invfact[y] * invfact[x - y];
    }
};

int main()
{
    using namespace std;

    using mint = atcoder::modint;
    using mat_t = array<mint, 4>;
    using lint = long long;
    array<lint, 4> m0;
    for (auto &e : m0) cin >> e;
    lint s, t;
    cin >> s >> t;
    lint n, k;
    cin >> n >> k;
    mint::set_mod(k);
    mat22<mint> m({m0[0], m0[1], m0[2], m0[3]});
    m = m.pow(n);
    mat22<mint> x({s, 0, t, 0});
    auto nx = m * x;
    cout << nx[0, 0].val() << " " << nx[1, 0].val() << endl;
}
0