You are given an array aaa consisting of nnn positive integers. You can perform operations on it.
In one operation you can replace any element of the array aia_iai with ⌊ai2⌋\lfloor \frac{a_i}{2} \rfloor⌊2ai⌋, that is, by an integer part of dividing aia_iai by 222 (rounding down).
See if you can apply the operation some number of times (possible 000) to make the array aaa become a permutation of numbers from 111 to nnn —that is, so that it contains all numbers from 111 to nnn, each exactly once.
For example, if a=[1,8,25,2]a = [1, 8, 25, 2]a=[1,8,25,2], n=4n = 4n=4, then the answer is yes. You could do the following:
- Replace 888 with ⌊82⌋=4\lfloor \frac{8}{2} \rfloor = 4⌊28⌋=4, then a=[1,4,25,2]a = [1, 4, 25, 2]a=[1,4,25,2].
- Replace 252525 with ⌊252⌋=12\lfloor \frac{25}{2} \rfloor = 12⌊225⌋=12, then a=[1,4,12,2]a = [1, 4, 12, 2]a=[1,4,12,2].
- Replace 121212 with ⌊122⌋=6\lfloor \frac{12}{2} \rfloor = 6⌊212⌋=6, then a=[1,4,6,2]a = [1, 4, 6, 2]a=[1,4,6,2].
- Replace 666 with ⌊62⌋=3\lfloor \frac{6}{2} \rfloor = 3⌊26⌋=3, then a=[1,4,3,2]a = [1, 4, 3, 2]a=[1,4,3,2].
Input
The first line of input data contains an integer ttt (1≤t≤1041 \le t \le 10^41≤t≤104) —the number of test cases.
Each test case contains exactly two lines. The first one contains an integer nnn (1≤n≤501 \le n \le 501≤n≤50), the second one contains integers a1,a2,…,ana_1, a_2, \dots, a_na1,a2,…,an (1≤ai≤1091 \le a_i \le 10^91≤ai≤109).
Output
For each test case, output on a separate line:
- YES if you can make the array aaa become a permutation of numbers from 111 to nnn,
- NO otherwise.
You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
Sample 1
Inputcopy | Outputcopy |
---|---|
6 4 1 8 25 2 2 1 1 9 9 8 3 4 2 7 1 5 6 3 8 2 1 4 24 7 16 7 5 22 6 22 4 22 |
思路:我们用一个数组a作为标记,数组中有过该数则标记为一,若a[n]为0的话,则说明该数第一次为出现,标记为1,若a[n]为1的话,则说明有该数字了。
先判断该数是否大于n,如果大于n的话则除以2,直到小于n为止,若a[n]为0,则标记为1,如果a[n]为1 的话就说明该数已经出现过了,那我们再将该数除以二,直到成为没有出现过的数,然后标记。
#include<bits/stdc++.h>
using namespace std;
bool a[60];
int main()
{
int t;cin>>t;
while(t--)
{
memset(a,false,sizeof a);
int n;cin>>n;
int ans=0;
for(int i=1;i<=n;i++)
{
int x;cin>>x;
while(x>n) x/=2;
while(x>0)
{
if(!a[x])
{
a[x]=true;
ans++;
break;
}
x/=2;
}
}
if(ans==n) cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}