发现目前题解都对 的子集和容斥做法阐述地不够清晰(普遍文字说明过少),就此来补一篇。(顺便炫耀展示优良码风)
枚举状态
所有棋盘的可能是 的,在 下枚举是可以接受的,转化为求每一种状态下的得分最后再进行统计。
实际纸老虎。每一种 组合的情况下只有 。因此统计每种移动的数目即可。
得分统计
对每一个状态枚举 求是 的,这里不多赘述。
考虑到底是什么阻碍了我们的统计。
设集合内的位置为 ,集合外的位置为 。
如果一次移动对状态产生贡献只有 一个限制的话,对于每个 的集合给 的权值,最后整个求子集和即可。
现在有 两个限制,在原来的基础上,所有含有子集 的集合 所有的贡献需要减去 ,容易想到对于 给予 的权值。
回到原题,这里多了 的限制所以 有 的权值。但是这时 又会多减,所以要再给 一个 的权值。
这就是容斥。但这里容斥的集合是“所有 的超集的集合”“所有 的超集的集合”等集合。一开始不能弄明白这一点的话很容易被绕晕。
朴素子集和枚举
枚举所有集合和其子集是 的,具体可见 OI-wiki。
其中 为子集和, 为集合权值。
会挂掉
unsigned N, K;
unsigned f[1 << maxn], fp[1 << maxn], maxf, cnt;
int main()
{
read(N, K);
for (unsigned k = 1, x, y, z; k <= K; k++)
{
read(x, y, z);
x = (1 << (x - 1));
y = (1 << (y - 1));
z = (1 << (z - 1));
++fp[x];
--fp[x ^ y];
--fp[x ^ z];
++fp[x ^ y ^ z];
// 子集和容斥
}
for (unsigned s = 1; s < (1 << N); s++)
{
for (unsigned t = s; t; t = (t - 1) & s)
{
f[s] += fp[t];
}
}
for (unsigned s = 0; s < (1 << N); s++)
{
if (maxf < f[s]) maxf = f[s], cnt = 1;
else if (maxf == f[s]) ++cnt;
}
print(maxf, ' ', cnt);
return 0;
}优化
考虑利用已有的子集和再计算子集和。但是这样你会发现又要套一层容斥,非常麻烦。尝试寻找一些比较取巧的方法。
一个集合的所有子集对于每一个元素而言可以分成包含该元素和不包含这个这个元素的子集,那么就可以 DP 做了。
设 表示只考虑不包含前 个位置情况下 的子集和。
可以滚掉,滚完之后仅需注意枚举 套在枚举 外面, 的枚举顺序没有太大关系。
如果您是大手子的话也可以直接套 FWT 做(但我不会 QAQ)。
代码
目前 C++ 最优解
#include<cstddef>
#include<cstdio>
#include<cctype>
#include<string>
#include<stdexcept>
#ifndef fastIO_defined
#define fastIO_defined 202604L
namespace fastIO
{
using namespace std;
typedef unsigned long long ull;
constexpr size_t IOSIZE = 100000;
unsigned int precision = 6, POW[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
#if defined(ONLINE_JUDGE) && !defined(FastIO)
#define FastIO
#endif
#ifdef FastIO
char obuf[IOSIZE], *p3 = obuf, ibuf[IOSIZE], *p1 = ibuf, *p2 = ibuf;
#define getchar() (((p1 == p2) && (p2 = (p1 = ibuf) + fread(ibuf, 1, IOSIZE, stdin)), p1 == p2) ? (EOF) : (*p1++))
// p2 终止指针 p1 目前要读哪里 一开始从 ibuf 读完就 p1 归零,p2 读新的
#define putchar(x) ((p3 == obuf + IOSIZE) && (fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf), *p3++ = x)
// p3 目前输出已占满的后一位 即输出 [obuf, p3)
#endif
template<typename T>
inline bool read(T &s)
{
s = 0;
bool w = 0;
char ch = getchar();
for(; !(ch > 47 && ch < 58) && ch != EOF; ch = getchar()) if(ch == '-') w ^= 1;
if(ch == EOF) return false;
for(; ch > 47 && ch < 58; ch = getchar()) s = s * 10 + (ch ^ 48);
if(w) s = -s;
return true;
}
inline bool read(unsigned &s)
{
s = 0;
bool w = 0;
char ch = getchar();
for(; !(ch > 47 && ch < 58) && ch != EOF; ch = getchar());
if(ch == EOF) return false;
for(; ch > 47 && ch < 58; ch = getchar()) s = s * 10 + (ch ^ 48);
if(w) s = -s;
return 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 void print(unsigned x)
{
if(x > 9) print(x / 10u);
putchar((x % 10u) ^ 48);
}
inline bool read(char & s)
{
for (s = getchar(); isspace(s) && s != EOF; s = getchar());
return s != EOF;
}
inline void print(char x) {putchar(x);}
inline bool read(char *s)
{
char ch = getchar();
while(ch != EOF && ch < 33) ch = getchar();
if(ch == EOF) return false;
for(; ch != EOF && !(ch < 33); ch = getchar()) *s++ = ch;
*s = '\0';
return true;
}
inline void print(const char *x)
{
while(*x) putchar(*x++);
}
inline bool read(string &s)
{
s.clear();
char ch = getchar();
while(ch != EOF && ch < 33) ch = getchar();
if(ch == EOF) return false;
for(; ch != EOF && !(ch < 33); ch = getchar()) s += ch;
return true;
}
inline void print(const string &s)
{
for(char i : s) putchar(i);
}
inline bool read(bool & b)
{
char ch = getchar();
while(ch < 33 && ch != EOF) ch = getchar();
return ch == EOF ? false : (b = ch ^ 48, true);
}
inline void print(bool b){putchar(b ^ 48);}
typedef long double ld;
inline bool read(ld &s)
{
s = 0;
char ch = getchar();
bool f = 0;
for(; !(ch > 47 && ch < 58) && ch != EOF; ch = getchar()) if(ch == '-') f ^= 1;
if(ch == EOF) return false;
for(; ch > 47 && ch < 58; ch = getchar()) s = s * 10 + (ch ^ 48);
if(ch == '.')
{
ld y = 0.1;
for(ch = getchar(); ch > 47 && ch < 58; y /= 10, ch = getchar()) s += y * (ch ^ 48);
}
if(f) s = -s;
return true;
}
inline bool read(double &s) {ld z; bool b = read(z); s = z; return b;}
inline bool read(float &s) {ld z; bool b = read(z); s = z; return b;}
inline void print(ld x)
{
if(x < 0)
{
putchar('-');
x = -x;
}
ull ux = x;
if(!precision)
{
if(x - ux < 0.5) print(ux);
else print(ux + 1);
return;
} // 特判:不用输出 .
ld y = ((x - ux) * POW[precision]);
ull uy = y;
ull z = (y - uy < 0.5 ? uy : uy + 1);
if(z == POW[precision]) ux++, z = 0;
print(ux), putchar('.');
for(int j = precision - 1; ~j; j--) putchar((z / POW[j] % 10) ^ 48);
}
inline void print(double x) {return print(ld(x));}
inline void print(float x) {return print(ld(x));}
template<typename T, typename... restT>
inline int read(T &s, restT & ...other)
{
return read(s) + read(other...);
}
template<typename T, typename... restT>
inline void print(T x, restT ...other)
{
return print(x), print(other...);
}
template<typename T>
inline T read()
{
T a;
return read(a), a;
}
struct Fast_IO
{
bool flag = 1;
#ifdef FastIO
inline ~Fast_IO(){fwrite(obuf, p3 - obuf, 1, stdout);}
#endif
// 程序终止调用析构
inline void SetPrecision(int x){if (x > 9) throw runtime_error("Precision too high!"); else if (x < 0) throw std::runtime_error("Precision too low!"); else precision = x;} // 浮点数精度设为 x,即精确到小数点后 x 位。
inline operator bool(){return flag;}
} io;
Fast_IO & operator >>(Fast_IO & io, char * b){return io.flag &= read(b), io;}
template<typename T> Fast_IO & operator >>(Fast_IO & io, T & b){return io.flag &= read(b), io;}
template<typename T> Fast_IO & operator << (Fast_IO & io, T b){return print(b), io;}
}
#endif
#include<bits/stdc++.h>
#define cout io
#define cin io
#define endl '\n'
using namespace std;
using fastIO::io;
using fastIO::read;
using fastIO::print;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int maxn = 20, maxk = 2e5;
unsigned N, K;
unsigned f[1 << maxn], maxf, cnt;
int main()
{
read(N, K);
for (unsigned k = 1, x, y, z; k <= K; k++)
{
read(x, y, z);
x = (1 << (x - 1));
y = (1 << (y - 1));
z = (1 << (z - 1));
++f[x];
--f[x ^ y];
--f[x ^ z];
++f[x ^ y ^ z];
// 子集和容斥
}
for (unsigned i = 1; i < (1 << N); i <<= 1)
for (unsigned S = (i + 1) | i; S < (1 << N); S = (S + 1) | i)
f[S] += f[S ^ i]; // 高维前缀和:依次考虑每一位的影响
// 一次考虑多位的影响可能导致重复,因此循环顺序要注意
for (unsigned s = 0; s < (1 << N); s++)
{
if (maxf < f[s]) maxf = f[s], cnt = 1;
else if (maxf == f[s]) ++cnt;
}
print(maxf, ' ', cnt);
return 0;
}创作声明
本文遵循 CC BY-NC-SA 4.0 协议。
保证本文未使用任何 GenAI 工具辅助创作。
转载例如下:
Python [原文](https://www.luogu.com.cn/article/q80lhbz7)作者为 [clx201022](https://www.luogu.com.cn/user/552688),转载人保证会遵循 [CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh-hans) 协议:以适当的方式署名,不以盈利为目的进行转载,并以相同方式共享此文。