CF1601A.Array Elimination

普及-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given array a1,a2,…,ana_1, a_2, \ldots, a_n, consisting of non-negative integers.

Let's define operation of "elimination" with integer parameter kk (1≤k≤n1 \leq k \leq n) as follows:

  • Choose kk distinct array indices 1≤i1<i2<…<ik≤n1 \leq i_1 \lt i_2 \lt \ldots \lt i_k \le n.
  • Calculate x=ai1 & ai2 & … & aikx = a_{i_1} ~ \& ~ a_{i_2} ~ \& ~ \ldots ~ \& ~ a_{i_k}, where &\& denotes the bitwise AND operation (notes section contains formal definition).
  • Subtract xx from each of ai1,ai2,…,aika_{i_1}, a_{i_2}, \ldots, a_{i_k}; all other elements remain untouched.

Find all possible values of kk, such that it's possible to make all elements of array aa equal to 00 using a finite number of elimination operations with parameter kk. It can be proven that exists at least one possible kk for any array aa.

Note that you firstly choose kk and only after that perform elimination operations with value kk you've chosen initially.

给你一个由非负整数构成的数组 a1,a2,…,ana_1, a_2, \ldots, a_n。

我们定义参数为 kk(其中 1≤k≤n1 \leq k \leq n)的“消去”操作如下:

  • 选择 kk 个互不相同的数组下标 1≤i1<i2<…<ik≤n1 \leq i_1 \lt i_2 \lt \ldots \lt i_k \le n;
  • 计算 x=ai1 & ai2 & … & aikx = a_{i_1} ~ \& ~ a_{i_2} ~ \& ~ \ldots ~ \& ~ a_{i_k},其中 &\& 表示按位与运算(注释部分包含其形式化定义);
  • 将 xx 从每个 ai1,ai2,…,aika_{i_1}, a_{i_2}, \ldots, a_{i_k} 中减去;其余元素保持不变。

请找出所有可能的 kk 值,使得可以通过有限次参数为 kk 的消去操作,将数组 aa 的所有元素变为 00。可以证明:对任意数组 aa,至少存在一个可行的 kk。

注意:你需首先选定 kk,之后的所有消去操作均必须使用该初始选定的 kk 值。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \leq t \leq 10^4). Description of the test cases follows.

The first line of each test case contains one integer nn (1≤n≤200 0001 \leq n \leq 200\,000) — the length of array aa.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (0≤ai<2300 \leq a_i \lt 2^{30}) — array aa itself.

It's guaranteed that the sum of nn over all test cases doesn't exceed 200 000200\,000.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \leq t \leq 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤200 0001 \leq n \leq 200\,000)—— 数组 aa 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(0≤ai<2300 \leq a_i \lt 2^{30})—— 数组 aa 本身。

保证所有测试用例的 nn 之和不超过 200 000200\,000。

输出格式

For each test case, print all values kk, such that it's possible to make all elements of aa equal to 00 in a finite number of elimination operations with the given parameter kk.

Print them in increasing order.

对于每个测试用例,输出所有满足条件的值 kk,使得在给定参数 kk 下,可以通过有限次消除操作将数组 aa 的所有元素变为 00。

按升序输出这些值。

输入输出样例

  • 输入#1

    5
    4
    4 4 4 4
    4
    13 7 25 19
    6
    3 5 3 1 7 1
    1
    1
    5
    0 0 0 0 0

    输出#1

    1 2 4
    1 2
    1
    1
    1 2 3 4 5

说明/提示

