#P11007. Construct Sequence with Given Sum and Sum of Squares

    ID: 13057 Type: Default 1000ms 256MiB

Construct Sequence with Given Sum and Sum of Squares

Construct Sequence with Given Sum and Sum of Squares

Given two integers \(x\) and \(y\), construct an integer sequence \(a_1,a_2,\ldots,a_n\) with length \(n\) not exceeding \(10^6\) such that:

\( \sum_{i=1}^{n} a_i = x \) and \( \sum_{i=1}^{n} a_i^2 = y \).

You are guaranteed that a solution exists. It is allowed to have repeated numbers and the numbers may be negative, zero, or positive. The answer you output can be any valid sequence satisfying the conditions.

Hint: A constructive approach is to first decide a nonnegative integer \(k\) (with \(k+2\le 10^6\)) and then find two integers \(a\) and \(b\) such that

[ \begin{aligned} a+b &= S = x-k,\ a^2+b^2 &= T = y-k. \end{aligned} ]

Notice that the quadratic equation for \(a\) (with \(b = S - a\)) is:

[ a^2 - S,a + \frac{S^2-T}{2}=0, \quad \text{with discriminant } D=2T-S^2. ]

If \(D\) is a perfect square and \(S+\sqrt{D}\) is even, then one may take

[ a=\frac{S+\sqrt{D}}{2}, \quad b=\frac{S-\sqrt{D}}{2}. ]

</p>

Finally, append \(k\) copies of \(1\) (since each 1 contributes 1 to the sum and 1 to the square sum) to complete the sequence.

inputFormat

The input consists of two space‐separated integers \(x\) and \(y\) on a single line.

outputFormat

Output a sequence of integers (separated by spaces) whose sum is \(x\) and the sum of squares is \(y\). It is guaranteed that such a sequence with length at most \(10^6\) exists.

sample

5 13
3 2