結果

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

ソースコード

diff #

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

//入力が必ず-mod<a<modの時.
template<int idx> //modが入力で与えられる場合. 
struct dynamic_modint{ //mod変更が可能 最初にsetmod必須 idxで複数個所持が可能.
    private:
    static int mod;
    public:
    long long v = 0;
    static constexpr long long getmod(){return mod;}
    static void setmod(int m){
        assert(m > 0);
        mod = m;
    }
    dynamic_modint(){v = 0;}
    dynamic_modint(int a){v = a<0?a+mod:a;} dynamic_modint(long long a){v = a<0?a+mod:a;}
    dynamic_modint(unsigned int a){v = a;}
    dynamic_modint(unsigned long long a){v = a;}
    long long val()const{return v;}
    void modu(){v = (v%mod+mod)%mod;}
 
    dynamic_modint &operator=(const dynamic_modint &b) = default;
    dynamic_modint operator+()const{return (*this);}
    dynamic_modint operator-()const{return dynamic_modint(0)-(*this);}
    dynamic_modint operator+(const dynamic_modint b)const{return dynamic_modint(v)+=b;}
    dynamic_modint operator-(const dynamic_modint b)const{return dynamic_modint(v)-=b;}
    dynamic_modint operator*(const dynamic_modint b)const{return dynamic_modint(v)*=b;}
    dynamic_modint operator/(const dynamic_modint b)const{return dynamic_modint(v)/=b;}
    dynamic_modint operator+=(const dynamic_modint b){
        v += b.v; if(v >= mod) v -= mod;
        return *this;
    }
    dynamic_modint operator-=(const dynamic_modint b){
        v -= b.v; if(v < 0) v += mod; 
        return *this;
    }   
    dynamic_modint operator*=(const dynamic_modint b){v = v*b.v%mod; return *this;}
    dynamic_modint operator/=(dynamic_modint b){ //b!=0 mod素数が必須.
        if(b == 0) assert(false);
        int left = mod-2;
        while(left){if(left&1) *this *= b; b *= b; left >>= 1;}
        return *this;
    }
    dynamic_modint operator++(){*this += 1; return *this;}
    dynamic_modint operator--(){*this -= 1; return *this;}
    dynamic_modint operator++(int){*this += 1; return *this;}
    dynamic_modint operator--(int){*this -= 1; return *this;}
    bool operator==(const dynamic_modint b)const{return v == b.v;}
    bool operator!=(const dynamic_modint b)const{return v != b.v;}
    bool operator>(const dynamic_modint b)const{return v > b.v;}
    bool operator>=(const dynamic_modint b)const{return v >= b.v;}
    bool operator<(const dynamic_modint b)const{return v < b.v;}
    bool operator<=(const dynamic_modint b)const{return v <= b.v;}
    dynamic_modint pow(long long n)const{
        dynamic_modint ret = 1,p = v;
        if(n < 0) p = p.inv(),n = -n;
        while(n){
            if(n&1) ret *= p;
            p *= p; n >>= 1;
        }
        return ret;
    }
    dynamic_modint inv()const{return dynamic_modint(1)/v;} //素数mod必須.
};
template<int idx> int dynamic_modint<idx>::mod=998244353;
using mint = dynamic_modint<0>;

template<typename T>
vector<vector<T>> multima(vector<vector<T>> &A,vector<vector<T>> &B){
    assert(A.at(0).size() == B.size() && B.size());
    int H = A.size(),W = B.at(0).size();
    vector<vector<T>> ret(H,vector<T>(W));
    for(int i=0; i<H; i++) for(int k=0; k<W; k++){
        for(int l=0; l<A.at(i).size(); l++){
            ret.at(i).at(k) += A.at(i).at(l)*B.at(l).at(k);
        }
    }
    return ret; //計算できる行列のみ.
}
template<typename T>
vector<vector<T>> Powmat(vector<vector<T>> A,vector<vector<T>> B,long long N){ //return A^N*B ABは行列;
    //n*n行列A n*1行列Bと想定 O(n^3*logN).
    assert(A.size() && A.size() == A.at(0).size() && N >= 0);
    while(N){
        if(N&1) B = multima(A,B);
        A = multima(A,A); N >>= 1;
    }
    return B;
}

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

    vector M(2,vector<mint>(2));
    for(auto &h : M) for(auto &w : h) cin >> w.v;
    vector M2(2,vector<mint>{1});
    for(auto &h : M2) for(auto &w : h) cin >> w.v;
    long long N,K; cin >> N >> K; mint::setmod(K);
    for(auto &h : M) for(auto &w : h) w.modu();
    for(auto &h : M2) for(auto &w : h) w.modu();
    auto ans = Powmat(M,M2,N);
    cout << ans.at(0).at(0).v << " " << ans.at(1).at(0).v << endl;
}
0