結果

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll A,B,C,D,S,T,N,K; 
  if(!(cin>>A>>B>>C>>D>>S>>T>>N>>K)) return 0;

  auto norm = [&](ll x)->ll{ x%=K; if(x<0) x+=K; return x; };

  struct M2 {
    ll a00,a01,a10,a11; // 2x2
  };

  auto mul = [&](const M2& X, const M2& Y)->M2{
    // 各項を K で正規化しておけば 1e8^2 ≈ 1e16 なので ll で安全
    M2 Z;
    Z.a00 = norm( norm(X.a00*Y.a00) + norm(X.a01*Y.a10) );
    Z.a01 = norm( norm(X.a00*Y.a01) + norm(X.a01*Y.a11) );
    Z.a10 = norm( norm(X.a10*Y.a00) + norm(X.a11*Y.a10) );
    Z.a11 = norm( norm(X.a10*Y.a01) + norm(X.a11*Y.a11) );
    return Z;
  };

  // 初期行列を K で正規化
  M2 base{
    norm(A), norm(B),
    norm(C), norm(D)
  };

  // M^N を二分累乗
  M2 acc{1%K,0,0,1%K}; // 単位行列(もKで正規化)
  ll n = N;
  while(n){
    if(n&1) acc = mul(acc, base);
    base = mul(base, base);
    n >>= 1;
  }

  // ベクトル (S,T) も正規化してから掛ける
  ll s = norm(S), t = norm(T);
  ll r = norm( norm(acc.a00*s) + norm(acc.a01*t) );
  ll u = norm( norm(acc.a10*s) + norm(acc.a11*t) );

  cout << r << ' ' << u << '\n';
  return 0;
}
0