版本比较
题解:P17189 [ICPC 2017 Hong Kong R] Count the Even Integers
正文
增加 97 行删除 89 行
1234567859101112132 行未改动16171819162021221923242526277 行未改动35363738353940414243444145464748454950475152495053545552565754585956576061626364656667656668697071727369747571727677787980817677787982838485868187888384899091928788899091939495969798939495991001011029798103104105106101102103107108109110105106111112113108109110111112113114115116117118119120115121122117123124125126121127128129124130131132133134135130136137138133134139140141136137138142143144145146141147148143149150145146151152153154155149150151152153154156157158159160161162156157163164165166167161168169170171172166173174175169170171176177178179173180181182176183184185186180181182183187188189190191192193194188195196197191192198199200194195196197201202203204205206200201207208209210211205212213214215## Solution
### 题意
求杨辉三角前 $n$ 层中偶数的个数。
### 先打表找规律
| n | 偶 | 奇 || n | 这行偶数的个数 | 这行奇数的个数 || :- | :-: | -: || 1 | 0 | 2 || 2 | 1 | 2 || 3 | 0 | 4 |2 行未改动| 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 定理。
7 行未改动$$
其中 $n_i,\, m_i \in \{0,\, 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}=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)}$。:::::::
于是答案为 $\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\limits_{m=1}^{n} 2^{\operatorname{popcount}(m)}$。只需要计算 $S=\sum\limits_{m=1}^{n} 2^{\operatorname{popcount}(m)}$。
这里使用数位 DP 思想。设 $n$ 的二进制位数为 $l$,初始化 $cnt=0$ 表示 $m$ 从高位到低位被固定的前缀中 $1$ 的个数,从第 $l-1$ 位枚举到第 $0$ 位,若其中 $n_i=1$,有两种选择:这里使用数位 DP 思想。设 $n$ 的二进制位数为 $l$,初始化 $\mathit{cnt}=0$ 表示 $m$ 从高位到低位被固定的前缀中 $1$ 的个数。从第 $l-1$ 位枚举到第 $0$ 位,若其中 $n_i=1$,有两种选择:
- 令 $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$,然后考虑下一位。- 令 $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$,$cnt$ 不变,直接考虑下一位。若 $n_i=0$,则必有 $m_i=0$,$\mathit{cnt}$ 不变,直接考虑下一位。
最后还需要 $S \longleftarrow S+2^{cnt}-1$(此时有 $cnt=\operatorname{popcount}(n)$)表示 $m=n$ 时的贡献并去掉多算的 $m=0$。最后还需要 $S \gets S+2^\mathit{cnt}-1$(此时有 $\mathit{cnt}=\operatorname{popcount}(n)$)表示 $m=n$ 时的贡献并去掉多算的 $m=0$。
## Code
::::success[Code]```c++ line-numbers#include <bits/stdc++.h>using namespace std;
// 高精度struct BigInt { bitset<1000> digits; // 从低位到高位,稍微开大一点 BigInt() {}
BigInt() { }
BigInt(int x) { digits.reset(); int i=0; int i = 0; while (x) { if (x&1) digits.set(i); x>>=1; 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'); 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; 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 {
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; 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; } carry = sum >> 1; } return res; } BigInt operator+=(const BigInt& other) { return *this=*this+other;
BigInt operator+=(const BigInt &other) { return *this = *this + other; } BigInt operator-(const BigInt& other) const {
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; 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; borrow = 0; } res.digits[i]=diff; res.digits[i] = diff; } return res; }
BigInt operator<<(int x) const { BigInt res; res.digits=digits<<x; res.digits = digits << x; return res; }
BigInt operator>>(int x) const { BigInt res; res.digits=digits>>x; res.digits = digits >> x; return res; } BigInt operator*(const BigInt& other) const {
BigInt operator*(const BigInt &other) const { BigInt res; for (int i=0;i<1000;i++) if (other.digits[i]) res=res+((*this)<<i); for (int i = 0; i < 1000; i++) if (other.digits[i]) res = res + ((*this) << i); return res; }
BigInt div2() const { return *this>>1; return *this >> 1; } pair<BigInt,int> divmod10() const { // 除以 10 的余数和商,输出要用
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 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++) { int idx = 0; for (auto it = qbits.rbegin(); it != qbits.rend(); it++) { if (*it) Q.digits.set(idx); idx++; } return {Q,r}; return {Q, r}; }
string toString() const { if (is0()) return "0"; BigInt tmp=*this; BigInt tmp = *this; string s; while (!tmp.is0()) { auto [q,r]=tmp.divmod10(); s.push_back('0'+r); tmp=q; auto [q,r] = tmp.divmod10(); s.push_back('0' + r); tmp = q; } reverse(s.begin(),s.end()); reverse(s.begin(), s.end()); return s; }}n,pow2[180],pow3[180]; // 10^50 有 167 位} 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); 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); ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); init(); string s; while (cin>>s) { n=BigInt(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]; 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'; 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)::::