C. Nezzar and Symmetric Array

Long time ago there was a symmetric array a1,a2,…,a2n consisting of 2n distinct integers. Array a1,a2,…,a2n is called symmetric if for each integer 1≤i≤2n, there exists an integer 1≤j≤2n such that ai=−aj.

For each integer 1≤i≤2n, Nezzar wrote down an integer di equal to the sum of absolute differences from ai to all integers in a, i. e. di=∑2nj=1|ai−aj|.

Now a million years has passed and Nezzar can barely remember the array d and totally forget a. Nezzar wonders if there exists any symmetric array a consisting of 2n distinct integers that generates the array d.

Input
The first line contains a single integer t (1≤t≤105) — the number of test cases.

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

The second line of each test case contains 2n integers d1,d2,…,d2n (0≤di≤1012).

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output
For each test case, print “YES” in a single line if there exists a possible array a. Otherwise, print “NO”.

You can print letters in any case (upper or lower).

Example
inputCopy
6
2
8 12 8 12
2
7 7 9 11
2
7 11 7 11
1
1 1
4
40 56 48 40 80 56 80 48
6
240 154 210 162 174 154 186 240 174 186 162 210
outputCopy
YES
NO
NO
NO
NO
YES
Note
In the first test case, a=[1,−3,−1,3] is one possible symmetric array that generates the array d=[8,12,8,12].

In the second test case, it can be shown that there is no symmetric array consisting of distinct integers that can generate array d.

思路
假设a>b>c,然后按照题目的方式求d,然后发现

|d1 = d2 = 2 * 3 * a|
|d3 = d4 = 2 * a + 4 * b|
|d5 = d6 = 2 * a + 2 * b + 2 * c|

然后大致的规律就找到了。

条件
1.有奇数,不成立
2.假如不成对出现,不成立
3.假如有相同的数或者后面大的数,不成立
4.数不能小于等于0(题目给出)

代码

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;

ll t, n, a[200500];

bool cmp(ll a, ll b)
{
	return a>b;
}

int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		int p=1;
		for(ll i=0; i<2*n; i++)
		{
			cin>>a[i];
			if(a[i]%2==1)//有奇数不成立 
			{
				p=0;
			} 
		}
		if(p==0)
		{
			printf("NO\n");
			continue;
		}
		sort(a, a+2*n, cmp);
//		for(int i=0; i<2*n; i++)
//		{
//			cout<<a[i]<<" ";
//		}
		for(int i=0; i<2*n; i+=2)//假如不成对出现,不成立 
		{
			if(a[i]!=a[i+1])
			{
				p=0;break;
			}
		} 
		if(p==0)
		{
			printf("NO\n");
			continue;
		}
		//开始算每一个数,假如有相同的数或者后面大的数,不成立 
		ll b, c, sum=0; 
 		c=(a[0]-sum)/(2*n);
		sum=2*c;
		if(a[0]%(2*n)!=0 || c<=0)//c不能小于等于0 
		{
			p=0;
			printf("NO\n");
			continue;
		}
		for(ll i=2; i<2*n; i+=2)
		{
			b=(a[i]-sum)/(2*n-i); 
			if(b>=c || (a[i]-sum)%(2*n-i)!=0 || b<=0)//b也不能小于等于0 
			{ 
				p=0;
				printf("NO\n");
				break;
			}
			sum+=2*b;
			c=b;
		}
		if(p) printf("YES\n");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值