Binary Banter: Counting Combinatorial Bits
Binary Banter: Counting Combinatorial Bits
Problem Description
Given a positive integer $n$, compute the value of the following expression modulo $998244353$: $\sum\limits_{i=0}^n \sum\limits_{j=0}^i \left( C_i^j \bmod 2 \right)$
Where:
- The binomial coefficient $C_i^j$ is defined as:
$
C_i^j = \begin{cases}
\frac{i!}{j!(i - j)!}, & \text{if } 0 \leq j \leq i \\
0, & \text{if } j > i
\end{cases}
$ - The factorial $m!$ is defined as the product of all positive integers from $1$ to $m$:
$
m! = \begin{cases}
1, & \text{if } m = 0 \\
1 \times 2 \times 3 \times \dots \times m, & \text{if } m \geq 1
\end{cases}
$ - The modulo operation $x \bmod y$ returns the remainder when $x$ is divided by $y$. For example:
$
5 \bmod 2 = 1, \quad 8 \bmod 3 = 2, \quad 7 \bmod 7 = 0
$
You need to output the value of the expression modulo $998244353$.
输入描述
There are multiple test cases. The first line of the input contains a single integer $t$ $(1 \leq t \leq 3 \times 10^5)$, denoting the number of test cases. For each test case: The first and only line contains one integer $n$ $(1 \leq n \leq 10^{18})$.
输出描述
For each test case, output one line containing one integer, denoting the value of the expression modulo $998244353$.
示例1
输入
3
3
5
1145141919810
输出
9
15
516911908
备注:
For the first sample test case, $n = 3$, so you need to compute the following expression: $\sum\limits_{i=0}^{3} \sum\limits_{j=0}^{i} \left( C_i^j \bmod 2 \right)$
Let’s analyze each row of binomial coefficients modulo $2$:
$i = 0$: $C_0^0 = 1 \Rightarrow 1 \bmod 2 = 1$
$i = 1$: $C_1^0 = 1, C_1^1 = 1 \Rightarrow 1 + 1 = 2$
$i = 2$: $C_2^0 = 1, C_2^1 = 2, C_2^2 = 1 \Rightarrow 1 + 0 + 1 = 2$
$i = 3$: $C_3^0 = 1, C_3^1 = 3, C_3^2 = 3, C_3^3 = 1 \Rightarrow 1 + 1 + 1 + 1 = 4$
So the total sum is $1 + 2 + 2 + 4 = 9$. And you need to output $9 \bmod 998244353 = 9$.
For the second sample test case, $n = 5$, so you need to compute the following expression: $\sum\limits_{i=0}^{5} \sum\limits_{j=0}^{i} \left( C_i^j \bmod 2 \right)$
Similarly:
$i = 0$: $1$
$i = 1$: $1 \ 1 \Rightarrow \text{sum} = 3$
$i = 2$: $1 \ 0 \ 1 \Rightarrow \text{sum} = 5$
$i = 3$: $1 \ 1 \ 1 \ 1 \Rightarrow \text{sum} = 9$
$i = 4$: $1 \ 0 \ 0 \ 0 \ 1 \Rightarrow \text{sum} = 11$
$i = 5$: $1 \ 1 \ 0 \ 0 \ 1 \ 1 \Rightarrow \text{sum} = 15$
Final result: $ (1 + 2 + 2 + 4 + 2 + 4) \bmod 998244353 = 15$
For the third sample test case, I have a brilliant explanation, but the space here is too small to write.
解题思路
当 $i$ 固定时,有结论 $\sum\limits_{j=0}^{i}{\left( C_{i}^{j} \bmod 2 \right)} = 2^{c(i)}$,其中 $c(i)$ 表示 $i$ 在二进制下 $1$ 的位数。可以通过打表 01 杨辉三角然后对每一行找规律猜出这个结论,但很明显我不可能有这种注意力。
实际上该等式可以证明。首先有 Lucas 定理 $C_{a}^{b} \equiv C_{a \bmod p}^{b \bmod p} \cdot C_{\left\lfloor{a/p}\right\rfloor}^{\left\lfloor{b/p}\right\rfloor} \pmod{p}$,在本题中 $p=2$。假设 $a$ 在二进制下等于 $a_{k-1}a_{k-2} \ldots a_{0}$,$b$ 在二进制下等于 $b_{k-1}b_{k-2} \ldots b_{0}$,根据 Lucas 定理 $C_{a}^{b} \equiv \prod\limits_{i=0}^{k-1}{C_{a_{i}}^{b_{i}}} \pmod{2}$。由于 $C_{0}^{0} = C_{1}^{0} = C_{1}^{1} = 1$,$C_{0}^{1} = 0$,因此 $C_{a}^{b} \equiv 1 \pmod{2}$ 等价于二进制下每一位都有 $a_i \geq b_i \, (0 \leq i \leq k-1)$。此时考虑 $a$ 固定的情况下,有多少个 $b \, (0 \leq b \leq a)$ 满足 $C_{a}^{b} \equiv 1 \pmod{2}$?由于要满足 $a_i \geq b_i$,因此对于 $a$ 中为 $0$ 的位,$b$ 对应位也应该为 $0$;对于 $a$ 中为 $1$ 的位,$b$ 对应位可以是 $0$ 或 $1$。所以根据乘法原理,满足该条件的 $b$ 的数量就是 $2^{c(a)}$。即 $\sum\limits_{b=0}^{a}{\left( C_{a}^{b} \bmod 2 \right)} = 2^{c(a)}$。
因此实际上要求的是 $\sum\limits_{i=0}^{n}{\sum\limits_{j=0}^{i}{\left( C_{i}^{j} \bmod 2 \right)}} = \sum\limits_{i=0}^{n}{2^{c(i)}}$。求解该式也是有技巧的,还挺难想到的。
我们考虑简单的情况,假设 $n = 2^{k}-1$,此时 $i$ 在二进制下有 $k$ 位,每一位可以是 $0$ 或 $1$,即有 $k$ 个自由的二进制位。容易知道在 $[0, 2^{k}-1]$ 中,二进制下恰好有 $i$ 个 $1$ 的数的个数是 $C_{k}^{i}$,因此 $\sum\limits_{i=0}^{n}{2^{c(i)}} = \sum\limits_{i=0}^{k}{C_{k}^{i} \cdot 2^i} = \sum\limits_{i=0}^{k}{C_{k}^{i} \cdot 2^i \cdot 1^{k-i}} = (2 + 1)^k = 3^k$。
我们参考 $[0, 2^{k}-1]$ 把 $[0, n]$ 拆分成若个子区间,假设 $n$ 在二进制下为 $1$ 的位从高到低依次为 $x_0,x_1,\ldots,x_{c(n)-1}$,则可以将 $[0,n]$ 拆分成以下若干个区间:
$$\left[0, 2^{x_0}-1\right], \, \left[2^{x_0}, 2^{x_0} + 2^{x_1}-1\right], \, \ldots, \, \left[\sum_{j=0}^{i-1}{2^{x_j}}, \left(\sum_{j=0}^{i}{2^{x_j}}\right)-1\right], \, \ldots, \, \left[\sum_{j=0}^{c(n)-2}{2^{x_j}}, \left(\sum_{j=0}^{c(n)-1}{2^{x_j}}\right)-1\right], \, \left[\sum_{j=0}^{c(n)-1}{2^{x_j}}, \sum_{j=0}^{c(n)-1}{2^{x_j}}\right] = [n,n]$$
其中区间 $\left[0, 2^{x_0}-1\right]$ 的结果为 $\sum\limits_{j=0}^{x_0}{C_{x_0}^{j} \cdot 2^j} = 3^{x_0}$,区间 $\left[2^{x_0}, 2^{x_0} + 2^{x_1}-1\right]$ 的结果为 $\sum\limits_{j=0}^{x_1}{C_{x_1}^{j} \cdot 2^{j+1}} = 2\sum\limits_{j=0}^{x_1}{C_{x_1}^{j} \cdot 2^{j}} = 2 \cdot3^{x_1}$,以此类推,区间 $\left[\sum_{j=0}^{i-1}{2^{x_j}}, \left(\sum_{j=0}^{i}{2^{x_j}}\right)-1\right]$ 的结果为 $\sum\limits_{j=0}^{x_i}{C_{x_i}^{j} \cdot 2^{j+i}} = 2^i\sum\limits_{j=0}^{x_i}{C_{x_i}^{j} \cdot 2^{j}} = 2^i \cdot3^{x_i}$。
最后的答案即为 $\sum\limits_{j=0}^{c(n)-1}{2^{j} \cdot 3^{x_j}} + 2^{c(n)}$。
AC 代码如下,时间复杂度为 $O(\log{n})$:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 998244353;
void solve() {
LL n;
cin >> n;
array<int, 60> p;
p[0] = 1;
for (int i = 1; i <= 59; i++) {
p[i] = 3ll * p[i - 1] % mod;
}
int ret = (1ll << __builtin_popcountll(n)) % mod;
for (int i = 59, q = 1; i >= 0; i--) {
if (n >> i & 1) {
ret = (ret + 1ll * p[i] * q) % mod;
q = 2 * q % mod;
}
}
cout << ret << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
参考资料
【题解】福建师范大学第二十二届程序设计竞赛(同步赛):https://www.nowcoder.com/discuss/753019277335158784
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/18895782

浙公网安备 33010602011771号