w=512 的(非完善)bitset

· · 科技·工程

例题 时间限制 3s。

本题有 O(nm2^m) 的正常做法(std),也有 O(n^2m) 的暴力做法(实际指令集优化为 O(n^2 \lceil \frac{m|\Sigma|}{\omega} \rceil)=O(n^2) 可过)。

下为原 bitset 做法(由 @Eric998 提出,感谢其提供的代码)。

使用 __m512i 作为 bitset 的底层存储器,即可做到 \omega=512。复杂度为 O(\frac{n^2m}{\omega})。最大点可以压缩至 ~65ms(使用 std::bitset 为 180ms)。

由此可见,本题出题人的解法实为理论最慢解。令 m=19 即可卡掉容斥而放过所有其他做法。令 m=200 就只能使用本文的做法。

注意这样做之后,bitset 仅有基础操作(与、或、异或)操作可以做到 \omega=512,其他操作(如 count,左右移等)仍为 \omega=64。故其实际作用并不大。

#pragma GCC optimize("2,3,Ofast,inline,unroll-loops")
#include <stdio.h>
#include <stdint.h>
#include <ctype.h>

struct IO {
#define MAXSIZE 100000
#define isdigit(x) (x >= '0' && x <= '9')
  char buf[MAXSIZE], *p1, *p2;
  IO() : p1(buf), p2(buf) {}

  char gc() {
    if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
    return p1 == p2 ? ' ' : *p1++;
  }

  void read(int &x) {
    bool neg = false;
    x = 0;
    char ch = gc();
    for (; !isdigit(ch); ch = gc())
      if (ch == '-') neg = true;
    if (neg)
      for (; isdigit(ch); ch = gc()) x = x * 10 + ('0' - ch);
    else
      for (; isdigit(ch); ch = gc()) x = x * 10 + (ch - '0');
  }

  void read(char *s) {
    char ch = gc();
    for (; isspace(ch); ch = gc());
    for (; !isspace(ch); ch = gc()) *s++ = ch;
    *s = 0;
  }

  void read(char &c) { for (c = gc(); isspace(c); c = gc()); }
} io;

#ifdef ONLINE_JUDGE
#pragma GCC target("avx2,avx512f,popcnt,avx512vpopcntdq")
#include <immintrin.h>
#define SIMD(_FUNC) _FUNC
#else
#include <simde/x86/avx2.h>
#include <simde/x86/avx512.h>
#define SIMD(_FUNC) simde##_FUNC
#endif

const int N = 50176, M = 98;

int lim;

int popcount_m512i(SIMD(__m512i) vec) {
    uint64_t parts[8];
    SIMD(_mm512_storeu_si512)((SIMD(__m512i)*)parts, SIMD(_mm512_popcnt_epi64)(vec));
    return parts[0] + parts[1] + parts[2] + parts[3] + parts[4] + parts[5] + parts[6] + parts[7];
}

SIMD(__m512i) masks[512] __attribute__((aligned(32)));

void init() {
    for (int k = 0; k < 512; k++) {
        union {
            SIMD(__m512i) vec;
            uint64_t val[8];
        } cvt;
        cvt.vec = SIMD(_mm512_setzero_si512)();
        cvt.val[k >> 6] |= (1ULL << (k & 63));
        masks[k] = cvt.vec;
    }
}

void set_m512i(SIMD(__m512i)& v, int k) {
    v = SIMD(_mm512_or_si512)(v, masks[k]);
}

struct recall { // 我常常追忆过去。
    SIMD(__m512i) a[M] __attribute__((aligned(32)));
    void set(int x) {
      set_m512i(a[x >> 9], x & 511);
    }
    int count() {
      int res = 0;
      for (int i = 0; i < lim; i++)
        res += popcount_m512i(a[i]);
      return res;
    }
};

void COPY(recall& a, const recall& b) {
    for (int i = 0; i < lim; i++)
        a.a[i] = b.a[i];
}
void AND(recall& a, const recall& b) {
    for (int i = 0; i < lim; i++)
        a.a[i] = SIMD(_mm512_and_si512)(a.a[i], b.a[i]);
}
void OR(recall& a, const recall& b) {
    for (int i = 0; i < lim; i++)
        a.a[i] = SIMD(_mm512_or_si512)(a.a[i], b.a[i]);
}

recall cur[6][30], msk, d;
char s[N][8];
int n, m, res;

main() {
    freopen("match.in", "r", stdin);
    freopen("match.out", "w", stdout);
    io.read(n);
    io.read(m);
    lim = (n + 511) >> 9;
    init();
    for (int i = 0; i < n; i++)
        io.read(s[i]);
    for (int c = 0; c < m; c++) {
        for (int i = 0; i < n; i++) {
            if (s[i][c] == '?') cur[c][26].set(i);
            else cur[c][s[i][c] - 'a'].set(i);
        }
        for (int i = 0; i < 26; i++)
            OR(cur[c][i], cur[c][26]);
    }
    for (int i = 0; i < n; i++) {
        COPY(d, msk);
        for (int c = 0; c < m; c++)
            if (s[i][c] != '?')
                AND(d, cur[c][s[i][c] - 'a']);
        res += d.count();
        msk.set(i);
    }
    printf("%d", res);
    fprintf(stderr, "%.2fms\n", 1000.0 * clock() / CLOCKS_PER_SEC);
}