結果

問題 No.2907 Business Revealing Dora Tiles
ユーザー 👑 獅子座じゃない人獅子座じゃない人
提出日時 2024-06-06 12:42:50
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,949 ms / 3,000 ms
コード長 9,928 bytes
コンパイル時間 11,287 ms
コンパイル使用メモリ 276,108 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-23 11:57:30
合計ジャッジ時間 32,670 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 3 ms
6,812 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 4 ms
6,944 KB
testcase_04 AC 4 ms
6,944 KB
testcase_05 AC 4 ms
6,944 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 18 ms
6,940 KB
testcase_08 AC 40 ms
6,944 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 128 ms
6,940 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 AC 3 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 3 ms
6,944 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 3 ms
6,944 KB
testcase_18 AC 17 ms
6,940 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 AC 3 ms
6,940 KB
testcase_21 AC 116 ms
6,944 KB
testcase_22 AC 5 ms
6,940 KB
testcase_23 AC 53 ms
6,940 KB
testcase_24 AC 187 ms
6,940 KB
testcase_25 AC 191 ms
6,940 KB
testcase_26 AC 191 ms
6,944 KB
testcase_27 AC 194 ms
6,940 KB
testcase_28 AC 192 ms
6,944 KB
testcase_29 AC 213 ms
6,940 KB
testcase_30 AC 195 ms
6,944 KB
testcase_31 AC 204 ms
6,944 KB
testcase_32 AC 203 ms
6,940 KB
testcase_33 AC 195 ms
6,944 KB
testcase_34 AC 206 ms
6,940 KB
testcase_35 AC 214 ms
6,944 KB
testcase_36 AC 200 ms
6,940 KB
testcase_37 AC 208 ms
6,940 KB
testcase_38 AC 202 ms
6,944 KB
testcase_39 AC 199 ms
6,944 KB
testcase_40 AC 196 ms
6,944 KB
testcase_41 AC 218 ms
6,944 KB
testcase_42 AC 204 ms
6,944 KB
testcase_43 AC 213 ms
6,944 KB
testcase_44 AC 1,152 ms
6,940 KB
testcase_45 AC 1,949 ms
6,944 KB
testcase_46 AC 973 ms
6,944 KB
testcase_47 AC 131 ms
6,944 KB
testcase_48 AC 107 ms
6,940 KB
testcase_49 AC 237 ms
6,940 KB
testcase_50 AC 915 ms
6,940 KB
testcase_51 AC 32 ms
6,940 KB
testcase_52 AC 194 ms
6,940 KB
testcase_53 AC 863 ms
6,940 KB
testcase_54 AC 57 ms
6,940 KB
testcase_55 AC 835 ms
6,940 KB
testcase_56 AC 864 ms
6,940 KB
testcase_57 AC 847 ms
6,944 KB
testcase_58 AC 675 ms
6,940 KB
testcase_59 AC 793 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

vector nim_prod_table(256, vector<unsigned long long>(256));
vector<unsigned long long> nim_inv_table(65536);

void nim_prod_precalc() {
    nim_prod_table[0][0]=0;
    nim_prod_table[0][1]=0;
    nim_prod_table[1][0]=0;
    nim_prod_table[1][1]=1;
    for(int d=0;d<3;++d){
        int p=1<<d;
        for(int a=0;a<(1<<(2*p));++a){
            for(int b=(1<<p);b<(1<<(2*p));++b){
                unsigned long long a_h=a>>p;
                unsigned long long a_l=a-(a_h<<p);
                unsigned long long b_h=b>>p;
                unsigned long long b_l=b-(b_h<<p);
                unsigned long long al_bl=nim_prod_table[a_l][b_l];
                unsigned long long ahl_bhl=nim_prod_table[a_h^a_l][b_h^b_l];
                unsigned long long ah_bh=nim_prod_table[a_h][b_h];
                unsigned long long ah_bh_h=nim_prod_table[ah_bh][1ULL<<(p-1)];
                nim_prod_table[a][b]=nim_prod_table[b][a]=((al_bl^ahl_bhl)<<p)^(al_bl^ah_bh_h);
            }
        }
    }
}

unsigned long long nim_product_power(unsigned long long a, int n, int p=64){
    if(p==64){
        if(a<(1ULL<<8) && n<8){
            p=8;
        } else if(a<(1ULL<<16) && n<16){
            p=16;
        } else if(a<(1ULL<<32) && n<32){
            p=32;
        }
    }
    if(p==8){
        return nim_prod_table[a][1<<n];
    }
    p/=2;
    unsigned long long a_h=a>>p;
    unsigned long long a_l=a-(a_h<<p);
    if(n<p){
        return (nim_product_power(a_h, n, p)<<p)^(nim_product_power(a_l, n, p));
    } else {
        n-=p;
        return (nim_product_power(a_h^a_l, n, p)<<p)^(nim_product_power(nim_product_power(a_h, n, p), p-1, p));
    }
}

