格雷码?CCF🐎*4
声明:部分卡壳的地方得到了 AI 的提示,文章经过了 deepseek 的润色和笔误修改。
下面给出一个更有启发意义的做法:
首先前面的内容和完善程序一样 , 表示第 个选手第 个题目的答案, 表示第 题答案。
那么每个人的得分为:
那么即转化为 最大化的问题。
考虑和原题一样记
考虑到
得到
记
对于 两个状态,很容易发现只需要保留一个,不妨让 。此时这一对状态能够取得的最优值为 ,于是只需要枚举前 个 。
注意到:每道题对 的贡献只依赖于它与状态 的 Hamming 距离。(此处获得了 AI 的点播,注意力有些牛了)。
对于第 道题,将绝对值内部整体乘上 ,得到:
将前 个 编成二进制状态 ,其中第 位为 表示 (即 取负号)。相应地,对第 道题构造一个状态 ,其第 位为 当且仅当
于是 的第 位刻画了 的符号:若该位为 ,则 ;否则为 。
不妨令:即 与 的 Hamming 距离。
由定义, 恰好是满足“ 与 异号”的位置个数:这些位置在 与 上取值不同,对应 ;其余 个位置同号,对应 。
因此:
也就是说,一道题对状态 的贡献只与 和 的 Hamming 距离有关,与具体是哪一道题无关。
于是可以按 值把题目归类。记
那么
这正是异或卷积的标准形式,可以用 FWT 一次性求出所有 。
的计算要简单得多:
令:
那么对于状态 (仍固定 ),
所有子集的 可以使用 lowbit 递推在 内求出。
因此最终只需要枚举所有 ,取
若最优状态满足 ,说明在 与 这一对中,我们真正要选的是 ,即应该将所有 整体取反。
确定最终的 后,对于每一道题重新计算
由于表达式对每个 独立取最优,故当 时标准答案取 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;
}