B. Equal Rectangles
Problem
You are given 4 × n 4 \times n 4×n sticks, the length of the i t h i^{th} ith stick is a i a_i ai.
You have to create n n n rectangles, each rectangle will consist of exactly 4 4 4 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 a a a and b b b is a × b a \times b a×b.
Your task is to say if it is possible to create exactly n n n rectangles of equal area or not.
You have to answer q q q independent queries.
Input
The first line of the input contains one integer q ( 1 ≤ q ≤ 500 ) q (1 \leq q \leq 500) q(1≤q≤500) — the number of queries. Then q q q queries follow.
The first line of the query contains one integer n ( 1 ≤ n ≤ 100 ) n (1 \leq n \leq 100) n(1≤n≤100) — the number of rectangles.
The second line of the query contains 4 × n 4 \times n 4×n integers a 1 , a 2 , … , a 4 n ( 1 ≤ a i ≤ 1 0 4 ) a_1,a_2,…,a_{4n} (1 \leq a_i \leq 10^4) a1,a2,…,a4n(1≤ai≤104), where a i a_i ai is the length of the i t h i^{th} ith stick.
Output
For each query print the answer to it. If it is impossible to create exactly n n n rectangles of equal area using given sticks, print “NO”. Otherwise print “YES”.
Example
input
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
YES
YES
NO
YES
YES
想法
因为要求所有矩形的面积一样,那么想到假设 S 1 = a 1 × b 1 , S 2 = a 2 × b 2 , S 3 = a 3 × b 3 S_1=a_1 \times b_1 , S_2=a_2 \times b_2 , S_3=a_3 \times b_3 S1=a1×b1,S2=a2×b2,S3=a3×b3,如果 a 1 < a 2 < a 3 a_1 < a_2 < a_3 a1<a2<a3,则必有 b 1 < b 2 < b 3 b_1 < b_2 < b_3 b1<b2<b3
那么就是稍微预处理边,求首尾相乘的面积是否相等。
The Code Of My Program
/*********************
*@Author: ChenShou *
*@Language: C++11 *
*********************/
//#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
#include<functional>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<sstream>
#include<iomanip>
#include<numeric>
#include<cctype>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
//const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-9;
const int INF = 0x3f3f3f3f;
inline ll read(){
long long x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
//cout.tie(0);
ll t ;
scanf("%lld",&t);
while(t--){
//}
int stick[10003]={0},n=read(),bian[500]={0},flag=1;
for(int i=0;i<4*n;i++){
stick[read()]++;
}
for(int i=0;i<10003;i++){
if(stick[i]%2){
flag=0;
}
else if(stick[i])stick[i]/=2;
}
if(!flag){
printf("NO\n");
continue;
}
int bs=0;
for(int i=0;i<10003;i++){
while(stick[i]--){
bian[bs++]=i;
}
}
int s=bian[0]*bian[n*2-1];
for(int i=0,j=n*2-1;i<j;i++,j--){
if(bian[i]*bian[j]!=s){
flag=0;
break;
}
}
if(flag){
printf("YES\n");
continue;
}
else {
printf("NO\n");
continue;
}
}
#ifdef DEBUG
printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
//cout << "Fuck You !" << endl;
return 0;
}