提供一种接近正解的小常数 做法以及没有实现过的 正解复杂度做法,注意后者只是口胡,如有错误,敬请指出。
大常数 做法
我们先考虑如何计算的 。
首先我们发现,,于是我们有结论 ,事实上,从它的定义中,也可以得到这个结论。
于是我们自然想要贪心的使得 。
这个等价于去除掉最大值后,剩下的数能通过 运算得到 ,或者最大值的这一位等于 。
下文有些地方没有考虑最大值的这一位等于 的情况,可以自行推导。
性质 1
假设第一个变为 的数是 ,那么一定存在 满足 被 去除最大值即 后的可重集合包含,且 。
也就是说,我们进行的计算一定都是先不断地对 进行操作,而不会先对另外两个数操作后,再对 操作。我们来证明这个结论。
性质 1 证明
我们假设先对 和 两个数进行了 的操作,再让 ,不难发现,这个操作不会增加新的二进制位。
所以,我们选择 一定是不劣的。
类比可以证明,无论你进行多少次,直接让 全部 上这些数是不劣的。
然后我们对选定 的每个二进制位来考虑,假设它位权位 的位上不为 ,如果这个位在 中除去最大值还出现过,那么直接进行一次 运算可以消掉,否则无法消除,但是我们。
于是我们发现,如果我们处理出来除去最大值之后,每个仅在 的所有数中出现一次的二进制位的位权和 ,对每个不等于最大值的 进行 ,则当存在 ,(其中 是区间最大值)时,答案为最大值。
性质 2
长度大于 的序列,答案为最大值。
性质 2 证明
我们发现,当区间长度大于 时,去除最大值后,还存在 个数(注意这里 是向上取整的,下文同)。
而仅在 中出现一次的位最多覆盖 个数,还剩下一个数不包含,所以存在 ,自然也存在 。
接下来我们考虑处理长度小于等于 的区间。
注意此时不一定是拿最大值操作最优,因此需要枚举 ,计算 ,然后暴力枚举 ,即可做到复杂度 。
对于大于的部分,显而易见的,单调栈处理后,可以做到 或者 ,后者更容易实现,且更容易想到,反正都能通过。
我们发现可能很难通过,是因为常数太大了。但是添加剪枝后可以通过,不过我认为可以卡掉,所以继续进行了优化。
小常数 做法
性质 3
去除最大值后,求出的 ,和直接判断在 中出现次数等于 求出的 (同样是位权和),造成贡献相同。
性质 3 证明
考虑分讨,设 表示这一位出现次数。
当 ,如果有一个出现在 上,那么同原先 一样都会造成一次贡献,否则没有变化,因为对 进行了与运算后,这一位会消失。
当 ,本来就不会产生影响。
于是我们省略了计算 的部分,大大降低了常数,已经可以通过。提供的代码添加了剪枝,注释掉后仍然可以通过。不过添加后获得了本题目前最优解第五。
这个做法的代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using lll = __int128;
using ulll = unsigned __int128;
class fastio
{
static const int MAX = 1 << 15; char ibuf[MAX], *p1 = ibuf, *p2 = ibuf, obuf[MAX], *p3 = obuf, sta[50]; bool file_end = false;
char get() { return p1 == p2 && (p2 = (p1 = ibuf) + fread(ibuf, 1, MAX, stdin), p1 == p2) ? (file_end = true), char(EOF) : *p1++; }
void put(const char x) { p3 - obuf < MAX ? *p3++ = x : (fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf, *p3++ = x); }
explicit operator bool() { return !file_end; }
size_t flush() { size_t f = fwrite(obuf, p3 - obuf, 1, stdout); p3 = obuf; *p3 = 0; return f; }
public :
fastio &operator >> (char &t) { for (t = get(); !isgraph(t); t = get()); return *this; }
fastio &operator >> (char *t) { char c; for (c = get(); !isgraph(c); c = get()); for (; isgraph(c); c = get()) * t = c, t++; *t = 0; return *this; }
fastio &operator >> (string &t) { t.clear(); char c; for (c = get(); !isgraph(c); c = get()); for (; isgraph(c); c = get()) t += c; return *this; }
fastio &operator << (const char t) { put(t); return *this; }
fastio &operator << (const char *t) { for (; *t; ++t) put(*t); return *this; }
fastio &operator << (const string &t) { for (const char it : t) put(it); return *this; }
fastio &read(char *t) { char c; for (c = get(); !isgraph(c); c = get()); for (; isgraph(c); c = get()) * t = c, t++; *t = 0; return *this; }
template <typename any1, typename any2> pair<any1, any2> tpval() { return pair<any1, any2>(tpval<any1>(), tpval<any2>()); }
template <typename any> typename enable_if <is_same<any, char>::value, any>::type tpval() { char t; for (t = get(); !isgraph(t); t = get()); return t; }
template <typename any> typename enable_if <is_same<any, string>::value, any>::type tpval() { string t; char c; for (c = get(); !isgraph(c); c = get()); for (; isgraph(c); c = get()) t += c; return t; }
template <typename any> typename enable_if <(is_signed<any>::value && is_integral<any>::value && !is_same<any, char>::value) || is_same<any, __int128_t>::value, any >::type tpval() { any t = 0; bool y = 0; char c = get(); for (; !isdigit(c); c = get()) if (c == 45) y = true; for (; isdigit(c); c = get()) t = t * 10 + c - 48; if (y == 1) t = -t; return t; }
template <typename any> typename enable_if <(is_signed<any>::value && std::is_integral<any>::value && !is_same<any, char>::value) || is_same<any, __int128_t>::value, fastio >::type &operator >> (any &t) { t = 0; bool y = 0; char c = get(); for (; !isdigit(c); c = get()) if (c == 45) y = true; for (; isdigit(c); c = get())t = t * 10 + c - 48; if (y == 1) t = -t; return *this; }
template <typename any> typename enable_if <(is_unsigned<any>::value && std::is_integral<any>::value && !is_same<any, char>::value) || is_same<any, __uint128_t>::value, any >::type tpval() { any t = 0; char c = get(); for (; !isdigit(c); c = get()); for (; isdigit(c); c = get()) t = t * 10 + c - 48; return t; }
template <typename any> typename enable_if <(is_unsigned<any>::value && is_integral<any>::value && !is_same<any, char>::value) || is_same<any, __uint128_t>::value, fastio >::type &operator >> (any &t) { t = 0; char c = get(); for (; !isdigit(c); c = get()); for (; isdigit(c); c = get()) t = t * 10 + c - 48; return *this; }
template <typename any1, typename any2> fastio &operator >> (pair<any1, any2> &t) { return *this >> t.first >> t.second; }
template <typename any> typename enable_if <(is_signed<any>::value && is_integral<any>::value && !is_same<any, char>::value) || is_same<any, __int128_t>::value, fastio >::type &operator << (any t) { if (!t) { put(48); return *this; } int len = 0; if (t < 0) t = -t, put(45); while (t) sta[len++] = char(t % 10 + 48), t /= 10; while (len--) put(sta[len]); return *this; }
template <typename any> typename enable_if <(is_unsigned<any>::value && is_integral<any>::value && !is_same<any, char>::value) || is_same<any, __uint128_t>::value, fastio >::type &operator << (any t) { if (!t) { put(48); return *this; } int len = 0; while (t) sta[len++] = char(t % 10 + 48), t /= 10; while (len--) put(sta[len]); return *this; }
template <typename any1, typename any2> fastio &operator << (const pair<any1, any2> &t) { return *this << t.first << ' ' << t.second; } template <typename any> fastio &write(const any &t) { return *this << t; } template <typename any, typename...args> fastio &write(const any &t1, const args &...t2) { return (*this << t1).write(t2...); }
template <typename any> fastio &read(any &t) { return *this >> t; }
template <typename any, typename...args> fastio & read(any &t1, args &...t2) { return (*this >> t1).read(t2...); }
~fastio() { fwrite(obuf, p3 - obuf, 1, stdout); }
} fio;
const int N = 2e5 + 5, M = 25, B = 27, mod = 998244353, inf = 1 << 25;
int n, a[N], s[N][M], l[N], r[N];
ll res;
void add(int x, int cnt[]) {
int pos = 0;
while (x) {
cnt[pos] += x & 1;
x >>= 1, ++pos;
}
}
void sub(int x, int cnt[]) {
int pos = 0;
while (x) {
cnt[pos] -= x & 1;
x >>= 1, ++pos;
}
}
int main() {
fio >> n;
for (int i = 1; i <= n; ++i) {
fio >> a[i];
for (int j = 0; j < M; ++j)
s[i][j] = s[i - 1][j];
add(a[i], s[i]);
}
for (int l = 1; l <= n; ++l)
for (int r = l; r - l + 1 <= B && r <= n; ++r) {
if (l == r) {
res += a[l];
continue;
}
int y = 0;
for (int i = 0; i < M; ++i)
if (s[r][i] - s[l - 1][i] <= 2)
y |= (1 << i);
int mx = 0;
for (int k = l; k <= r; ++k) {
if (a[k] <= mx) continue;
int mn, x = y & a[k];
mn = x;
for (int i = l; i <= r; ++i)
if (i != k) {
mn = min(mn, a[i] & x);
if (!mn) break;
}
mx = max(mx, a[k] - mn);
}
res += mx;
}
res %= mod;
stack<int> stk;
a[0] = inf;
stk.push(0);
for (int i = 1; i <= n; ++i) {
while (!stk.empty() && a[stk.top()] < a[i]) stk.pop();
l[i] = stk.top() + 1;
stk.push(i);
}
while (!stk.empty()) stk.pop();
a[n + 1] = inf;
stk.push(n + 1);
for (int i = n; i; --i) {
while (!stk.empty() && a[stk.top()] <= a[i]) stk.pop();
r[i] = stk.top() - 1;
stk.push(i);
}
for (int i = 1; i <= n; ++i) {
ll cnt = (ll)(i - l[i] + 1) * (r[i] - i + 1);
for (int j = 0; j <= B && j <= i - l[i]; ++j)
for (int k = 0; k <= B - 1 - j && k <= r[i] - i; ++k)
--cnt;
res = (res + cnt * a[i]) % mod;
}
fio << res;
return 0;
}做法
我们发现,复杂度瓶颈仍然在计算长度小于等于 的区间。
首先我们有性质 3,所以有点连边的感觉了,于是考虑图论建模,这里用到了一个重要的思想,当两个元素互相有影响的时候,我们考虑转化为一条边,视情况取无向或者有向边。
我们考虑对与一个二进制位,如过它仅在 和 中出现过,那么连边 ,这里先不去重。
于是我们枚举 ,枚举相连的所有边,看看是否覆盖了所有点,如果没有,答案就是 ,否则对这些点进行上述运算即可。这个的复杂度是 ,于是复杂度为 。
连边的具体实现就是枚举 的时候,继承 的边,然后删掉多余的边,添加新的边,均摊下来复杂度是对的。
这个做法带有较大的常数,所以并没有比小常数 快多少。