题解:P17189 [ICPC 2017 Hong Kong R] Count the Even Integers

· · 题解

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

直接观察偶数个数没有明显规律,正难则反,所以把奇数个数也写出来,观察到奇数的个数都是 2 的幂次,继续观察找规律。

::::info[规律]{open} 第 n 行奇数的个数是 2^{\operatorname{popcount}(n)}。

:::success[Proof]{open} 考虑 Lucas 定理。

对第 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\},允许有前导零。

由 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)}。
::::

奇数和偶数一共有 \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)}。

这里使用数位 DP 思想。设 n 的二进制位数为 l,初始化 \mathit{cnt}=0 表示 m 从高位到低位被固定的前缀中 1 的个数。从第 l-1 位枚举到第 0 位,若其中 n_i=1,有两种选择:

若 n_i=0,则必有 m_i=0,\mathit{cnt} 不变,直接考虑下一位。

最后还需要 S \gets S+2^\mathit{cnt}-1(此时有 \mathit{cnt}=\operatorname{popcount}(n))表示 m=n 时的贡献并去掉多算的 m=0。

::::success[Code]

#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