B. Equal Rectangles ( Codeforces Round #579 )

爱⌒轻易说出口 提交于 2019-11-28 16:42:35

You are given 4n4n sticks, the length of the ii-th stick is aiai.

You have to create nn rectangles, each rectangle will consist of exactly 44 sticks from the given set. The rectangle consists of four sides, opposite sides should have equal length and all angles in it should be right. Note that each stick can be used in only one rectangle. Each stick should be used as a side, you cannot break the stick or use it not to the full length.

You want to all rectangles to have equal area. The area of the rectangle with sides aa and bb is aba⋅b.

Your task is to say if it is possible to create exactly nn rectangles of equal area or not.

You have to answer qq independent queries.

Input

The first line of the input contains one integer qq (1q5001≤q≤500) — the number of queries. Then qq queries follow.

The first line of the query contains one integer nn (1n1001≤n≤100) — the number of rectangles.

The second line of the query contains 4n4n integers a1,a2,,a4na1,a2,…,a4n (1ai1041≤ai≤104), where aiai is the length of the ii-th stick.

Output

For each query print the answer to it. If it is impossible to create exactly nn rectangles of equal area using given sticks, print "NO". Otherwise print "YES".

Example
input
Copy
5
1
1 1 10 10
2
10 5 2 10 1 1 2 5
2
10 5 1 10 5 1 1 1
2
1 1 1 1 1 1 1 1
1
10000 10000 10000 10000
output
Copy
YES
YES
NO
YES
YES
#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
typedef long long ll;
using namespace std;
const int INT=1e6+5;
#define lson rt<<1, l, m  
#define rson rt<<1|1, m+1, r
#define read(x) scanf("%d",&x)
#define lread(x) scanf("%lld",&x);
#define pt(x) printf("%d\n",(x))
#define cn cin>>
#define ct cout<<
#define en <<endl
#define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++)
#define mem(s,t) memset(s,t,sizeof(s))
#define re return 0;
#define TLE std::ios::sync_with_stdio(false);
ll a[100000+5],b[10000+5];
priority_queue<ll>q1,q2;
int main()
{
    TLE;
    int t,n,m,mx,flag;
    cn t;
    while(t--)
    {
        map<int,int>mp;
        mx=-INT;
        cn n;
        mem(b,0);
        rep(1,4*n)
        {
            cn a[i];
            b[ a[i] ]++;
        }
        m=0;flag = 0;
        sort(a+1,a+4*n+1);
        mx=a[1]*a[4*n];
        if(n==1 && b[a[1]]==4) 
            ct "YES" en;
        else
        {
            rep(1,4*n-1)
            {
                if(a[i]!=a[i+1] || a[i]*a[4*n-i+1]!=mx)
                {
                    flag = 1;
                    break;                    
                }
                i++;
            }
            if(flag)     ct "NO" en;
            else     ct "YES" en;
        }
    }
    re ;
}
View Code

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!