#P8468. Sequence Construction with XOR Zero
Sequence Construction with XOR Zero
Sequence Construction with XOR Zero
Given two integers s and m, you are to construct a sequence a with an arbitrary length n (subject to 1 ≤ n ≤ m
) satisfying the following conditions:
- Each element ai must satisfy
1 ≤ ai ≤ s
. - The sum of all elements is exactly s, i.e.
\(a_1+a_2+\cdots+a_n=s\). - The bitwise XOR of all elements is
0
, i.e.
\(S(a)=a_1\oplus a_2\oplus\cdots\oplus a_n=0\). (Here, \(\oplus\) denotes the bitwise XOR operation.)
If it is possible to construct such a sequence, output any valid sequence; otherwise, output -1. Note that if n=1 then the only possibility is a1=s but then \(S(a)=s\), which is 0 only when s=0. Thus, for any s ≥ 1 a valid sequence exists if and only if m ≥ 2 and s is even. A simple valid construction in this case is to take n = 2 and let a1 = a2 = s/2.
inputFormat
The input consists of two space-separated integers s
and m
on one line.
Constraints:
- 1 ≤ s ≤ 109
- 1 ≤ m ≤ 109
outputFormat
If a valid sequence exists, first output an integer n
(the length of the sequence), followed by a line with n
space-separated integers representing the sequence. If no valid sequence exists, output a single integer -1
.
sample
4 2
2
2 2
</p>