結果

問題 No.3117 Reversible Tile
ユーザー GOTKAKO
提出日時 2025-04-19 10:18:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 3,774 bytes
コンパイル時間 2,865 ms
コンパイル使用メモリ 206,380 KB
実行使用メモリ 19,536 KB
最終ジャッジ日時 2025-04-19 10:18:48
合計ジャッジ時間 10,946 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other TLE * 1 -- * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

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

long long mod = 998244353;
//入力が必ず-mod<a<modの時.
struct mint{
    long long v = 0;
    mint(){} mint(int a){v = a<0?a+mod:a;} mint(long long a){v = a<0?a+mod:a;}
    mint(unsigned long long a){v = a;}
    long long val(){return v;}
    void modu(){v %= mod;}

    mint &operator=(const mint &b) = default;
    mint operator-() const {return mint(0)-(*this);}
    mint operator+(const mint b){return mint(v)+=b;}
    mint operator-(const mint b){return mint(v)-=b;}
    mint operator*(const mint b){return mint(v)*=b;}
    mint operator/(const mint b){return mint(v)/=b;}

    mint operator+=(const mint b){
        v += b.v; if(v >= mod) v -= mod;
        return *this;
    }
    mint operator-=(const mint b){
        v -= b.v; if(v < 0) v += mod; 
        return *this;
    }   
    mint operator*=(const mint b){v = v*b.v%mod; return *this;}
    mint operator/=(mint b){
        if(b == 0) assert(false);
        int left = mod-2;
        while(left){if(left&1) *this *= b; b *= b; left >>= 1;}
        return *this;
    }

    mint operator++(){*this += 1; return *this;}
    mint operator--(){*this -= 1; return *this;}
    mint operator++(int){*this += 1; return *this;}
    mint operator--(int){*this -= 1; return *this;}
    bool operator==(const mint b){return v == b.v;}
    bool operator!=(const mint b){return v != b.v;}
    bool operator>(const mint b){return v > b.v;}
    bool operator>=(const mint b){return v >= b.v;}
    bool operator<(const mint b){return v < b.v;}
    bool operator<=(const mint b){return v <= b.v;}

    mint pow(long long n){
        mint 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;
    }
    mint inv(){return mint(1)/v;}
};

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

    int N,M; cin >> N >> M;
    vector<int> A(N);
    for(auto &a : A) cin >> a;
    {
        auto B = A;
        for(int i=1; i<N; i++) B.at(i) ^= A.at(i-1);
        A = B;
    }

    vector<mint> X(N),Y(N); //Xi<-A0=0の時のA1~An-1の1sがi個となる総数,Yi<-A0=1の時.
    {
        int one = 0;
        for(int i=1; i<N; i++) one += A.at(i);
        if(A.at(0) == 0) X.at(one) = 1;
        else Y.at(one) = 1;
    }
    mint div2 = mint(1)/2;
    while(M--){
        vector<mint> nX(N),nY(N);
        for(int i=0; i<N; i++){
            if(X.at(i) == 0) continue;
            if(i) nY.at(i-1) += X.at(i)*i; //A0とA1~n-1で1->0.
            if(i < N-1) nY.at(i+1) += X.at(i)*(N-1-i); //A0とA1~n-1で0->1.
            nY.at(i) += X.at(i); //A0とAn. (An->Ri=N-1の時にありうる 変化なし).
            
            if(i) nX.at(i-1) += X.at(i)*i; //A1~An-1 An.
            if(i < N-1) nX.at(i+1) += X.at(i)*(N-1-i);
            nX.at(i) += X.at(i)*i*(N-1-i); //0->1 1->0.
            if(i > 1) nX.at(i-2) += X.at(i)*i*(i-1)*div2; //0->1 x2.
            if(i < N-2) nX.at(i+2) += X.at(i)*(N-1-i)*(N-1-i-1)*div2; //1->0 x2.
        }
        for(int i=0; i<N; i++){
            if(Y.at(i) == 0) continue;
            if(i) nX.at(i-1) += Y.at(i)*i; //A0とA1~n-1で1->0.
            if(i < N-1) nX.at(i+1) += Y.at(i)*(N-1-i); //A0とA1~n-1で0->1.
            nX.at(i) += Y.at(i); //A0とAn. (An->Ri=N-1の時にありうる 変化なし).
            
            if(i) nY.at(i-1) += Y.at(i)*i; //A1~An-1 An.
            if(i < N-1) nY.at(i+1) += Y.at(i)*(N-1-i);
            nY.at(i) += Y.at(i)*i*(N-1-i); //0->1 1->0.
            if(i > 1) nY.at(i-2) += Y.at(i)*i*(i-1)*div2; //0->1 x2.
            if(i < N-2) nY.at(i+2) += Y.at(i)*(N-1-i)*(N-1-i-1)*div2; //1->0 x2.
        }
        X = nX,Y = nY;
    }

    cout << X.at(0).v << endl;
}
0