Beautiful Array (Codeforces 1715B)
Tags: Greedy, constructive algorithms

Stanley defines the beauty of an array $$$a$$$ of length $$$n$$$, which contains non-negative integers, as follows: $$$$$$\sum\limits_{i = 1}^{n} \left \lfloor \frac{a_{i}}{k} \right \rfloor,$$$$$$ which means that we divide each element by $$$k$$$, round it down, and sum up the resulting values.Stanley told Sam the integer $$$k$$$ and asked him to find an array $$$a$$$ of $$$n$$$ non-negative integers, such that the beauty is equal to $$$b$$$ and the sum of elements is equal to $$$s$$$. Help Sam — find any of the arrays satisfying the conditions above.

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows.The first line of each test case contains integers $$$n$$$, $$$k$$$, $$$b$$$, $$$s$$$ ($$$1 \leq n \leq 10^{5}$$$, $$$1 \leq k \leq 10^{9}$$$, $$$0 \leq b \leq 10^{9}$$$, $$$0 \leq s \leq 10^{18}$$$).It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output: For each test case print $$$-1$$$ if such array $$$a$$$ does not exist. Otherwise print $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_{i} \leq 10^{18}$$$) — the answer.

Note: In the first, the second, the fifth and the sixth test cases of the example it is possible to show that such array does not exist.In the third testcase of the example $$$a = [0, 0, 19]$$$. The sum of elements in it is equal to 19, the beauty of it is equal to $$$\left ( \left \lfloor \frac{0}{6} \right \rfloor + \left \lfloor \frac{0}{6} \right \rfloor + \left \lfloor \frac{19}{6} \right \rfloor \right ) = (0 + 0 + 3) = 3$$$.In the fourth testcase of the example $$$a = [0, 3, 3, 3, 29]$$$. The sum of elements in it is equal to $$$38$$$, the beauty of it is equal to $$$(0 + 0 + 0 + 0 + 7) = 7$$$.

Sample Inputs:
8
1 6 3 100
3 6 3 12
3 6 3 19
5 4 7 38
5 4 7 80
99978 1000000000 100000000 1000000000000000000
1 1 0 0
4 1000000000 1000000000 1000000000000000000

Sample Outputs:
-1
-1
0 0 19
0 3 3 3 29
-1
-1
0
0 0 0 1000000000000000000

Solution (Python):
t = int(input())
for z in range(t):
    (n, k, b, s) = tuple([int(x) for x in input().split()])
    a = k * b
    c = a + n * (k - 1)
    if s > a and s <= c and (k != 1):
        x = (s - a - 1) // (k - 1)
        print((n - x - 1) * '0 ' + x * (str(k - 1) + ' ') + str(s - x * (k - 1)))
    elif k == 1 and b == s or s == a:
        print((n - 1) * '0 ' + str(s))
    else:
        print('-1')

Solution (Python):
for _ in range(int(input())):
    (n, k, b, s) = map(int, input().split())
    nums = 0
    if k * b <= s <= k * b + n * (k - 1):
        while s - k >= b * k:
            s -= k - 1
            nums += 1
        print('0 ' * (n - nums - 1) + f'{k - 1} ' * nums + str(s))
    else:
        print(-1)

Solution (Python):
def solve():
    (n, k, b, s) = [int(x) for x in input().split()]
    if k * b > s:
        print(-1)
        return
    ans = [0] * n
    ans[0] = k * b
    remain = s - k * b
    for i in range(n):
        if remain > k - 1:
            ans[i] += k - 1
            remain -= k - 1
        else:
            ans[i] += remain
            remain = 0
            break
    print(' '.join(map(str, ans))) if not remain else print(-1)
for _ in range(int(input())):
    solve()