D. Exam in MAC
D. Exam in MAC
The Master's Assistance Center has announced an entrance exam, which consists of the following.
The candidate is given a set $s$ of size $n$ and some strange integer $c$. For this set, it is needed to calculate the number of pairs of integers $(x, y)$ such that $0 \leq x \leq y \leq c$, $x + y$ is not contained in the set $s$, and also $y - x$ is not contained in the set $s$.
Your friend wants to enter the Center. Help him pass the exam!
Input
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n$ and $c$ ($1 \leq n \leq 3 \cdot 10^5$, $1 \leq c \leq 10^9$) — the size of the set and the strange integer.
The second line of each test case contains $n$ integers $s_1, s_2, \ldots, s_{n}$ ($0 \leq s_1 < s_2 < \ldots < s_{n} \leq c$) — the elements of the set $s$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
Output
For each test case, output a single integer — the number of suitable pairs of integers.
Example
input
8
3 3
1 2 3
1 179
57
4 6
0 3 5 6
1 1
1
5 10
0 2 4 8 10
5 10
1 3 5 7 9
4 10
2 4 6 7
3 1000000000
228 1337 998244353
output
3
16139
10
2
33
36
35
499999998999122959
Note
In the first test case, the following pairs are suitable: $(0, 0)$, $(2, 2)$, $(3, 3)$.
In the third test case, the following pairs are suitable: $(0, 1)$, $(0, 2)$, $(0, 4)$, $(1, 3)$, $(2, 6)$, $(3, 4)$, $(3, 5)$, $(4, 5)$, $(4, 6)$, $(5, 6)$.
解题思路
正着不好算,考虑容斥原理反过来做。用总的数对,减去满足 $x+y \in S$ 的数对数量,减去 $y-x \in S$ 的数对数量,最后加上同时满足 $x+y \in S$ 且 $y-x \in S$ 的数对数量,就是要求的答案。
其中总的数对就是 $(c+1) + c + (c-1) + \ldots + 1 = \frac{(c+1)(c+2)}{2}$。
满足 $x+y \in S$ 的数对数量。由于 $0 \leq x \leq y$ 且 $x + y = s_i$,则有 $0 \leq x \leq s_i - x$,解得 $0 \leq x \leq \lfloor \frac{s_i}{2} \rfloor$。由于 $x$ 与 $y$ 一一对应,因此只用考虑 $x$ 的数量,即 $\lfloor \frac{s_i}{2} \rfloor + 1$。由于每个 $s_i$ 都不同,因此总的数对数量就是 $\sum\limits_{i=1}^{n}{\lfloor \frac{s_i}{2} \rfloor + 1}$。
满足 $y-x \in S$ 的数对数量。同理解得 $s_i \leq y \leq c$,总的数对数量就是 $\sum\limits_{i=1}^{n}{c - s_i + 1}$。
同时满足 $x+y \in S$ 且 $y-x \in S$ 的数对数量。意味着同一个数对 $(x,y)$ 被减了 $2$ 次,即满足 $\begin{cases} x + y = s_i \\ y - x = s_j \end{cases}$,$i$ 可以等于 $j$。有 $x = \frac{s_i - s_j}{2}$,$y = \frac{s_i+s_j}{2}$,意味着 $s_i$ 和 $s_j$ 的奇偶性要相同。假设有 $c_0$ 个偶数的 $s_i$,$c_1$ 奇数的 $s_i$,那么数对数量就是 $C_{c_0}^{2} + C_{c_1}^{2}$。
AC 代码如下,时间复杂度为 $O(n)$:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 3e5 + 10;
int a[N];
void solve() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
}
LL ret = (m + 1ll) * (m + 2ll) / 2;
int c0 = 0, c1 = 0;
for (int i = 0; i < n; i++) {
ret -= a[i] / 2 + 1 + m - a[i] + 1;
if (a[i] & 1) c1++;
else c0++;
}
ret += c0 * (c0 + 1ll) / 2 + c1 * (c1 + 1ll) / 2;
printf("%lld\n", ret);
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
参考资料
Codeforces Round #932 (Div. 2) Editorial:https://codeforces.com/blog/entry/126662
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/18056900