unsigned long long nim_square(unsigned long long a, int p=64) {
    if(p==64){
        if(a<(1ULL<<8)){
            p=8;
        } else if(a<(1ULL<<16)){
            p=16;
        } else if(a<(1ULL<<32)){
            p=32;
        }
    }
    if(p==8){
        return nim_prod_table[a][a];
    }
    p/=2;
    unsigned long long a_h=a>>p;
    unsigned long long a_l=a-(a_h<<p);
    unsigned long long h_sq=nim_square(a_h, p);
    unsigned long long l_sq=nim_square(a_l, p);
    return (h_sq<<p)^nim_product_power(h_sq, p-1, p)^l_sq;
}

unsigned long long nim_product(unsigned long long a, unsigned long long b, int p=64) {
    if(a==0 || b==0){
        return 0;
    }
    if(a==1){
        return b;
    }
    if(b==1){
        return a;
    }
    if(p==64){
        if(a<(1ULL<<8) && b<(1ULL<<8)){
            p=8;
        } else if(a<(1ULL<<16) && b<(1ULL<<16)){
            p=16;
        } else if(a<(1ULL<<32) && b<(1ULL<<32)){
            p=32;
        }
    }
    if(p==8){
        return nim_prod_table[a][b];
    }
    p/=2;
    unsigned long long a_h=a>>p;
    unsigned long long a_l=a-(a_h<<p);
    unsigned long long b_h=b>>p;
    unsigned long long b_l=b-(b_h<<p);
    unsigned long long al_bl=nim_product(a_l, b_l, p);
    unsigned long long ahl_bhl=nim_product(a_h^a_l, b_h^b_l, p);
    unsigned long long ah_bh=nim_product(a_h, b_h, p);
    unsigned long long ah_bh_h=nim_product_power(ah_bh, p-1, p);
    return ((al_bl^ahl_bhl)<<p)^(al_bl^ah_bh_h);
}

void nim_inv_precalc() {
    for(int i=0;i<256;++i){
        for(int j=i;j<256;++j){
            if(nim_prod_table[i][j]==1){
                nim_inv_table[i]=j;
                nim_inv_table[j]=i;
                break;
            }
        }
    }
    for(unsigned long long a=(1<<8);a<(1<<16);++a){
        int p=8;
        unsigned long long a_h=a>>p;
        unsigned long long a_l=a-(a_h<<p);
        unsigned long long half_inv=nim_inv_table[nim_product(a_h^a_l, a_l, p)^nim_product_power(nim_square(a_h, p), p-1, p)];
        nim_inv_table[a]=(nim_product(half_inv, a_h, p)<<p)^nim_product(half_inv, a_h^a_l, p);
    }
}

unsigned long long nim_inv(unsigned long long a, int p=64) {
    if(p==64){
        if(a<(1ULL<<16)){
            p=16;
        } else if(a<(1ULL<<32)){
            p=32;
        }
    }
    if(p==16){
        return nim_inv_table[a];
    }
    p/=2;
    unsigned long long a_h=a>>p;
    unsigned long long a_l=a-(a_h<<p);
    unsigned long long half_inv=nim_inv(nim_product(a_h^a_l, a_l, p)^nim_product_power(nim_square(a_h, p), p-1, p), p);
    return (nim_product(half_inv, a_h, p)<<p)^nim_product(half_inv, a_h^a_l, p);
}

tuple<int,vector<int>,vector<int>> gauss_jordan(int r, int c, vector<vector<unsigned long long>> & m) {
    vector<int> members;
    vector<int> not_members;
    int rank=0;
    for(int i=0;i<c;++i){
        int pivot=-1;
        for(int j=rank;j<r;++j){
            if(m[j][i]>0){
                pivot=j;
                break;
            }
        }
        if(pivot==-1){
            not_members.emplace_back(i);
            continue;
        }
        members.emplace_back(i);
        swap(m[pivot], m[rank]);
        auto inv=nim_inv(m[rank][i]);
        m[rank][i]=1;
        for(int k=i+1;k<c;++k){
            m[rank][k]=nim_product(m[rank][k], inv);
        }
        for(int j=0;j<r;++j){
            if(j==rank){
                continue;
            }
            auto factor=m[j][i];
            m[j][i]=0;
            for(int k=i+1;k<c;++k){
                m[j][k]^=nim_product(m[rank][k], factor);
            }
        }
        ++rank;
        if(rank==r){
            for(int j=i+1;j<c;++j){
                not_members.emplace_back(j);
            }
            break;
        }
    }
    return tuple{rank,members,not_members};
}

