You are given an array aa consisting of nn positive integers. You can perform operations on it.
In one operation you can replace any element of the array a_iai with \lfloor \frac{a_i}{2} \rfloor⌊2ai⌋, that is, by an integer part of dividing a_iai by 22 (rounding down).
See if you can apply the operation some number of times (possible 00) to make the array aa become a permutation of numbers from 11 to nn —that is, so that it contains all numbers from 11 to nn, each exactly once.
For example, if a = [1, 8, 25, 2]a=[1,8,25,2], n = 4n=4, then the answer is yes. You could do the following:
- Replace 88 with \lfloor \frac{8}{2} \rfloor = 4⌊28⌋=4, then a = [1, 4, 25, 2]a=[1,4,25,2].
- Replace 2525 with \lfloor \frac{25}{2} \rfloor = 12⌊225⌋=12, then a = [1, 4, 12, 2]a=[1,4,12,2].
- Replace 1212 with \lfloor \frac{12}{2} \rfloor = 6⌊212⌋=6, then a = [1, 4, 6, 2]a=[1,4,6,2].
- Replace 66 with \lfloor \frac{6}{2} \rfloor = 3⌊26⌋=3, then a = [1, 4, 3, 2]a=[1,4,3,2].
Input
The first line of input data contains an integer tt (1 \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 nn (1 \le n \le 501≤n≤50), the second one contains integers a_1, a_2, \dots, a_na1,a2,…,an (1 \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 aa become a permutation of numbers from 11 to nn,
- 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 | YES NO YES NO NO YES |
有一个长度为 n 的数组 a。 你可以对其中的数进行以下操作: 不断除以 2,并向下取整。 求:能否在若干次操作后,使得 a 数组变成一个 1∼n 的全排列。
Input
第一行输入包含一个整数 tt (1 \le t \le 10^41≤t≤104) —测试用例的数量
每个测试用例包含一个整数 nn (1 \le n \le 501≤n≤50), 第二行包含整数a_1, a_2, \dots, a_na1,a2,…,an (1 \le a_i \le 10^91≤ai≤109).
Output
每个用例输出单独的一行
- YES如果你可以使数组 aa 成为一个排列 11 到 nn,
- NO
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t -- )
{
int n;
cin >> n;
int a[100];
for (int i = 1; i <= n ; i ++ )
{
cin >> a[i];
}
int bk[100] = {0};
for (int i = 1; i <= n; i ++ )
{
int x = a[i];
while (x > n) x /= 2;
while (bk[x] && x > 0) x /= 2;
bk[x] = 1;
}
int f = 1;
for (int i= 1; i <= n; i ++ )
{
if (!bk[i])
{
cout << "NO" << endl;
f = 0;
break;
}
}
if (f) cout << "YES" << endl;
}
return 0;
}