結果

問題 No.2907 Business Revealing Dora Tiles
ユーザー 👑 獅子座じゃない人獅子座じゃない人
提出日時 2024-06-06 11:28:31
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 871 ms / 3,000 ms
コード長 8,314 bytes
コンパイル時間 11,390 ms
コンパイル使用メモリ 261,320 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-23 11:49:14
合計ジャッジ時間 35,359 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,812 KB
testcase_01 AC 4 ms
6,812 KB
testcase_02 AC 4 ms
6,940 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 4 ms
6,944 KB
testcase_05 AC 4 ms
6,944 KB
testcase_06 AC 4 ms
6,940 KB
testcase_07 AC 39 ms
6,940 KB
testcase_08 AC 4 ms
6,940 KB
testcase_09 AC 4 ms
6,944 KB
testcase_10 AC 4 ms
6,940 KB
testcase_11 AC 36 ms
6,944 KB
testcase_12 AC 4 ms
6,940 KB
testcase_13 AC 4 ms
6,940 KB
testcase_14 AC 4 ms
6,940 KB
testcase_15 AC 4 ms
6,944 KB
testcase_16 AC 4 ms
6,940 KB
testcase_17 AC 4 ms
6,940 KB
testcase_18 AC 9 ms
6,940 KB
testcase_19 AC 4 ms
6,944 KB
testcase_20 AC 4 ms
6,940 KB
testcase_21 AC 37 ms
6,944 KB
testcase_22 AC 6 ms
6,940 KB
testcase_23 AC 63 ms
6,940 KB
testcase_24 AC 417 ms
6,940 KB
testcase_25 AC 420 ms
6,944 KB
testcase_26 AC 421 ms
6,944 KB
testcase_27 AC 417 ms
6,944 KB
testcase_28 AC 420 ms
6,944 KB
testcase_29 AC 420 ms
6,940 KB
testcase_30 AC 420 ms
6,944 KB
testcase_31 AC 417 ms
6,944 KB
testcase_32 AC 419 ms
6,940 KB
testcase_33 AC 417 ms
6,940 KB
testcase_34 AC 419 ms
6,940 KB
testcase_35 AC 419 ms
6,940 KB
testcase_36 AC 421 ms
6,944 KB
testcase_37 AC 419 ms
6,940 KB
testcase_38 AC 420 ms
6,940 KB
testcase_39 AC 417 ms
6,944 KB
testcase_40 AC 417 ms
6,944 KB
testcase_41 AC 420 ms
6,944 KB
testcase_42 AC 418 ms
6,940 KB
testcase_43 AC 431 ms
6,948 KB
testcase_44 AC 871 ms
6,940 KB
testcase_45 AC 819 ms
6,940 KB
testcase_46 AC 763 ms
6,940 KB
testcase_47 AC 322 ms
6,944 KB
testcase_48 AC 797 ms
6,944 KB
testcase_49 AC 710 ms
6,940 KB
testcase_50 AC 371 ms
6,940 KB
testcase_51 AC 43 ms
6,940 KB
testcase_52 AC 419 ms
6,944 KB
testcase_53 AC 435 ms
6,940 KB
testcase_54 AC 62 ms
6,944 KB
testcase_55 AC 440 ms
6,940 KB
testcase_56 AC 436 ms
6,940 KB
testcase_57 AC 436 ms
6,940 KB
testcase_58 AC 325 ms
6,940 KB
testcase_59 AC 472 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

typedef   uint8_t     u8;
typedef   uint16_t    u16;
typedef   uint32_t    u32;
typedef   uint64_t    u64;
typedef __uint128_t   u128;
#define ctz32(a) ((a) ? __builtin_ctz((a)) : (32))
static const u32 fn[16] = {
    0x0001u, 0x0102u, 0x0183u, 0x8041u,
    0x015cu, 0x5f24u, 0xde2cu, 0x957eu,
    0x01f4u, 0xf7d8u, 0x76b0u, 0x5d52u,
    0xa977u, 0x20d1u, 0xc1a4u, 0x271fu};
