Powerless
chAsing_meteor · · 题解
提供一种好想好写的
观察所求式子,可以分为三个要素分析:
于是考虑拆位,我们枚举每一位
接下来考虑
胡乱实现也能做到
:::info[code]
#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;
#define int ll
const int N = 2e5 + 5, P = 29, M = N * (P + 1), mod = 998244353;
int n, m, a[N], V, cnt[N][P + 1][2], pw[P + 1], ans;
int get(int r, int l) {
if (r < l) return 0;
return ((1 << (r - l + 1)) - 1) << l;
}
int calc(int i, int x, int j, int y) {
int res = (m & get(P, i + 1)) >> 2;
if (((m >> i) & 1) != x) {
if (((m >> i) & 1) > x)
res += 1 << (i - 1);
return res;
}
res += (m & get(i - 1, j + 1)) >> 1;
if (((m >> j) & 1) != y) {
if (((m >> j) & 1) > y)
res += 1 << j;
return res;
}
res += m & get(j - 1, 0);
return res;
}
struct trie {
int tot, son[M][2], l[M], r[M];
#define ls son[p][0]
#define rs son[p][1]
void init() {
tot = 1;
memset(l, 63, sizeof l);
}
void insert(int x) {
int p = 1;
for (int i = P; i >= 0; --i) {
int w = (x >> i) & 1;
if (!son[p][w]) son[p][w] = ++tot;
p = son[p][w];
}
}
void dfs(int p, int x, int i) {
if (i < 0) {
l[p] = r[p] = lower_bound(a + 1, a + V + 1, x) - a;
--l[p];
return;
}
if (ls) dfs(ls, x, i - 1);
if (rs) dfs(rs, x | (1 << i), i - 1);
l[p] = min(l[ls], l[rs]);
r[p] = max(r[ls], r[rs]);
}
void solve(int p, int i) {
if (!p) return;
if (ls && rs) {
for (int j = i - 1; j >= 0; --j) {
int c[2][2] = { { cnt[r[ls]][j][0] - cnt[l[ls]][j][0],
cnt[r[ls]][j][1] - cnt[l[ls]][j][1]},
{ cnt[r[rs]][j][0] - cnt[l[rs]][j][0],
cnt[r[rs]][j][1] - cnt[l[rs]][j][1]} };
for (int x = 0; x <= 1; ++x)
for (int y = 0; y <= 1; ++y)
(ans += 2 * c[0][y] * c[1][y ^ 1] % mod * calc(i, x, j, x ^ y ^ 1) % mod * pw[j]) %= mod;
}
}
solve(ls, i - 1), solve(rs, i - 1);
}
} tr;
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
tr.init();
pw[0] = 1;
for (int i = 1; i <= P; ++i) pw[i] = (pw[i - 1] * 2) % mod;
cin >> n >> m;
++m;
map<int, int> mp;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
++mp[a[i]];
tr.insert(a[i]);
}
sort(a + 1, a + n + 1);
V = unique(a + 1, a + n + 1) - a - 1;
for (int i = 1; i <= V; ++i) {
int ct = mp[a[i]];
for (int j = 0; j <= P; ++j) {
cnt[i][j][0] = cnt[i - 1][j][0];
cnt[i][j][1] = cnt[i - 1][j][1];
cnt[i][j][(a[i] >> j) & 1] += ct;
}
}
for (int j = 0; j <= P; ++j)
for (int x = 0; x <= 1; ++x)
(ans += cnt[V][j][x] * cnt[V][j][x] % mod * calc(P + 1, 0, j, x ^ 1) % mod * pw[j]) %= mod;
tr.dfs(1, 0, P);
tr.solve(1, P);
cout << ans;
return 0;
}
:::