Candy Party (Hard Version)

题意翻译

### 题目描述 有 $n$ 个人,第 $i$ 个人有 $a_i$ 颗糖,在派对上,每个人 **至多会做一次下面的事情** : - 选一个正整数 $p\ (\ 1 \leq p \leq n\ )$ 和一个非负整数 $x$ ,然后把 $2^x$ 颗糖给第 $p$ 个人。注意任意时刻一个人手上的糖不能变成负数,并且一个人不能把糖给自己,每个人只能接受至多一个人的糖。 你需要回答能否在上述操作后让每个人手中的糖果数量相同。 注意本题和 Easy Version 不同的是本题可以不接受糖果,也可以不给出糖果。 ### 输入格式 **本题有多组数据**。 第一行一个整数 $t\ (1 \leq t \leq 1000)$ ,表示数据组数。 对于每组数据: 第一行一个整数 $n\ (1 \leq n \leq 2\ ·10^5)$ 。 接下来一行 $n$ 个整数,表示$a_i\ (1 \leq a_i \leq 10^9)$。 保证对于所有数据,$\sum n \leq 2\ ·10^5$ 。 ### 输出格式 对于每组数据输出一行, "Yes" 代表存在一种方案满足要求,否则输出 "No" 。 对大小写不敏感。

题目描述

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.

输入输出格式

输入格式


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 $ .

输出格式


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.

输入输出样例

输入样例 #1

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

输出样例 #1

Yes
Yes
No
Yes
No
Yes

说明

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.