結果

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

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long; using mint=modint998244353; using ld = long double; 
const ll infl = 1LL << 60;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const vector<int> dx = {1, 0, -1, 0}; const vector<int> dy = {0, 1, 0, -1};
template<typename T> using vc = vector<T>; template<typename T> using vvc = vc<vc<T>>; template<typename T> using vvvc = vc<vvc<T>>;
using vi = vc<int>;  using vvi = vvc<int>; using vl = vc<ll>; using vvl = vvc<ll>; using vvvl = vvc<vl>; using vvvvl = vvc<vvl>;
using vs = vc<string>; using vvs = vvc<string>; using P = pair<ll, ll>;
#define nrep(i,n) for (ll i = 0; i < (n); ++i)
#define nfor(i,s,n) for(ll i=s;i<n;i++)//i=s,s+1...n-1 ノーマルfor
#define vc_cout(v){ll n = v.size();nrep(i,n)cout<<v[i]<<" \n"[i+1==n];}//一次元配列を出力する
#define vv_cout(v){ll n = v.size();nrep(i,n){nrep(j,v[i].size()){cout<<v[i][j]<<' ';}cout<<endl;}}//二次元配列を出力する
template<class T> using pq = priority_queue<T, vc<T>>;//★大きい順に取り出す コスト,頂点 bfs系で使う 小さい順じゃないですABC305E
template<class T> using pq_g = priority_queue<T, vc<T>, greater<T>>;//小さい順に取り出す ダイクストラ法で使う
#define cout(n) cout<<n<<endl;
vvl matmul(const vvl& a, const vvl& b, ll mod = infl) {
    ll n = a.size(), m = b[0].size(), k = a[0].size();
    vvl c(n, vl(m, 0));
    nrep(i, n) nrep(j, m) nrep(l, k) c[i][j] = (c[i][j] + a[i][l] * b[l][j]) % mod;
    return c;
}
vvl matpow(const vvl& a, ll n, ll mod = infl) {
    ll m = a.size();
    vvl res(m, vl(m, 0));
    nrep(i, m) res[i][i] = 1;
    vvl base = a;
    while (n) {
        if (n & 1) res = matmul(res, base, mod);
        base = matmul(base, base, mod);
        n >>= 1;
    }
    return res;
}
int main() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    vvl a = {{1, 2}, {3, 4}};
    vvl b = {{5}, {7}};
    cin >> a[0][0] >> a[0][1] >> a[1][0] >> a[1][1];
    cin >> b[0][0] >> b[1][0];
    ll N, K;
    cin >> N >> K;
    ll offset = 1e9;
    a[0][0] = (a[0][0] + K * (offset / K)) % K;
    a[0][1] = (a[0][1] + K * (offset / K)) % K;
    a[1][0] = (a[1][0] + K * (offset / K)) % K;
    a[1][1] = (a[1][1] + K * (offset / K)) % K;
    b[0][0] = (b[0][0] + K * (offset / K)) % K;
    b[1][0] = (b[1][0] + K * (offset / K)) % K;
    // vv_cout(a);
    // vv_cout(b);
    vvl c = matpow(a, N, K);
    // vv_cout(c);
    c = matmul(c, b, K);
    cout(c[0][0] << ' ' << c[1][0]);

    return 0;
}
0