版本比较
题解:P17189 [ICPC 2017 Hong Kong R] Count the Even Integers
正文
增加 186 行删除 26 行
12345678567891011121314910111213141516171819162021221923242526272425282930313233343531363738343940364142434440454647434845495047485152505354555256575455585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215## Solution
### 题意
求杨辉三角前 $n$ 层中偶数的个数。
### 先打表找规律
| n | 偶 | 奇 ||:---|:---:|---:|| 1 | 0 | 2 || 2 | 1 | 2 || 3 | 0 | 4 || 4 | 3 | 2 || 5 | 2 | 4 || 6 | 3 | 4 || 7 | 0 | 8 || 8 | 7 | 2 || n | 这行偶数的个数 | 这行奇数的个数 || :- | :-: | -: || 1 | 0 | 2 || 2 | 1 | 2 || 3 | 0 | 4 || 4 | 3 | 2 || 5 | 2 | 4 || 6 | 3 | 4 || 7 | 0 | 8 || 8 | 7 | 2 |
正难则反,所以把奇数个数也写出来,观察到奇数的个数都是 $ 2 $ 的幂次,继续找规律。直接观察偶数个数没有明显规律,正难则反,所以把奇数个数也写出来,观察到奇数的个数都是 $2$ 的幂次,继续观察找规律。
::::info[规律]{open}第 $ n $ 行奇数的个数是 $ 2^{\operatorname{popcount}(n)} $第 $n$ 行奇数的个数是 $2^{\operatorname{popcount}(n)}$。
:::success[Proof]{open}考虑 Lucas 定理。
对第 $ n $ 行中第 $ m $ 个数 $ \binom{n}{m} $,设:$$ 对第 $n$ 行中第 $m$ 个数 $\binom{n}{m}$,设:
$$\begin{aligned}n&=(n_k n_{k-1} \cdots n_0)_2 \\m&=(m_k m_{k-1} \cdots m_0)_2\end{aligned}$$其中 $ n_i,\, m_i \in \{0,\, 1\} $,允许有前导零。
其中 $n_i,\, m_i \in \{0,\, 1\}$,允许有前导零。
由 Lucas 定理得 $ \binom{n}{m} \equiv \prod_{i=0}^k \binom{n_i}{m_i} \pmod{2} $,故 $ \binom{n}{m} $ 为奇数**当且仅当** $ \binom{n_i}{m_i}=1 $ 恒成立。而 $ \binom{0}{0}=1,\, \binom{0}{1}=0,\, \binom{1}{0}=1,\, \binom{1}{1}=1 $,所以若 $ n_i=0 $,则 $ m_i=0 $,有一种可能;若 $ n_i=1 $,则 $ m_i=0 $ 或 $ m_i=1$,有两种可能。由 Lucas 定理得 $\binom{n}{m} \equiv \prod\limits_{i=0}^k \binom{n_i}{m_i} \pmod{2}$,故 $\binom{n}{m}$ 为奇数**当且仅当** $\binom{n_i}{m_i} \equiv 1 \pmod{2}$ 恒成立。而 $\binom{0}{0}=1,\, \binom{0}{1}=0,\, \binom{1}{0}=1,\, \binom{1}{1}=1$,所以若 $n_i=0$,则 $m_i=0$,有一种可能;若 $n_i=1$,则 $m_i=0$ 或 $m_i=1$,有两种可能。
由乘法原理,符合要求的个数为 $ 2^{\operatorname{popcount}(n)}$。由乘法原理,符合要求的个数为 $2^{\operatorname{popcount}(n)}$。:::::::
于是答案为 $ \frac{(n+1)(n+2)}{2}-1-\sum\limits_{m=1}^{n} 2^{\operatorname{popcount}(m)}$。奇数和偶数一共有 $\sum\limits_{i=1}^n (i+1)=\frac{n(n+3)}{2}$ 个,于是答案为 $\frac{n(n+3)}{2}-\sum\limits_{m=1}^{n} 2^{\operatorname{popcount}(m)}$。
### 计算目标是计算 $S=\sum_{m=1}^{n} 2^{\operatorname{popcount}(m)}$。
显然可以考虑枚举 $ p $,这里使用数位 DP 思想。设 $n$ 的二进制位数为 $l$,对每一个固定的 $ p $,初始化 $ cnt=0 $ 表示 $ m $ 从高位到低位被固定的前缀中 $1$ 的个数,从第 $ l-1 $ 位枚举到第 $ 0 $ 位,若其中 $ n_i=1$,有两种选择:只需要计算 $S=\sum\limits_{m=1}^{n} 2^{\operatorname{popcount}(m)}$。
- 令 $m_i=0$,此时已经可以确定 $m<n$,剩下的 $i$ 位数可以任取,对 $S$ 的贡献为:$2^{cnt} \sum\limits_{x=0}^{2^i-1} 2^{\operatorname{popcount}(x)}=2^{cnt} (1+2)^i=2^{cnt} 3^i$- 令 $m_i=1$,此时 $cnt \longleftarrow cnt+1$,然后考虑下一位。这里使用数位 DP 思想。设 $n$ 的二进制位数为 $l$,初始化 $\mathit{cnt}=0$ 表示 $m$ 从高位到低位被固定的前缀中 $1$ 的个数。从第 $l-1$ 位枚举到第 $0$ 位,若其中 $n_i=1$,有两种选择:
若 $n_i=0$,则必有 $m_i=0$,$cnt$ 不变,直接考虑下一位。- 令 $m_i=0$,此时已经可以确定 $m<n$,剩下的 $i$ 位数可以任取,对 $S$ 的贡献为:$2^\mathit{cnt} \sum\limits_{x=0}^{2^i-1} 2^{\operatorname{popcount}(x)}=2^\mathit{cnt} (1+2)^i=2^\mathit{cnt} 3^i$。- 令 $m_i=1$,此时 $\mathit{cnt} \gets \mathit{cnt}+1$,然后考虑下一位。
于是就可以计算了。若 $n_i=0$,则必有 $m_i=0$,$\mathit{cnt}$ 不变,直接考虑下一位。
## Code~~明天再写~~最后还需要 $S \gets S+2^\mathit{cnt}-1$(此时有 $\mathit{cnt}=\operatorname{popcount}(n)$)表示 $m=n$ 时的贡献并去掉多算的 $m=0$。
::::success[Code]```c++ line-numbers#include <bits/stdc++.h>using namespace std;
// 高精度struct BigInt { bitset<1000> digits; // 从低位到高位,稍微开大一点
BigInt() { }
BigInt(int x) { digits.reset(); int i = 0; while (x) { if (x & 1) digits.set(i); x >>= 1; i++; } }
BigInt(const string &s) { *this = BigInt(0); for (char ch: s) *this = *this * 10 + BigInt(ch - '0'); }
int len() const { for (int i = 999; i >= 0; i--) if (digits[i]) return i + 1; return 0; }
bool at(int i) const { return digits[i]; } bool is0() const { return len() == 0; }
BigInt operator+(const BigInt &other) const { BigInt res; bool carry = 0; for (int i = 0; i < 1000; i++) { int sum = digits[i] + other.digits[i] + carry; res.digits[i] = sum & 1; carry = sum >> 1; } return res; }
BigInt operator+=(const BigInt &other) { return *this = *this + other; }
BigInt operator-(const BigInt &other) const { BigInt res; bool borrow = 0; for (int i = 0; i < 1000; i++) { int diff = digits[i] - other.digits[i] - borrow; if (diff < 0) { diff += 2; borrow = 1; } else { borrow = 0; } res.digits[i] = diff; } return res; }
BigInt operator<<(int x) const { BigInt res; res.digits = digits << x; return res; }
BigInt operator>>(int x) const { BigInt res; res.digits = digits >> x; return res; }
BigInt operator*(const BigInt &other) const { BigInt res; for (int i = 0; i < 1000; i++) if (other.digits[i]) res = res + ((*this) << i); return res; }
BigInt div2() const { return *this >> 1; }
pair<BigInt, int> divmod10() const { // 除以 10 的余数和商,输出要用 BigInt Q; // 商 vector<bool> qbits; int r = 0; //余数 int l = len(); for (int i = l - 1; i >= 0; i--) { r = (r << 1) | digits[i]; qbits.push_back(r / 10); r %= 10; } int idx = 0; for (auto it = qbits.rbegin(); it != qbits.rend(); it++) { if (*it) Q.digits.set(idx); idx++; } return {Q, r}; }
string toString() const { if (is0()) return "0"; BigInt tmp = *this; string s; while (!tmp.is0()) { auto [q,r] = tmp.divmod10(); s.push_back('0' + r); tmp = q; } reverse(s.begin(), s.end()); return s; }} n, pow2[180], pow3[180]; // 10^50 有 167 位
// 预处理void init() { pow2[0] = pow3[0] = BigInt(1); for (int i = 1; i < 180; i++) { pow2[i] = pow2[i - 1] * BigInt(2); pow3[i] = pow3[i - 1] * BigInt(3); }}
int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); init(); string s; while (cin >> s) { n = BigInt(s); BigInt S(0); int l = n.len(), cnt = 0; for (int i = l - 1; i >= 0; i--) { if (n.at(i) == 0) continue; S += pow2[cnt] * pow3[i]; cnt++; } S += pow2[cnt] - BigInt(1); // 补上 m=n,去掉 m=0 cout << ((n * (n + BigInt(3))).div2() - S).toString() << '\n'; } return 0;}
```[AC Record](https://www.luogu.com.cn/record/290584477)::::