数组A中最大的元素数量可以通过使用操作最多可将其降低至1

Maximum number of elements in array A that can be reduced to 1 by using operation at most K times

本文关键字:操作 元素 可以通过 数组      更新时间:2023-10-16

您有一个长度N的数组A。您可以在数组的元素上执行操作(可以多次执行操作(A。在操作中,您可以将任何元素分开最小的因素大于1。您将得到Q任务。在每个任务中,您将获得一个整数K大多数k时。

输入:

第一行包含两个空间分隔的整数,n和q。

第二行包含n个空间分离的整数,表示数组a。

的元素

下一个Q线包含一个整数,表示k。

的值

输出:

对于每个任务,在新行中打印t th 任务的答案。

Example Input:                          Example Output:
3 3                                     1
8 9 12                                  3
3                                       0
10
1
Explanation:
Number of operations required are 3,2,3 respectively.
For the first task, we can reduce any one of the three elements to 1.
For second task, we can reduce all the elements of the array to 1.
For third task, we cannot reduce any elements to 1.

对不起我的英语。

首先,您可以建立一个eRatosthenes的筛子,但不是一个布尔值,而是保存较小的素数的信息,例如:

for (int i=4; i < MAXN; i+=2) sieve[i] = 2;
for (int i=3; i < MAXN; i+=2){
   if (!sieve[i]) for (int j=i*i; j <= MAXN; j+=2*i){
       if (!sieve[j]) sieve[j] = i;
}  }

然后,使用筛子,您可以轻松且最佳的构建一个数组B,其中包含A中的每个元素所需的操作数量。之后,对数组B进行排序,您可以使用二进制搜索来回答查询。