B. Integral Array
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
You are given an array aa of nn positive integers numbered from 11 to nn. Let's call an array integral if for any two, not necessarily different, numbers xx and yy from this array, x≥yx≥y, the number ⌊xy⌋⌊xy⌋ (xx divided by yy with rounding down) is also in this array.
You are guaranteed that all numbers in aa do not exceed cc. Your task is to check whether this array is integral.
Input
The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers nn and cc (1≤n≤1061≤n≤106, 1≤c≤1061≤c≤106) — the size of aa and the limit for the numbers in the array.
The second line of each test case contains nn integers a1a1, a2a2, ..., anan (1≤ai≤c1≤ai≤c) — the array aa.
Let NN be the sum of nn over all test cases and CC be the sum of cc over all test cases. It is guaranteed that N≤106N≤106 and C≤106C≤106.
Output
For each test case print Yes if the array is integral and No otherwise.
Examples
input
Copy
4 3 5 1 2 5 4 10 1 3 3 7 1 2 2 1 1 1
output
Copy
Yes No No Yes
input
Copy
1 1 1000000 1000000
output
Copy
No
Note
In the first test case it is easy to see that the array is integral:
- ⌊11⌋=1⌊11⌋=1, a1=1a1=1, this number occurs in the arry
- ⌊22⌋=1⌊22⌋=1
- ⌊55⌋=1⌊55⌋=1
- ⌊21⌋=2⌊21⌋=2, a2=2a2=2, this number occurs in the array
- ⌊51⌋=5⌊51⌋=5, a3=5a3=5, this number occurs in the array
- ⌊52⌋=2⌊52⌋=2, a2=2a2=2, this number occurs in the array
Thus, the condition is met and the array is integral.
In the second test case it is enough to see that
⌊73⌋=⌊213⌋=2⌊73⌋=⌊213⌋=2, this number is not in aa, that's why it is not integral.
In the third test case ⌊22⌋=1⌊22⌋=1, but there is only 22 in the array, that's why it is not integral.
==================================================================================================================================================
我们可以on遍历数组,对于每个数字,遍历其在c内的倍数,但由于要验证的是一个区间内的数字,所以可以利用前缀和来进行判断。要验证【L,R]区间内是否有数,用sum[R]-sum[L-1],判断是否为0即可。
但这样仍然超时,本题对时间的控制还是比较严格,考虑到1的特殊性,元素的重复性,故对2-c进行判断即可。
#include<bits/stdc++.h>
using namespace std;
int n,c,T,mat[1000005],s[1000005];
bool check(){
scanf("%d%d",&n,&c);
for(int i=1;i<=c;i++) mat[i]=s[i]=0;
for(int i=1,x;i<=n;i++){
scanf("%d",&x);
mat[x]=1;
}
if(!mat[1]) return false;
for(int i=1;i<=c;i++) s[i]=s[i-1]+mat[i];
for(int i=2;i<=c;i++){
if(!mat[i]) continue;
for(int j=i;j<=c;j+=i)
if(s[min(c,j+i-1)]-s[j-1])
if(!mat[j/i]) return false;
}
return true;
}
int main(){
scanf("%d",&T);
while(T--){
if(check()) printf("Yes\n");
else printf("No\n");
}
return 0;
}