Array Construction

5

2 votes
Basics of Hash Tables, Data Structures, Hash Tables
Problem

You are given two arrays a=[a1,a2,...,an] and b=[b1,b2,...,bn], with a1=a2=...=an=0.

First, you must choose two of indices i,j (1i,jn,ij) and assign ai=aj=1.

Then perform the following operation as many times as you want:

  • Choose an index k(1kn), and add the product of the two largest integers in a to ak. That is, ak=ak+aiaj, where aiajal for all l(1ln,li,lj).

Now, determine if you can transform the array a into the array b or not.

If you can construct the array, then print “YES”. Otherwise, print “NO”.

Constraints

  • 1T1000
  • 2n105
  • 0bi109

Input Format

  • The first line contains a single integer T —- the number of test cases.
  • For each testcase:
    • The first line contains one integer n —- the length of the arrays.
    • The second line contains n nonnegative integers b1,b2,...,bn.
  • The sum of n over all test cases does not exceed 106.

Output Format

  • For each Testcase, output "YES" if the array a can be transformed into b. Otherwise, output "NO".
Time Limit: 1.5
Memory Limit: 256
Source Limit:
Explanation

In the first case:

  • a=[0,0,0,0] initially.
  • Assign a1=a2=1, so a=[1,1,0,0].
  • Operation with k=1, after the opertaion a=[2,1,0,0].
  • Operation with k=1, after the opertaion a=[4,1,0,0].

  • Operation with k=1, after the opertaion a=[8,1,0,0].

In the second case:

  • a=[0,0,0,0] initially.
  • Assign a1=a2=1, so a=[1,1,0,0].
  • Operation with k=2, after the opertaion a=[1,2,0,0].
  • Operation with k=3, after the opertaion a=[1,2,2,0].

  • Operation with k=1, after the opertaion a=[5,2,2,0].

Editor Image

?