Official

B - Increasing Prefix XOR Editorial by evima


Consider the problem in binary.

If positive integers \(a\) and \(b\) have the same number of digits, the only case when both \(a<c\) and \(b < b\oplus c\) hold is when \(c\) has more digits than each of \(a\) and \(b\), in which case \(c\) and \(b \oplus c\) have the same number of digits.

From this, it can be inductively shown that when \(A\) satisfies the conditions, \(A_{i+1}\) has more digits than \(A_i\). On the other hand, if \(A_{i+1}\) has more digits than \(A_i\) for every \(i\), the conditions are obviously satisfied.

Therefore, when \(M < 2^{60}\), the answer is \(0\) if \(60 < N\). Otherwise, let us precompute the number of \(k\)-digit integers between \(1\) and \(M\) to solve the problem in \(O(N\log^2 M)\) time by dynamic programming, where we set

\(dp[n][k]:\) the number of sequences \((A_1,\ A_2,\ \dots,\ A_n)\) of positive integers with increasing numbers of digits whose \(A_n\) has \(k\) digits.

(Alternatively, consider taking at most one \(k\)-digit integer for each \(k\) to choose \(N\) in total, to solve it in \(O(N \log M)\) time.)

Therefore, the problem can be solved in \(O(\log^3{M})\) (or \(O(\log^2 {M})\)) time.

posted:
last update: