codeforce 1478C C. Nezzar and Symmetric Array 模拟 认真写 C


codeforce 1478C C. Nezzar and Symmetric Array 模拟 认真写 C

https://codeforces.com/contest/1478/problem/C

C. Nezzar and Symmetric Array time limit per test 2 seconds memory limit per test 512 megabytes input standard input output standard output

Long time ago there was a symmetric array a1,a2,,a2n">a1,a2,,a2na1,a2,…,a2n consisting of 2n">2n2n distinct integers. Array a1,a2,,a2n">a1,a2,,a2na1,a2,…,a2n is called symmetric if for each integer 1i2n">1i2n1≤i≤2n, there exists an integer 1j2n">1j2n1≤j≤2n such that ai=aj">ai=?ajai=?aj.

For each integer 1i2n">1i2n1≤i≤2n, Nezzar wrote down an integer di">didi equal to the sum of absolute differences from ai">aiai to all integers in a">aa, i. e. di=j=12n|aiaj|">di=2nj=1|ai?aj|di=∑j=12n|ai?aj|.

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

Input

The first line contains a single integer t">tt (1t105">1t1051≤t≤105) — the number of test cases.

The first line of each test case contains a single integer n">nn (1n105">1n1051≤n≤105).

The second line of each test case contains 2n">2n2n integers d1,d2,,d2n">d1,d2,,d2nd1,d2,…,d2n (0di1012">0di10120≤di≤1012).

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

Output

For each test case, print "YES" in a single line if there exists a possible array a">aa. Otherwise, print "NO".

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

Example input Copy
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
output Copy
YES
NO
NO
NO
NO
YES
Note

In the first test case, a=[1,3,1,3]">a=[1,?3,?1,3]a=[1,?3,?1,3] is one possible symmetric array that generates the array d=[8,12,8,12]">d=[8,12,8,12]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">dd.

 分析

按照题目说的

很容易发现

顺序没有用,对应的数一定有对应的结果

写的时候一定认真写

比赛的时候写的乱七八糟的,结果用了很久才写出来

写错了好多好多次

按照从小到大排序a数组

那么d数组最后的面的就是n*2*a_0

往前依次有规律递减两个数,d数组倒数第二个是(n-1)*2*a_1*2*a_0

照样子写出来就可以了

代码

#include 
#include 
#include 
#include 
#include 
#include <string.h>
#include 
#include 
#include <string>
#include 
#include 
#include 
#include 
#include 
#include 
#include <set>
#include 
#include 
#include <string.h>
#include 
#define sf scanf
#define pf printf
#define lf double
#define p123 printf("123\n");
#define pn printf("\n");
#define pk printf(" ");
#define p(n) printf("%d",n);
#define pln(n) printf("%d\n",n);
#define s(n) scanf("%d",&n);
#define ss(n) scanf("%s",n);
#define ps(n) printf("%s",n);
#define sld(n) scanf("%lld",&n);
#define pld(n) printf("%lld",n);
#define slf(n) scanf("%lf",&n);
#define plf(n) printf("%lf",n);
#define sc(n) scanf("%c",&n);
#define pc(n) printf("%c",n);
#define gc getchar();
#define ll long long
#define re(n,a) memset(n,a,sizeof(n));
#define len(a) strlen(a)
#define eps 1e-13
#define zero(x) (((x) > 0? (x):(-x)) < eps)
using namespace std;
vector a(100005);
vector b(100005);
map m;
int main(){
    ll t;
    sld(t)
    while(t --){
        b.clear();
        a.clear();
        ll n,d;
        sld(n)
        ll x = 0ll;
        n*=2ll;
        ll flag = 0ll;
        ll tt;
        for(ll i = 0ll; i < n; i ++){
            sld(tt)
            if(m[tt]%2ll == 0){
                a.push_back(tt);
            }
            m[tt] ++;
            if(tt % 2ll == 1ll){
                flag = 1ll;
            }
            x ^= tt;
        }
        if(x != 0ll || flag == 1ll){
            puts("NO");
            continue;
        }
        sort(a.begin(),a.end());
        ll sum = 0ll;
        ll maxi = n;
        for(ll i = n/2ll-1ll; i >= 0ll ; i --){
            if( (a[i]-sum)%((i+1ll)*2ll) != 0ll){
                flag = 1ll;
                break;
            }
            if(a[i]-sum <= 0){
                flag = 1ll;
                break;
            }
            maxi = (a[i]-sum)/((i+1ll)*2ll);
            b.push_back(maxi);
            sum += maxi*2ll;
        }
        if(b[0ll] == 0ll){
            flag = 1ll;
        }
        for(ll i = 1ll; i < b.size(); i ++){
            //pld(b[i]) pk
            if(b[i] == b[i-1ll]){
                flag = 1ll;
                break;
            }
            if(b[i] == 0ll){
                flag = 1ll;
                break;
            }
        }
        if(flag == 1ll){
            puts("NO");
        }else{
            puts("YES");
        }
    }
    return 0;
}

https://codeforces.com/contest/1478/submission/105764657

ACM