bool dynamic_matrix_rank(int rank, int a_col, int from_col, int to_col, vector<vector<unsigned long long>> & u, vector<vector<unsigned long long>> & m){
    vector<unsigned long long> v(rank);
    for(int i=0;i<rank;++i){
        v[i]=m[i][from_col]^m[i][to_col];
    }
    vector<unsigned long long> v2(rank);
    for(int i=0;i<rank;++i){
        for(int j=0;j<rank;++j){
            v2[i]^=nim_product(u[i][j], v[j]);
        }
    }
    if(v2[a_col]==1){
        return false;
    }
    vector<unsigned long long> clean(rank);
    unsigned long long inv=nim_inv(v2[a_col]^1);
    for(int i=0;i<rank;++i){
        if(i==a_col){
            clean[i]=inv^1;
        } else {
            clean[i]=nim_product(v2[i], inv);
        }
    }
    auto u_row=u[a_col];
    for(int i=0;i<rank;++i){
        for(int j=0;j<rank;++j){
            u[i][j]^=nim_product(clean[i], u_row[j]);
        }
    }
    return true;
}

int main(void)
{
    nim_prod_precalc();
    nim_inv_precalc();
    int n,t;
    cin >> n >> t;
    vector h(t, vector<unsigned long long>(n));
    for(int i=0;i<t;++i){
        for(int j=0;j<n;++j){
            cin >> h[i][j];
            --h[i][j];
        }
    }
    auto [rank,members,not_members]=gauss_jordan(t, n, h);
    int s=0;
    for(int i: members){
        s+=1<<i;
    }
    vector<bool> seen(1<<n);
    vector<int> ranks(1<<n);
    stack<tuple<int,int,int>> st;
    seen[s]=true;
    ranks[s]=rank;
    vector u(rank, vector<unsigned long long>(rank));
    for(int i=0;i<rank;++i){
        u[i][i]=1;
    }
    st.emplace(tuple{s+(1<<n),n,n});
    st.emplace(tuple{s,n,n});
    while(!st.empty()){
        auto [s,i,j]=st.top();
        st.pop();
        if(s<(1<<n)){
            if(i<n){
                if(dynamic_matrix_rank(rank, i, members[i], not_members[j], u, h)){
                    ranks[s]=rank;
                    swap(members[i], not_members[j]);
                } else {
                    continue;
                }
            }
            for(int i=0;i<(int)members.size();++i){
                for(int j=0;j<(int)not_members.size();++j){
                    int new_s=s-(1<<members[i])+(1<<not_members[j]);
                    if(seen[new_s]){
                        continue;
                    }
                    seen[new_s]=true;
                    st.emplace(tuple{new_s+(1<<n),i,j});
                    st.emplace(tuple{new_s,i,j});
                }
            }
        } else {
            s-=1<<n;
            if(i<n && ranks[s]>0){
                swap(members[i], not_members[j]);
                dynamic_matrix_rank(rank, i, members[i], not_members[j], u, h);
            }
        }
    }
    for(int r=rank-1;r>=0;--r){
        vector<bool> is_member(n);
        for(int i=n-r;i<n;++i){
            is_member[i]=true;
        }
        do {
            int s=0;
            for(int i=0;i<n;++i){
                if(is_member[i]){
                    s+=1<<i;
                }
            }
            for(int i=0;i<n;++i){
                if(is_member[i]){
                    continue;
                }
                if(ranks[s+(1<<i)]>0){
                    ranks[s]=r;
                    break;
                }
            }
        } while(next_permutation(is_member.begin(), is_member.end()));
    }
    for(int r=0;r<n;++r){
        vector<bool> is_member(n);
        for(int i=n-r;i<n;++i){
            is_member[i]=true;
        }
        do {
            int s=0;
            for(int i=0;i<n;++i){
                if(is_member[i]){
                    s+=1<<i;
                }
            }
            for(int i=0;i<n;++i){
                if(is_member[i]){
                    continue;
                }
                ranks[s+(1<<i)]=max(ranks[s], ranks[s+(1<<i)]);
            }
        } while(next_permutation(is_member.begin(), is_member.end()));
    }
    mint ans=0;
    mint p=932051910;
    for(unsigned int s=0;s<(1U<<n);++s){
        int count=popcount(s);
        if(n%2>0){
            if(count%2>0){
                ans+=p.pow(count-ranks[s]);
            } else {
                ans-=p.pow(count-ranks[s]);
            }
        } else {
            if(count%2>0){
                ans-=p.pow(count-ranks[s]);
            } else {
                ans+=p.pow(count-ranks[s]);
            }
        }
    }
    cout << ans.val() << endl;
    return 0;
}
0