static inline u32 to_nimber(u32 x) {
    u32 y = 0;
    for (; x; x &= x - 1)
        y ^= fn[ctz32(x)];
    return y;
}
static inline u32 to_double(u32 x) { return x << 1 ^ (x < 32768 ? 0 : 93099U); }
u16 ln[65536];
u16 expo[196605];
u16 *Hexpo = expo + 62133;
u16 *H2expo = expo + 58731;
__attribute__((constructor)) void _construct_nimber_product_() {
    *expo = 1;
    for (int i = 1; i < 65535; ++i)
        expo[i] = to_double(expo[i - 1]);
    for (int i = 1; i < 65535; ++i) {
        expo[i] = to_nimber(expo[i]);
        ln[expo[i]] = i;
    }
    memcpy(expo + 65535, expo, 131070);
    memcpy(expo + 131070, expo, 131070);
}
u16 product_16(u16 A, u16 B) { return A && B ? expo[ln[A] + ln[B]] : 0; }
u16 H_16(u16 A) { return A ? Hexpo[ln[A]] : 0; }
u16 H2_16(u16 A) { return A ? H2expo[ln[A]] : 0; }
u16 Hproduct_16(u16 A, u16 B) { return A && B ? Hexpo[ln[A] + ln[B]] : 0; }
u32 product_32(u32 A, u32 B) {
    u16 a = A & 65535;
    u16 b = B & 65535;
    u16 c = A >> 16;
    u16 d = B >> 16;
    u16 e = product_16(a, b);
    if(c==0 && d==0) {
        return e;
    }
    return (u32)(product_16((u16)(a ^ c), (u16)(b ^ d)) ^ e) << 16 | (Hproduct_16(c, d) ^ e);
}
u32 H_32(u32 A) {
    u16 a = A & 65535;
    u16 b = A >> 16;
    return H_16((u16)(a ^ b)) << 16 | H2_16(b);
}
u64 product(u64 A, u64 B) {
    u32 a = A & UINT_MAX;
    u32 b = B & UINT_MAX;
    u32 c = A >> 32;
    u32 d = B >> 32;
    u32 e = product_32(a, b);
    if(c==0 && d==0) {
        return e;
    }
    return (u64)(product_32(a ^ c, b ^ d) ^ e) << 32 | (H_32(product_32(c, d)) ^ e);
}

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

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(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(ah_bh, 1ULL<<(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;
            }
        }
    }
}

unsigned long long nim_inv(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_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(product(a_h^a_l, a_l)^product(product(a_h, a_h), 1ULL<<(p-1)), p);
    return (product(half_inv, a_h)<<p)^product(half_inv, a_h^a_l);
}

int gauss(int r, int c, vector<vector<unsigned long long>> & m, vector<bool> & is_member) {
    int rank=0;
    for(int i=0;i<c;++i){
        if(!is_member[i]){
            continue;
        }
        int pivot=-1;
        for(int j=rank;j<r;++j){
            if(m[j][i]>0){
                pivot=j;
                break;
            }
        }
        if(pivot==-1){
            continue;
        }
        swap(m[pivot], m[rank]);
        auto inv=nim_inv(m[rank][i]);
        m[rank][i]=1;
        for(int k=0;k<c;++k){
            if(k==i){
                continue;
            }
            m[rank][k]=product(m[rank][k], inv);
        }
        for(int j=rank+1;j<r;++j){
            auto factor=m[j][i];
            m[j][i]=0;
            for(int k=0;k<c;++k){
                if(k==i){
                    continue;
                }
                m[j][k]^=product(m[rank][k], factor);
            }
        }
        ++rank;
        if(rank==r){
            break;
        }
    }
    return rank;
}

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];
        }
    }
    mint ans=0;
    vector is_member(n, true);
    int rank=gauss(t, n, h, is_member);
    vector<int> ranks(1<<n);
    for(int i=0;i<n-rank;++i){
        is_member[i]=false;
    }
    for(int i=n-rank;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;
            }
        }
        if(gauss(rank, n, h, is_member)==rank){
            ranks[s]=rank;
        }
    } while(next_permutation(is_member.begin(), is_member.end()));
    for(int r=rank-1;r>=0;--r){
        for(int i=0;i<n-r;++i){
            is_member[i]=false;
        }
        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){
        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 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