Candy Party (Hard Version)
Contest: Codeforces Round 896 (Div. 1)
Difficulty rating: 2100
Tags: bitmasks, constructive algorithms, dp, greedy, implementation, math

This is the hard version of the problem. The only difference is that in this version everyone must give candies to no more than one person and receive candies from no more than 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 no more than 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 no more than 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 format:
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 format:
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.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first test case, the second person gives $$$1$$$ candy to the first person, then all people have $$$3$$$ candies.

In the second test case, the fourth person gives $$$1$$$ candy to the second person, the fifth person gives $$$2$$$ candies to the first person, the third person does nothing. And after the swaps everyone has $$$3$$$ 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 two people do not need to do anything.

Example input:
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

Example output:
Yes
Yes
No
Yes
No
Yes

Editorial:
Read the tutorial for D1 first.
Consider the graph we built in this version. It should only consist of chains and cycles.
For the start nodes of chains and the end nodes of chains, $$$|a_i-s|=2^d$$$ must hold.
Thus, for $$$|a_i-s|\ne 2^d$$$, we sill have only one way to decide the number of candies given away and received (the same as D1). However, for $$$|a_i-s|=2^d$$$, we have two ways:
1. the same way as D1 (use the equation $$$a_i-2^{x_i}+2^{y_i}=s$$$);
2. let this person be the start or end node of chains, that is, $$$a_i-2^{x_i}=s$$$ or $$$a_i-2^{x_i}=s$$$. (Suppose $$$2^{x_i}$$$ is the number of candies he gives away or receives)
Again, let's omit those nodes with $$$|a_i-s|=2^d$$$ first. Then we can similarly maintain two sets $$$S$$$ and $$$T$$$ to D1.
Let $$$cntS_i$$$ be the number of $$$i$$$-s in set $$$S$$$ and $$$cntT_i$$$ be the number of $$$i$$$-s in $$$T$$$. Note that now we omit nodes with $$$|s-a_i|=2^d$$$ when maintaining both sets.
Let $$$cntDS_k$$$ be the number of nodes with $$$a_i-s=2^k$$$ and $$$cntDT_i$$$ be the number of nodes with $$$s-a_i=2^{k}$$$.
For nodes with $$$|a_i-s|=2^{x_i}$$$, the two ways are:
1. $$$a_i-2^{x_i}=s$$$, or $$$a_i+2^{x_i}=s$$$;
2. $$$a_i+2^{x_i}-2^{x_i+1}=s$$$, or $$$a_i-2^{x_i-1}+2^{x_i}$$$.
To make the two sets $$$S$$$ and $$$T$$$ equal, we must make $$$cntS_k=cntT_k$$$ for all $$$0\le k\le 30$$$.
Now consider nodes with $$$|a_i-s|=2^d$$$. We can determine bit-by-bit. Let's start from the first bit.
Consider $$$cntDS_i$$$: if we choose $$$x\le cntDS_i$$$ elements to use the first way, then:
• $$$cntS_i:=cntS_i+x$$$;
• $$$cntS_{i+1}:=cntS_{i+1}+x$$$;
• $$$cntT_i:=cntT_i+(cntDS_i-x)$$$.
Consider $$$cntDT_i$$$: if we choose $$$x\le cntDT_i$$$ elements to use the first way, then:
• $$$cntT_i:=cntT_i+x$$$;
• $$$cntT_{i-1}:=cntT_{i-1}+x$$$;
• $$$cntT_i:=cntT_i+(cntDT_i-x)$$$.
Let's consider the highest bit. All elements in $$$cntDS_i$$$ must be chosen the second way ($$$x=0$$$), otherwise, it will influence $$$cntS_{i+1}$$$. Then we can determine the $$$x$$$ for $$$cntDT_i$$$, then $$$cntDS_{i-1},cntDT_{i-1},...,cntDS_0,cntDT_0$$$.
At last, we check if $$$cntS_0=cntT_0$$$ (we have made $$$cntS_i=cntT_i$$$ for all $$$i>0$$$). If so, the answer is "Yes", otherwise the answer is "No".