Candy Party (Easy Version) (Codeforces 1869D1)
Tags: Math, bitmasks

This is the easy version of the problem. The only difference is that in this version everyone must give candies to exactly one person and receive candies from exactly one person. Note that a submission cannot pass both versions of the problem at the same time. You can make hacks only if both versions of the problem are solved.After Zhongkao examination, Daniel and his friends are going to have a party. Everyone will come with some candies.There will be $$$n$$$ people at the party. Initially, the $$$i$$$-th person has $$$a_i$$$ candies. During the party, they will swap their candies. To do this, they will line up in an arbitrary order and everyone will do the following exactly once:  Choose an integer $$$p$$$ ($$$1 \le p \le n$$$) and a non-negative integer $$$x$$$, then give his $$$2^{x}$$$ candies to the $$$p$$$-th person. Note that one cannot give more candies than currently he has (he might receive candies from someone else before) and he cannot give candies to himself. Daniel likes fairness, so he will be happy if and only if everyone receives candies from exactly one person. Meanwhile, his friend Tom likes average, so he will be happy if and only if all the people have the same number of candies after all swaps.Determine whether there exists a way to swap candies, so that both Daniel and Tom will be happy after the swaps.

Input: The first line of input contains a single integer $$$t$$$ ($$$1\le t\le 1000$$$) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer $$$n$$$ ($$$2\le n\le 2\cdot 10^5$$$) — the number of people at the party.The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1\le a_i\le 10^9$$$) — the number of candies each person has.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 "Yes" (without quotes) if exists a way to swap candies to make both Daniel and Tom happy, and print "No" (without quotes) otherwise.You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Note: In the first test case:  The first person gives $$$1$$$ candy to the third person;  The second person gives $$$2$$$ candies to the first person;  The third person gives $$$1$$$ candy to the second person. Then all people have $$$3$$$ candies.In the second test case:  The fifth person gives $$$4$$$ candies to the first person, from now on the first person has $$$5$$$ candies;  The first person gives $$$2$$$ candies to the third person;  The third person gives $$$2$$$ candies to the fifth person;  The fourth person gives $$$2$$$ candies to the second person;  The second person gives $$$1$$$ candy to the fourth person. Then all people have $$$3$$$ candies. Note that at first the first person cannot give $$$2$$$ candies to the third person, since he only has $$$a_1=1$$$ candy. But after the fifth person gives him $$$4$$$ candies, he can do this, because he currently has $$$1+4=5$$$ candies.In the third test case, it's impossible for all people to have the same number of candies.In the fourth test case, the first person gives $$$1024$$$ candies to the second person, and the second person gives $$$1024$$$ candies to the first person as well.

Sample Inputs:
6
3
2 4 3
5
1 2 3 4 5
6
1 4 7 1 5 4
2
20092043 20092043
12
9 9 8 2 4 4 3 5 1 1 1 1
6
2 12 7 16 11 12

Sample Outputs:
Yes
Yes
No
Yes
No
No

Solution (Python):
from sys import stdin
input = lambda : stdin.readline().rstrip()
from collections import Counter, defaultdict
import math
d = {}
for i in range(30):
    for j in range(30):
        d[2 ** i - 2 ** j] = (i, j)
for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    c = 0
    s = sum(a)
    if s // n != s / n:
        print('NO')
        continue
    s //= n
    m = [0] * 30
    mm = [0] * 30
    f = 0
    for i in a:
        dd = i - s
        if dd in d:
            m[d[dd][0]] += 1
            mm[d[dd][1]] += 1
        else:
            f = 1
            break
    if m == mm and f == 0:
        print('YES')
    else:
        print('NO')

Solution (Python):
from sys import stdin
input = lambda : stdin.readline().rstrip()
from collections import Counter, defaultdict
import math
d = {}
for i in range(30):
    for j in range(30):
        d[2 ** i - 2 ** j] = (i, j)
for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    c = 0
    s = sum(a)
    if s // n != s / n:
        print('NO')
        continue
    s //= n
    m = [0] * 30
    mm = [0] * 30
    f = 0
    for i in a:
        dd = i - s
        if dd in d:
            m[d[dd][0]] += 1
            mm[d[dd][1]] += 1
        else:
            f = 1
            break
    if m == mm and f == 0:
        print('YES')
    else:
        print('NO')

Solution (Python):
def solve():
    n = int(input())
    a = list(map(int, input().split()))
    s = 0
    for i in a:
        s += i
    if s % n != 0:
        print('NO')
        return
    sr = s // n
    mp = dict()
    for i in range(n):
        r = abs(a[i] - sr)
        tek = r + (r & -r)
        if tek & -tek != tek:
            print('NO')
            return
        if a[i] < sr:
            if tek in mp.keys():
                mp[tek] += 1
            else:
                mp[tek] = 1
            lb = r & -r
            if lb in mp.keys():
                mp[lb] -= 1
            else:
                mp[lb] = -1
        else:
            if tek in mp.keys():
                mp[tek] -= 1
            else:
                mp[tek] = -1
            lb = r & -r
            if lb in mp.keys():
                mp[lb] += 1
            else:
                mp[lb] = 1
    for i in mp.keys():
        if mp[i] != 0:
            print('NO')
            return
    print('YES')
t = int(input())
while t:
    solve()
    t -= 1