Codeforces Round #775 (Div. 2,based on Moscow Open Olympiad in Informatics) - D. Integral Array - 题解

Posted Tisfy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #775 (Div. 2,based on Moscow Open Olympiad in Informatics) - D. Integral Array - 题解相关的知识,希望对你有一定的参考价值。

目录

Codeforces Round #775 (Div. 2, based on Moscow Open Olympiad in Informatics)-D. Integral Array

传送门
Time Limit: 2 seconds
Memory Limit: 512 megabytes

Problem Description

You are given an array a a a of n n n positive integers numbered from 1 1 1 to n n n. Let’s call an array integral if for any two, not necessarily different, numbers x x x and y y y from this array, x ≥ y x \\ge y xy, the number ⌊ x y ⌋ \\left \\lfloor \\fracxy \\right \\rfloor yx ( x x x divided by y y y with rounding down) is also in this array.

You are guaranteed that all numbers in a a a do not exceed c c c. 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 t t t ( 1 ≤ t ≤ 1 0 4 1 \\le t \\le 10^4 1t104) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers n n n and c c c ( 1 ≤ n ≤ 1 0 6 1 \\le n \\le 10^6 1n106, 1 ≤ c ≤ 1 0 6 1 \\le c \\le 10^6 1c106) — the size of a a a and the limit for the numbers in the array.

The second line of each test case contains n n n integers a 1 a_1 a1, a 2 a_2 a2, …, a n a_n an ( 1 ≤ a i ≤ c 1 \\le a_i \\le c 1aic) — the array a a a.

Let N N N be the sum of n n n over all test cases and C C C be the sum of c c c over all test cases. It is guaranteed that N ≤ 1 0 6 N \\le 10^6 N106 and C ≤ 1 0 6 C \\le 10^6 C106.

Output

For each test case print Yes if the array is integral and No otherwise.

Sample Input

4
3 5
1 2 5
4 10
1 3 3 7
1 2
2
1 1
1

Sample Onput

Yes
No
No
Yes

Note

In the first test case it is easy to see that the array is integral:

Thus, the condition is met and the array is integral.

In the second test case it is enough to see that

⌊ 7 3 ⌋ = ⌊ 2 1 3 ⌋ = 2 \\left \\lfloor \\frac73 \\right \\rfloor = \\left \\lfloor 2\\frac13 \\right \\rfloor = 2 37=231=2, this number is not in a a a, that’s why it is not integral.

In the third test case ⌊ 2 2 ⌋ = 1 \\left \\lfloor \\frac22 \\right \\rfloor = 1 22=1, but there is only 2 2 2 in the array, that’s why it is not integral.


题目大意

给你一个数组 a a a,如果 ∀ x , y ∈ a \\forall x,y\\in a x,ya x ≥ y x\\geq y xy,都有 ⌊ x y ⌋ \\left \\lfloor \\fracxy \\right \\rfloor yx,那么就输出Yes;否则No。

解题思路

假设 x , y ∈ a x,y\\in a x,ya并且 r ∉ a r\\notin a r/a,如果 y ⋅ r ≤ x < y ⋅ ( r + 1 ) y⋅ r\\leq x< y⋅(r+1) yrx<y(r+1),那么 ⌊ x y ⌋ \\left \\lfloor \\fracxy \\right \\rfloor yx 就等于 r r r( ∉ a \\notin a /a),因此输出No

所以,对于所有的“不属于 a a a r r r、属于 a a a y y y”,如果存在“ y ⋅ r ≤ x < y ⋅ ( r + 1 ) y⋅ r\\leq x< y⋅(r+1) yrx<y(r+1)”的 x x x,那么就输出No

也就是说,枚举所有不属于 a a a r r r和所有属于 a a a y y y,对于这个“ r r r y y y”,看是否有属于 [ ( y ⋅ r ) , ( y ⋅ ( r + 1 ) ) ) [(y⋅ r),(y⋅(r+1))) [(yr),(y(r+1)))的元素 x x x a a a中,如果有就输出No;如果枚举完也没有不符合条件的 x x x,就输出Yes

具体方法:
第一层循环从 1 1 1 c c c枚举 r r r,第二层循环从 1 1 1开始枚举 y y y,直到 y ⋅ r > c y⋅r>c yr>c时结束。对于不存在于 a a a中的 r r r和存在于 a a a中的 y y y,查看是否有存在于不可行范围内的 a a a

复杂度分析:
循环复杂度为 O ( c + c 2 + c 3 + ⋯ + 1 ) = O ( c log ⁡ c ) O(c+\\fracc2+\\fracc3+\\cdots+1)=O(c\\log c) O(c+2c+3c++1)=O(clogc)
对于给定的 r r r y y y,可以用前缀和以 O ( 1 ) O(1) O(1)的复杂度求出一定范围内是否有 x ∈ a x\\in a xa
∑ c = C \\sum c=C Codeforces Round #775 (Div. 2) ABCD题解

Codeforces Round #775 (Div. 2) ABCD题解

Codeforces Round #775 (Div. 2) ABCD题解

Codeforces Round #775 (Div. 2, based on Moscow Open Olympiad in Informatics)(ABCDE)

Codeforces Round #774 (Div. 2)(ABCDE)

Codeforces Round #436 E. Fire(背包dp+输出路径)