Codeforces Round #778——C. Alice and the Cake

Alice has a cake, and she is going to cut it. She will perform the following operation n−1n−1 times: choose a piece of the cake (initially, the cake is all one piece) with weight w≥2w≥2 and cut it into two smaller pieces of weight ⌊w2⌋⌊w2⌋ and ⌈w2⌉⌈w2⌉ (⌊x⌋⌊x⌋ and ⌈x⌉⌈x⌉ denote floor and ceiling functions, respectively).

After cutting the cake in nn pieces, she will line up these nn pieces on a table in an arbitrary order. Let aiai be the weight of the ii-th piece in the line.

You are given the array aa. Determine whether there exists an initial weight and sequence of operations which results in aa.

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105).

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).

It is guaranteed that the sum of nn for all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, print a single line: print YES if the array aa could have resulted from Alice's operations, otherwise print NO.

You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).

Example

input

Copy

14
1
327
2
869 541
2
985214736 985214737
3
2 3 1
3
2 3 3
6
1 1 1 1 1 1
6
100 100 100 100 100 100
8
100 100 100 100 100 100 100 100
8
2 16 1 8 64 1 4 32
10
1 2 4 7 1 1 1 1 7 2
10
7 1 1 1 3 1 3 3 2 3
10
1 4 4 1 1 1 3 3 3 1
10
2 3 2 2 1 2 2 2 2 2
4
999999999 999999999 999999999 999999999

output

Copy

YES
NO
YES
YES
NO
YES
NO
YES
YES
YES
YES
NO
NO
YES

Note

In the first test case, it's possible to get the array aa by performing 00 operations on a cake with weight 327327.

In the second test case, it's not possible to get the array aa.

In the third test case, it's possible to get the array aa by performing 11 operation on a cake with weight 19704294731970429473:

  • Cut it in half, so that the weights are [985214736,985214737][985214736,985214737].

Note that the starting weight can be greater than 109109.

In the fourth test case, it's possible to get the array aa by performing 22 operations on a cake with weight 66:

  • Cut it in half, so that the weights are [3,3][3,3].
  • Cut one of the two pieces with weight 33, so that the new weights are [1,2,3][1,2,3] which is equivalent to [2,3,1][2,3,1] up to reordering.

题目类型:dfs, 剪枝,计数

解题思路:见代码注释。

AC代码:

#include <bits/stdc++.h>
#define PII pair <double, double>
#define inf 0x3f3f3f3f
#define int long long
using namespace std;
const int N = 2e5+10;
int a[N], sum;
bool flag;
map <int, int > mp;
/*
为什么可以用 mp[x] 判断 ,是否有解。
可以发现将样例 反向推理,可以得到一个 二叉树,同一层上的数字的差的绝对值不超过1,mp上记录的是所有数字出现次数,因此mp[x]>=该分支上所有x的个数, 所以当mp[x]不够的时候,一定没有解。 
*/
void dfs(int x)
{
   if(!flag)
   {
         if(x == 0) {  //cut 到0都没找到 理想数字。
          flag = true;
          return ;
        }if(mp[x]){ //理想数字存在, 个数减一,停止这部分折半,继续另一半的判断. 如果 两个数字相等,折半哪个的后序序列是相同的
          mp[x]--;
          return ;
        } 
          dfs(x/2);  //如果这部分的折半失败了,另一半进行也没有意义。
          dfs(x - x / 2);
   }
}
signed main()
{
    int t;
    cin>>t;
    while(t-- )
    {
        memset(a, 0, sizeof(a));
        int sum = 0, n;
          mp.clear();
          cin>> n;
        for (int i = 0;i < n;i ++) {
            cin >> a[i], sum += a[i];
            mp[a[i]] ++;
        }
        flag = false;
        dfs(sum);
        if (!flag)  cout << "YES" << endl;
        else  cout << "NO" << endl;
    
    }

   return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值