Solution of Codeforces 1957B
Description
Given integers \(n\) and \(k\), find a non-negative sequence \(\{a_n\}\) satisfying the following conditions:
- The summation of \(\{a_n\}\) equals to \(k\), that is,
\[\sum_{i=1}^n a_i = k
\]
- The binary representation of \(a_1\mid a_2\mid \dots\mid a_n\) has the maximum number of \(1\)s, where \(\mid\) represents the bitwise OR operation.
Multiple tests.
Data Range: \(1\leq n\leq 2\cdot 10^5\), \(1\le k\le 10^9\), \(1\le t\le 10^4\).
Solution
For \(n=1\), there is only one way to construct the sequence: to output \(k\) itself.
For \(n>1\), We can easily find out that the answer must be less or equal to \(\log_2 k\), therefore, we only need to find the number of bits in the binary representation of \(k\) (denoted by \(b\)), then a valid sequence is constructed by:
\(2^{b-1}, k - 2^{b-1}\) and \(n-2\) of \(0\)s.