In the first test case:

  • If k=1k = 1, we can make four elimination operations with sets of indices 1{1}, 2{2}, 3{3}, 4{4}. Since &\& of one element is equal to the element itself, then for each operation x=aix = a_i, so ai−x=ai−ai=0a_i - x = a_i - a_i = 0.
  • If k=2k = 2, we can make two elimination operations with, for example, sets of indices 1,3{1, 3} and 2,4{2, 4}: x=a1 & a3x = a_1 ~ \& ~ a_3 == a2 & a4a_2 ~ \& ~ a_4 == 4 & 4=44 ~ \& ~ 4 = 4. For both operations x=4x = 4, so after the first operation a1−x=0a_1 - x = 0 and a3−x=0a_3 - x = 0, and after the second operation — a2−x=0a_2 - x = 0 and a4−x=0a_4 - x = 0.
  • If k=3k = 3, it's impossible to make all aia_i equal to 00. After performing the first operation, we'll get three elements equal to 00 and one equal to 44. After that, all elimination operations won't change anything, since at least one chosen element will always be equal to 00.
  • If k=4k = 4, we can make one operation with set 1,2,3,4{1, 2, 3, 4}, because x=a1 & a2 & a3 & a4x = a_1 ~ \& ~ a_2 ~ \& ~ a_3 ~ \& ~ a_4 =4= 4.

In the second test case, if k=2k = 2 then we can make the following elimination operations:

  • Operation with indices 1,3{1, 3}: x=a1 & a3x = a_1 ~ \& ~ a_3 == 13 & 25=913 ~ \& ~ 25 = 9. a1−x=13−9=4a_1 - x = 13 - 9 = 4 and a3−x=25−9=16a_3 - x = 25 - 9 = 16. Array aa will become equal to [4,7,16,19][4, 7, 16, 19].
  • Operation with indices 3,4{3, 4}: x=a3 & a4x = a_3 ~ \& ~ a_4 == 16 & 19=1616 ~ \& ~ 19 = 16. a3−x=16−16=0a_3 - x = 16 - 16 = 0 and a4−x=19−16=3a_4 - x = 19 - 16 = 3. Array aa will become equal to [4,7,0,3][4, 7, 0, 3].
  • Operation with indices 2,4{2, 4}: x=a2 & a4x = a_2 ~ \& ~ a_4 == 7 & 3=37 ~ \& ~ 3 = 3. a2−x=7−3=4a_2 - x = 7 - 3 = 4 and a4−x=3−3=0a_4 - x = 3 - 3 = 0. Array aa will become equal to [4,4,0,0][4, 4, 0, 0].
  • Operation with indices 1,2{1, 2}: x=a1 & a2x = a_1 ~ \& ~ a_2 == 4 & 4=44 ~ \& ~ 4 = 4. a1−x=4−4=0a_1 - x = 4 - 4 = 0 and a2−x=4−4=0a_2 - x = 4 - 4 = 0. Array aa will become equal to [0,0,0,0][0, 0, 0, 0].

Formal definition of bitwise AND:

Let's define bitwise AND (&\&) as follows. Suppose we have two non-negative integers xx and yy, let's look at their binary representations (possibly, with leading zeroes): xk…x2x1x0x_k \dots x_2 x_1 x_0 and yk…y2y1y0y_k \dots y_2 y_1 y_0. Here, xix_i is the ii-th bit of number xx, and yiy_i is the ii-th bit of number yy. Let r=x & yr = x ~ \& ~ y is a result of operation &\& on number xx and yy. Then binary representation of rr will be rk…r2r1r0r_k \dots r_2 r_1 r_0, where:

r_i=begincases1, textif x_i=1 textand y_i=10, textif x_i=0 textor y_i=0endcasesr\_i = \\begin{cases} 1, ~ \\text{if} ~ x\_i = 1 ~ \\text{and} ~ y\_i = 1 \\\\ 0, ~ \\text{if} ~ x\_i = 0 ~ \\text{or} ~ y\_i = 0 \\end{cases}

在第一个测试用例中:

  • 若 k=1k = 1,我们可以执行四次消去操作,每次操作的下标集合分别为 {1}\{1\}、{2}\{2\}、{3}\{3\}、{4}\{4\}。由于单个元素的按位与(&\&)结果即为该元素自身,因此每次操作中 x=aix = a_i,从而有 ai−x=ai−ai=0a_i - x = a_i - a_i = 0。
  • 若 k=2k = 2,我们可以执行两次消去操作,例如下标集合分别为 {1,3}\{1, 3\} 和 {2,4}\{2, 4\}:x=a1 & a3=a2 & a4=4 & 4=4x = a_1 ~ \& ~ a_3 = a_2 ~ \& ~ a_4 = 4 ~ \& ~ 4 = 4。两次操作中均有 x=4x = 4,因此第一次操作后 a1−x=0a_1 - x = 0 且 a3−x=0a_3 - x = 0,第二次操作后 a2−x=0a_2 - x = 0 且 a4−x=0a_4 - x = 0。
  • 若 k=3k = 3,则无法使所有 aia_i 均变为 00。执行第一次操作后,将得到三个值为 00 的元素和一个值为 44 的元素;此后所有消去操作均不再改变数组,因为每次所选元素中至少有一个恒为 00。
  • 若 k=4k = 4,我们可以执行一次消去操作,其下标集合为 {1,2,3,4}\{1, 2, 3, 4\},因为 x=a1 & a2 & a3 & a4=4x = a_1 ~ \& ~ a_2 ~ \& ~ a_3 ~ \& ~ a_4 = 4。

在第二个测试用例中,若 k=2k = 2,则可执行如下消去操作:

  • 下标集合为 {1,3}\{1, 3\} 的操作:x=a1 & a3=13 & 25=9x = a_1 ~ \& ~ a_3 = 13 ~ \& ~ 25 = 9;a1−x=13−9=4a_1 - x = 13 - 9 = 4,a3−x=25−9=16a_3 - x = 25 - 9 = 16;数组 aa 变为 [4,7,16,19][4, 7, 16, 19]。
  • 下标集合为 {3,4}\{3, 4\} 的操作:x=a3 & a4=16 & 19=16x = a_3 ~ \& ~ a_4 = 16 ~ \& ~ 19 = 16;a3−x=16−16=0a_3 - x = 16 - 16 = 0,a4−x=19−16=3a_4 - x = 19 - 16 = 3;数组 aa 变为 [4,7,0,3][4, 7, 0, 3]。
  • 下标集合为 {2,4}\{2, 4\} 的操作:x=a2 & a4=7 & 3=3x = a_2 ~ \& ~ a_4 = 7 ~ \& ~ 3 = 3;a2−x=7−3=4a_2 - x = 7 - 3 = 4,a4−x=3−3=0a_4 - x = 3 - 3 = 0;数组 aa 变为 [4,4,0,0][4, 4, 0, 0]。
  • 下标集合为 {1,2}\{1, 2\} 的操作:x=a1 & a2=4 & 4=4x = a_1 ~ \& ~ a_2 = 4 ~ \& ~ 4 = 4;a1−x=4−4=0a_1 - x = 4 - 4 = 0,a2−x=4−4=0a_2 - x = 4 - 4 = 0;数组 aa 变为 [0,0,0,0][0, 0, 0, 0]。

按位与(bitwise AND)的严格定义如下:

我们定义按位与运算(&\&)如下:设 xx 和 yy 为两个非负整数,考察其二进制表示(必要时可在高位补零):xk…x2x1x0x_k \dots x_2 x_1 x_0 和 yk…y2y1y0y_k \dots y_2 y_1 y_0。其中 xix_i 表示 xx 的第 ii 位,yiy_i 表示 yy 的第 ii 位。令 r=x & yr = x ~ \& ~ y 为 xx 与 yy 进行 &\& 运算的结果,则 rr 的二进制表示为 rk…r2r1r0r_k \dots r_2 r_1 r_0,其中:

r_i=begincases1, text若 x_i=1 text且 y_i=10, text若 x_i=0 text或 y_i=0endcasesr\_i = \\begin{cases} 1, ~ \\text{若} ~ x\_i = 1 ~ \\text{且} ~ y\_i = 1 \\\\ 0, ~ \\text{若} ~ x\_i = 0 ~ \\text{或} ~ y\_i = 0 \\end{cases}

输入解题思路,AI测评打分。不知道怎么写?

首页