C. Nezzar and Symmetric Array
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
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.
定义0<a<b<c<d. a=[a,b,c,d,-a,-b,-c,-d]
a,-a sum=b-a+c-a+d-a+2a+a+b+a+c+a+d=2a+2b+2c+2d
b,-b sum=b-a+c-b+d-b+b+a+b+b+b+c+b+d=4b+2c+2d
c,-c sum=c-a+c-b+d-c+c+a+c+b+c+c+c+d=6c+2d
d,-d sum=d-a+d-b+d-c+d+a+d+b+d+c+d+d=8d
总结出三个要求
- 所有数均为偶数
- 有n对相同的数
- ai中的数均为正整数且不相同
#include<bits/stdc++.h>
using namespace std;
#define N 1000000
typedef long long ll;
ll n,flag,d[N],t,sum,x;
int main()
{
scanf("%lld",&t);
while (t--)
{
set<ll> s;
scanf("%lld",&n);
flag=0;
for (int i=1;i<=2*n;i++) scanf("%lld",&d[i]);
sort(d+1,d+1+2*n);
ll cnt=2*n;
sum=0;
for (int i=2*n;i>=2;i-=2)
{
if((d[i]-sum)%cnt==0&&d[i]>sum&&s.count((d[i]-sum)/cnt)==0&&d[i]==d[i-1])
{
s.insert((d[i]-sum)/cnt);
sum+=((d[i]-sum)/cnt)*2;
}
else
{
flag=1;
break;
}
cnt-=2;
}
if (flag) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}