Vupsen, Pupsen and 0 (Codeforces 1582D)
Tags: Math, constructive algorithms

Vupsen and Pupsen were gifted an integer array. Since Vupsen doesn't like the number $$$0$$$, he threw away all numbers equal to $$$0$$$ from the array. As a result, he got an array $$$a$$$ of length $$$n$$$.Pupsen, on the contrary, likes the number $$$0$$$ and he got upset when he saw the array without zeroes. To cheer Pupsen up, Vupsen decided to come up with another array $$$b$$$ of length $$$n$$$ such that $$$\sum_{i=1}^{n}a_i \cdot b_i=0$$$. Since Vupsen doesn't like number $$$0$$$, the array $$$b$$$ must not contain numbers equal to $$$0$$$. Also, the numbers in that array must not be huge, so the sum of their absolute values cannot exceed $$$10^9$$$. Please help Vupsen to find any such array $$$b$$$!

Input: The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The next $$$2 \cdot t$$$ lines contain the description of test cases. The description of each test case consists of two lines.The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the length of the array.The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$-10^4 \le a_i \le 10^4$$$, $$$a_i \neq 0$$$) — the elements of the array $$$a$$$.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output: For each test case print $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ — elements of the array $$$b$$$ ($$$|b_1|+|b_2|+\ldots +|b_n| \le 10^9$$$, $$$b_i \neq 0$$$, $$$\sum_{i=1}^{n}a_i \cdot b_i=0$$$).It can be shown that the answer always exists.

Note: In the first test case, $$$5 \cdot 1 + 5 \cdot (-1)=5-5=0$$$. You could also print $$$3$$$ $$$-3$$$, for example, since $$$5 \cdot 3 + 5 \cdot (-3)=15-15=0$$$In the second test case, $$$5 \cdot (-1) + (-2) \cdot 5 + 10 \cdot 1 + (-9) \cdot (-1) + 4 \cdot (-1)=-5-10+10+9-4=0$$$.

Sample Inputs:
3
2
5 5
5
5 -2 10 -9 4
7
1 2 3 4 5 6 7

Sample Outputs:
1 -1
-1 5 1 -1 -1
-10 2 2 -3 5 -1 -1

Solution (Python):
def main():
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    inf = 1e+17
    mod = 10 ** 9 + 7

    def factorial(n):
        f = 1
        for i in range(1, n + 1):
            f = f * i % mod
        return f

    def factorial_by(n, by):
        f = 1
        for i in range(1, n + 1):
            if i == by:
                continue
            f = f * i % mod
        return f

    def ncr(n, r):
        num = den = 1
        for i in range(r):
            num = num * (n - i) % mod
            den = den * (i + 1) % mod
        return num * pow(den, mod - 2, mod) % mod

    def primeFactors(num):
        pf = []
        while num % 2 == 0:
            pf.append(2)
            num = num // 2
        for i in range(3, int(math.sqrt(num)) + 1, 2):
            while num % i == 0:
                pf.append(i)
                num = num // i
        if num > 2:
            pf.append(num)
        return pf

    class Node(object):

        def __init__(self, name, d):
            self.d = d
            self.name = name

        def __repr__(self):
            pass

    def solve(n, a):
        if n % 2:
            b = []
            for i in range(0, n - 3, 2):
                b.append(-a[i + 1])
                b.append(a[i])
            b.append(0)
            b.append(0)
            b.append(0)
            if a[n - 3] != a[n - 2]:
                b[n - 3] = -a[n - 1]
                b[n - 2] = a[n - 1]
                b[n - 1] = a[n - 3] - a[n - 2]
            elif a[n - 3] != a[n - 1]:
                b[n - 3] = -a[n - 2]
                b[n - 2] = a[n - 3] - a[n - 1]
                b[n - 1] = a[n - 2]
            elif a[n - 1] != a[n - 2]:
                b[n - 3] = a[n - 2] - a[n - 1]
                b[n - 2] = -a[n - 3]
                b[n - 1] = a[n - 3]
            else:
                b[n - 3] = 1
                b[n - 2] = 1
                b[n - 1] = -2
            return ' '.join(list(map(str, b)))
        else:
            b = []
            for i in range(0, n, 2):
                b.append(-a[i + 1])
                b.append(a[i])
            return ' '.join(list(map(str, b)))
    t = int(input())
    ans = []
    for _ in range(t):
        n = int(input())
        a = [int(x) for x in input().split()]
        ans.append(solve(n, a))
    p = 1
    for answer in ans:
        print(answer)
        p += 1
if __name__ == '__main__':
    import sys, threading
    import bisect
    import math
    import itertools
    from sys import stdout
    import heapq
    from queue import PriorityQueue
    from collections import deque
    sys.setrecursionlimit(2 ** 32 // 2 - 1)
    threading.stack_size(1 << 27)
    input = sys.stdin.readline
    thread = threading.Thread(target=main)
    thread.start()
    thread.join()

Solution (Python):
def solve(n, aa):
    (head, tail) = ([], [])
    if n % 2 == 1:
        (a, b, c) = (aa[n - 3], aa[n - 2], aa[n - 1])
        if b + c != 0:
            tail = [b + c, -a, -a]
        else:
            tail = [b - c, -a, a]
        n -= 3
    idx = 0
    while idx < n:
        head.append(aa[idx + 1])
        head.append(-aa[idx])
        idx += 2
    return head + tail
for _ in range(int(input())):
    n = int(input())
    aa = list(map(int, input().split()))
    bb = solve(n, aa)
    print(' '.join(map(str, bb)))

Solution (Python):
import sys
p2D = lambda x: print(*x, sep='\n')

def II():
    return int(sys.stdin.buffer.readline())

def MI():
    return map(int, sys.stdin.buffer.readline().split())

def LI():
    return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
    return [LI() for _ in range(rows_number)]

def BI():
    return sys.stdin.buffer.readline().rstrip()

def SI():
    return sys.stdin.buffer.readline().rstrip().decode()

def li():
    return [int(i) for i in input().split()]

def lli(rows):
    return [li() for _ in range(rows)]

def si():
    return input()

def ii():
    return int(input())

def ins():
    return input().split()

def drank(d, processing, da, rank):
    tmp = 10 ** 9
    if len(d[da]) == 1:
        return 1
    for di in d[da]:
        if processing[di - 1] == 0:
            processing[di - 1] = 1
            tmp = min(tmp, drank(d, processing, di, rank))
            processing[di - 1] = 0
    rank[da - 1] = tmp + 1
    return tmp + 1

def binary_search(n, a):
    l = len(a)
    low = 0
    high = l - 1
    while high >= low:
        mid = (high + low) // 2
        if a[mid] == n:
            return mid
        elif a[mid] > n:
            high = mid - 1
        else:
            low = mid + 1
    return -1
mod = 10 ** 9 + 7

def solve():
    n = ii()
    a = li()
    m = n - 3 if n % 2 else n
    b = []
    for i in range(m // 2):
        b.append(a[2 * i + 1])
        b.append(-a[2 * i])
    if n % 2:
        if a[-2] + a[-1] != 0:
            b.append(a[-2] + a[-1])
            b.append(-a[-3])
            b.append(-a[-3])
        elif a[-3] + a[-2] != 0:
            b.append(a[-1])
            b.append(a[-1])
            b.append(-(a[-3] + a[-2]))
        else:
            b.append(a[-2])
            b.append(-(a[-1] + a[-3]))
            b.append(a[-2])
    return ' '.join(map(str, b))

def main():
    for _ in range(ii()):
        sys.stdout.write(str(solve()) + '\n')
if __name__ == '__main__':
    main()