一些前置内容
Min-Max 容斥
\[\max_{x \in S}x = \sum_{T \subseteq S}(-1)^{T - 1}\min_{x \in T}x
\]
\[\min_{x \in S}x = \sum_{T \subseteq S}(-1)^{T - 1}\max_{x \in T}x
\]
\[E\left(\max_{x \in S}x\right) = \sum_{T \subseteq S}(-1)^{T - 1}E\left(\min_{x \in T}x\right)
\]
\[E\left(\min_{x \in S}x\right) = \sum_{T \subseteq S}(-1)^{T - 1}E\left(\max_{x \in T}x\right)
\]
由题得:
\[F(a, b) = \sum_{i = 0} ^ {b}\dbinom{b}{i}\dbinom{n - i}{a}
\]
根据公式 \(\displaystyle \dbinom{n}{m} = \dbinom{n - 1}{m - 1} + \dbinom{n - 1}{m}\),有:
\[F(a, b) = \sum_{i = 0} ^ {b}\dbinom{b - 1}{i - 1}\dbinom{n - i}{a} + \sum_{i = 0} ^ {b}\dbinom{b - 1}{i}\dbinom{n - i}{a}
\]
\[F(a, b) = \sum_{i = 0} ^ {b}\dbinom{b - 1}{i - 1}\dbinom{n - i}{a} + F(a, b - 1)
\]
好的我们发现这个式子烦人的一点就是 \(\displaystyle \sum_{i = 0} ^ {b}\dbinom{b - 1}{i - 1}\dbinom{n - i}{a}\),考虑将其也写成若干个 \(F\) 乘系数的形式。
\[\displaystyle \sum_{i = 0} ^ {b}\dbinom{b - 1}{i - 1}\dbinom{n - i}{a} = \sum_{i = -1} ^ {b - 1}\dbinom{b - 1}{i}\dbinom{n - i - 1}{a}
\]
\[= \sum_{i = -1} ^ {b - 1}\dbinom{b - 1}{i}\dbinom{n - i}{a} - \sum_{i = -1} ^ {b - 1}\dbinom{b - 1}{i}\dbinom{n - i - 1}{a - 1}
\]
\[= F(a, b - 1) - \sum_{i = -1} ^ {b - 1}\dbinom{b - 1}{i}\dbinom{n - i - 1}{a - 1}
\]
现在的目标转化为化简 \(\displaystyle \sum_{i = -1} ^ {b - 1}\dbinom{b - 1}{i}\dbinom{n - i - 1}{a - 1}\),继续:
\[\sum_{i = -1} ^ {b - 1}\dbinom{b - 1}{i}\dbinom{n - i - 1}{a - 1} = \sum_{i = -1} ^ {b - 1}\dbinom{b}{i + 1}\dbinom{n - i - 1}{a - 1} - \sum_{i = -1} ^ {b - 1}\dbinom{b - 1}{i + 1}\dbinom{n - i - 1}{a - 1}
\]
\[= \sum_{i = 0} ^ {b}\dbinom{b}{i}\dbinom{n - i}{a - 1} - \sum_{i = 0} ^ {b}\dbinom{b - 1}{i}\dbinom{n - i}{a - 1}
\]
因为 \(\dbinom{b - 1}{b} = 0\),所以有:
\[= \sum_{i = 0} ^ {b}\dbinom{b}{i}\dbinom{n - i}{a - 1} - \sum_{i = 0} ^ {b - 1}\dbinom{b - 1}{i}\dbinom{n - i}{a - 1}
\]
\[= F(a - 1, b) - F(a - 1, b - 1)
\]
整理一下,有:
\[\displaystyle \sum_{i = -1} ^ {b - 1}\dbinom{b - 1}{i}\dbinom{n - i - 1}{a - 1} = F(a - 1, b) - F(a - 1, b - 1)
\]
\[\displaystyle \sum_{i = 0} ^ {b}\dbinom{b - 1}{i - 1}\dbinom{n - i}{a} = F(a, b - 1) - (F(a - 1, b) - F(a - 1, b - 1))
\]
\[\displaystyle \sum_{i = 0} ^ {b}\dbinom{b - 1}{i - 1}\dbinom{n - i}{a} = F(a, b - 1) - F(a - 1, b) + F(a - 1, b - 1)
\]
\[F(a, b) = F(a, b - 1) - F(a - 1, b) + F(a - 1, b - 1) + F(a, b - 1)
\]
\[F(a, b) = 2F(a, b - 1) - F(a - 1, b) + F(a - 1, b - 1)
\]
好的!我们现在得到了递推式,可以 \(O(m ^ 2)\) 计算,但是考虑一些边界情况:
\[F(0, b) = 2^b
\]
\[F(a, 0) = \dbinom{n}{a}
\]
因为 \(n \leq 10^9\),所以需要使用以下公式计算:
\[\dbinom{n}{a} = \dfrac{\displaystyle \prod_{i = n - a + 1} ^ {n} i}{a!}
\]
这里设 \(U = \{0, 1, \dots, n - 1\}\)。
设 \(t_i(0 \leq i < n)\) 表示 \(2 ^ i\) 这个二进制位在第 \(t_i\) 次操作时 第一次从 \(\symbfit{0 \to 1}\)。
那么答案为 \(\displaystyle \max_{i = 0} ^ {n - 1} t_i\),为了方便转化为 Min-Max 容斥的形式,考虑将其写成 \(\displaystyle \max_{i \in U} t_i\)。
因为 \(\displaystyle \max_{i \in S} t_i\) 很难求解,所以考虑求 \(\displaystyle \min_{i \in S} t_i\):
我们考虑求选出的数集与 \(S\) 的交集不为空的概率:这样很难计算,考虑正难则反:求出选出的数集与 \(S\) 的交集为空的概率,也就是选出的数集是 \(U \backslash S\) 的子集的概率,高维前缀和可以 \(O(2 ^ n \times n)\) 求解。
然后考虑套用 Min-Max 容斥的公式:
\[\max_{x \in S}x = \sum_{T \subseteq S}(-1)^{T - 1}\min_{x \in T}x
\]
然后将 \(\displaystyle (-1)^{T - 1}\min_{x \in T}x\) 扔到一个数组中,然后跑一遍高维前缀和即可。