P6599 题解

· · 题解

Description

给定 n,l,求构造一个长度为 l 的序列 a_i,a_i \in [1,n],满足:

\sum\limits_{i=1}^l\sum\limits_{j=1}^{i-1}a_i \oplus a_j

最大,求这个最大值。

Solution

按位考虑,下文的 位 定义为,第 i 位的权为 2^i。

设第 i 位有 k 个 1,那么对答案的贡献为 k \times (l-k) \times 2^i。

根据和一定,差小积大可得 $k=\lfloor\frac l 2\rfloor$ 时答案最大。 因此答案即为(设 $\overline n=\lfloor\log _2 n\rfloor $): $$\begin{aligned}\textsf{ans}&=\sum\limits_{i=0}^{\overline n}\left\lfloor\frac l 2\right \rfloor \times \left(l-\left\lfloor\frac l 2\right\rfloor\right) \times2^i\\&=\left\lfloor\frac l 2\right\rfloor \times \left(l-\left\lfloor\frac l 2\right\rfloor\right) \times \sum\limits_{i=0}^{\overline n}2^i\\&=\left\lfloor\frac l 2\right\rfloor \times \left(l-\left\lfloor\frac l 2\right\rfloor\right) \times (2^{\overline n+1}-1)\end{aligned}$$ 然后我们就用 $\mathcal O(t \log \log n)$ 的优秀解法解决了这道题。 总结:本题是一道早期 EZEC 良心题,再次谴责现在 EZEC 出的大毒瘤题。 #### Code ```cpp #include <bits/stdc++.h> #define Mod 1000000007 using namespace std; int main () { int t; scanf("%d", &t); while (t--) { long long n, l; scanf("%lld%lld", &n, &l); if (n == 1) { puts("0"); continue; } n = log2(n); long long ans = (((l / 2) * (l - (l / 2))) % Mod) * (((1ll << (n + 1)) - 1)); printf("%lld\n", ans % Mod); } return 0; } ```