题解:P17482 标准答案
格雷码?CCF🐎*4
声明:部分卡壳的地方得到了 AI 的提示,文章经过了 deepseek 的润色和笔误修改。
下面给出一个更有启发意义的做法:
首先前面的内容和完善程序一样
那么每个人的得分为:
那么即转化为
考虑和原题一样记
考虑到
得到
记
对于
注意到:每道题对
对于第
将前
于是
不妨令:
由定义,
因此:
也就是说,一道题对状态
于是可以按
那么
这正是异或卷积的标准形式,可以用 FWT 一次性求出所有
那么对于状态
所有子集的
因此最终只需要枚举所有
若最优状态满足
确定最终的
由于表达式对每个 A(即 B(即
时间复杂度为
下面是让deepseek进行了卡常的代码,比没卡常的累计快了40ms:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
namespace fast_IO {
#define IOSIZE 100000
char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
inline void print(char x) { putchar(x); }
inline void print(char *x) { while (*x) putchar(*x++); }
inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
inline void print(bool b) { putchar(b+48); }
template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
const int MAXN = 1 << 17;
int cnt[MAXN], kk[MAXN], s1[MAXN], LL[MAXN];
ll hh[MAXN];
inline void fwt_i(int *a, int n) {
for (int i = 1; i < n; i <<= 1) {
int step = i << 1;
for (int j = 0; j < n; j += step) {
int *p = a + j, *q = p + i;
for (int k = 0; k < i; ++k) {
int x = p[k], y = q[k];
p[k] = x + y;
q[k] = x - y;
}
}
}
}
inline void fwt_l(ll *a, int n) {
for (int i = 1; i < n; i <<= 1) {
int step = i << 1;
for (int j = 0; j < n; j += step) {
ll *p = a + j, *q = p + i;
for (int k = 0; k < i; ++k) {
ll x = p[k], y = q[k];
p[k] = x + y;
q[k] = x - y;
}
}
}
}
int main() {
int n, m;
cin >> n >> m;
int x[20];
for (int i = 0; i < n; ++i) cin >> x[i];
static char a[20][305];
for (int i = 0; i < n; ++i) cin >> a[i];
int N = 1 << (n - 1);
for (int j = 0; j < m; ++j) {
int p = 0;
char An = a[n - 1][j];
for (int i = 0; i < n - 1; ++i) {
if (a[i][j] != An) p |= (1 << i);
}
++cnt[p];
}
for (int i = 0; i < N; ++i) {
int pc = __builtin_popcount((unsigned)i);
int v = n - 2 * pc;
kk[i] = v < 0 ? -v : v;
}
fwt_i(cnt, N);
fwt_i(kk, N);
for (int i = 0; i < N; ++i) hh[i] = (ll)cnt[i] * kk[i];
fwt_l(hh, N);
int C = 0;
int c[20];
for (int i = 0; i < n; ++i) {
c[i] = m - 2 * x[i];
C += c[i];
}
s1[0] = 0;
LL[0] = C;
for (int i = 1; i < N; ++i) {
int lb = i & (-i);
s1[i] = s1[i ^ lb] + c[__builtin_ctz((unsigned)lb)];
LL[i] = C - 2 * s1[i];
}
ll best = LLONG_MIN;
int bstid = 0;
for (int i = 0; i < N; ++i) {
int av = LL[i] < 0 ? -LL[i] : LL[i];
ll val = hh[i] + (ll)N * av;
if (val > best) {
best = val;
bstid = i;
}
}
int s[20];
for (int i = 0; i < n - 1; ++i) {
s[i] = (bstid & (1 << i)) ? -1 : 1;
}
s[n - 1] = 1;
if (LL[bstid] < 0) {
for (int i = 0; i < n; ++i) s[i] = -s[i];
}
string ans(m, 'A');
for (int j = 0; j < m; ++j) {
int v = 0;
for (int i = 0; i < n; ++i) {
v += s[i] * (a[i][j] == 'A' ? 1 : -1);
}
ans[j] = (v >= 0) ? 'A' : 'B';
}
cout << ans;
}