Numbers with K Set Bits — Solution & Editorial
Binary digit DP: walk the bits of the bound; at each set bit, fixing it to 0 frees the lower bits, contributing C(remaining bits, needed ones). Combine over both bounds.
Complexity: O(log² N)
Watch out: Count in binary, not decimal; and remember to add 1 for the bound itself if it has exactly K bits.