2048 Game(分治法)

传送门

You are playing a variation of game 2048. Initially you have a multiset ss of nn integers. Every integer in this multiset is a power of two.

You may perform any number (possibly, zero) operations with this multiset.

During each operation you choose two equal integers from ss, remove them from ss and insert the number equal to their sum into ss.

For example, if s={1,2,1,1,4,2,2}s={1,2,1,1,4,2,2} and you choose integers 22 and 22, then the multiset becomes {1,1,1,4,4,2}{1,1,1,4,4,2}.

You win if the number 20482048 belongs to your multiset. For example, if s={1024,512,512,4}s={1024,512,512,4} you can win as follows: choose 512512 and 512512, your multiset turns into {1024,1024,4}{1024,1024,4}. Then choose 10241024 and 10241024, your multiset turns into {2048,4}{2048,4} and you win.

You have to determine if you can win this game.

You have to answer qq independent queries.

Input

The first line contains one integer qq (1≤q≤1001≤q≤100) – the number of queries.

The first line of each query contains one integer nn (1≤n≤1001≤n≤100) — the number of elements in multiset.

The second line of each query contains nn integers s1,s2,…,sns1,s2,…,sn (1≤si≤2291≤si≤229) — the description of the multiset. It is guaranteed that all elements of the multiset are powers of two.

Output

For each query print YES if it is possible to obtain the number 20482048 in your multiset, and NO otherwise.

You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).

Example

Input

6
4
1024 512 64 512
1
2048
3
64 512 2
2
4096 4
7
2048 2 2048 2048 2048 2048 2048
2
2048 4096

Output

YES
YES
NO
NO
YES
YES

Note

In the first query you can win as follows: choose 512512 and 512512, and ss turns into {1024,64,1024}{1024,64,1024}. Then choose 10241024 and 10241024, and ss turns into {2048,64}{2048,64} and you win.

In the second query ss contains 20482048 initially.

题解:

题意概括:给你一列数字,都是2的次幂,这个数列中相同的数可以互相相加,相加之后的数列中相同的数字又可以相加。问这个数列在变换中有没有可能出现2048.

如果按照正常思路,就是把相同的数一直相加,看有没有2048出现,但是这样的话比较复杂。

这道题可以用分治法。

例如1024 512 256 128 128

把2048划分成1024 1024 上面有一个1024,消去一个。

把1024分成512 512,有一个512,消去一个。

把512分成256 256,有一个256,消去一个。

把256分成128 128,有一个128,消去一个,还有一个1278,再消去一个。

这样的话,2048向下分解的所有数能和数列中的数相抵消,输出YES。

#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <map>

using namespace std;

map<int,int> mp;
bool flag;

void fz(int n)
{
	if(n==0)//如果n一直分一直分,直到分到0了还没找到,那么就说明数列中的数相加不能等于2048,return。 
	{
		flag=0;
		return;
	}
	if(mp[n]>0) mp[n]--;//如果数列中有这个数,那就抵消 
	else//如果没有,继续分治向下寻找知道找到为止 
	{
		fz(n/2);
		fz(n/2);
	}
}

int main()
{
//	freopen("input.txt","r",stdin);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	int q;
	cin>>q;
	while(q--)
	{
		flag=1;
		mp.clear();
		int n;
		cin>>n;
		for(int i=0;i<n;i++) 
		{
			int tmp;
			cin>>tmp;
			mp[tmp]++;
		}
		fz(2048);
		if(flag) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	
	return 0;
}
原创文章 99 获赞 15 访问量 7358

猜你喜欢

转载自blog.csdn.net/qq_45328552/article/details/101108732
今日推荐