B. Equal Rectangles ( Codeforces Round #579 )

Posted shallow-dream

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了B. Equal Rectangles ( Codeforces Round #579 )相关的知识,希望对你有一定的参考价值。

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

 

以上是关于B. Equal Rectangles ( Codeforces Round #579 )的主要内容,如果未能解决你的问题,请参考以下文章

B. Equal Rectangles

B. Queries about less or equal elements1300 / 二分

Expression.Equal - 如何比较 Nullable 和 Non Nullable 字段?

HDU2056(rectangles)

hdu 2056 Rectangles

844B - Rectangles(组合数)