Official

G - Increasing Popcount Editorial by en_translator


First, consider a graph with \((R-L+1)\) vertices, vertex \(L\) through \(R\). For \(L\le i < j \le R\), add a directed edge from vertex \(i\) to vertex \(j\) if and only if \(\operatorname{popcount}(i) < \operatorname{popcount}(j)\). This graph is a DAG (Directed Acyclic Graph), and if there is an edge from vertex \(i\) to \(j\) and \(j\) to \(k\), then there is also an edge from \(i\) to \(k\) (i.e. it is transitive). Thus, by Dilworth theorem the size of a minimum path cover equals the size of a largest antichain.

Since the size of a minimum path cover equals the answer to the original problem, so the answer to the original problem equals the answer to the following:

Find the maximum length of an integer sequence \(x=(x_1,x_2,\ldots,x_n)\) such that:

  • \(L\le x_1 < x_2 < \dots < x_n \le R\),
  • \(\operatorname{popcount}(x_1) \geq \operatorname{popcount}(x_2) \geq \dots \geq \operatorname{popcount}(x_n)\).

From now on, we will consider the answer to this problem.


First, consider the following lemma:

For non-negative integers \(m,l\), and \(r\), the maximum length of an integer sequence \(x=(x_1,x_2,\ldots,x_n)\) satisfying the conditions below equals \(\displaystyle \max_{l\le k\le r}\binom mk\). The maximum length can be achieved by arranging integers with popcount \(k\), for the \(l\le k\le r\) maximizing \(\displaystyle \binom mk\).

  • \(0\le x_1 < x_2 < \dots < x_n < 2^m\)
  • \(r\geq \operatorname{popcount}(x_1) \geq \operatorname{popcount}(x_2) \geq \dots \geq \operatorname{popcount}(x_n)\geq l\)

Proof:

Let \(S\) be the set of integers between \(0\) and \(2^m-1\), with popcount between \(l\) and \(r\).

Define an inclusion relation between elements of \(S\) by the inclusion relation of standing bits. Then if the integer sequence \(x\) satisfies the conditions, \(x\) interpreted as a set forms an antichain. Therefore, by LYM (Lubell–Yamamoto–Meshalkin) inequality, \(\displaystyle \sum_{i=1}^n\binom{m}{\operatorname{popcount}(x_i)}^{-1}\le 1\).

Since \(l\le \operatorname{popcount}(x_i)\le r\), we have \(\displaystyle 1\geq\sum_{i=1}^n\binom{m}{\operatorname{popcount}(x_i)}^{-1}\geq \frac nM\) where \(\displaystyle M=\max_{l\le k\le r}\binom mk\), so it follows that \(n\le M\). By arranging the values with popcount \(k\) for the \(l\le k\le r\) that maximizes \(\displaystyle \binom mk\), we can achieve \(n=M\).

For any integers \(L\) and \(R\), there exists an integer sequence \(T=(T_0,T_1,\ldots,T_N)\) such that:

  • \(L=T_0 < T_1<\dots < T_N=R+1\);
  • for all \(i=0,1,\ldots,N-1\), \(T_{i+1}-T_i\) can be represented as \(2^{c_i}\) for some non-negative integer \(c_i\), and moreover \(T_i\) is a multiple of \(2^{c_i}\);
  • \(N=O(\log R)\).

By chunking the segment \([L,R]\) this way, within each segment \([T_i,T_{i+1})\), it is optimal to take all the values with the same popcount, as demonstrated by the lemma above. Therefore, the answer can be obtained via Dynamic Programming, where \(d[i][j]\) is defined as the maximum length of \(x\) when the last element has a popcount of at least \(j\), considering the elements strictly less than \(T_j\). The complexity is \(O(\log^2R)\).

For LYM inequality, refer to the following article (Japanese): Sperner の定理 - AtCoderInfo (Sperner’s Theorem - AtCoderInfo)

Sample code (Python3)

import sys

input = sys.stdin.readline
C = [[0] * 62 for _ in range(62)]
for i in range(62):
    C[i][0] = C[i][i] = 1
    for j in range(1, i):
        C[i][j] = C[i - 1][j - 1] + C[i - 1][j]
for _ in range(int(input())):
    l, r = map(int, input().split())
    r += 1
    d = [0] * 62
    while l < r:
        x = min(1 << ((r - l).bit_length() - 1), l & -l)
        c = x.bit_length() - 1
        p = l.bit_count()
        for i in range(c + 1):
            d[p + i] += C[c][i]
        for i in range(60, -1, -1):
            d[i] = max(d[i], d[i + 1])
        l += x
    print(d[0])

posted:
last update: