CF1579F.Array Stabilization (AND version)

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

You are given an array a[0n1]=[a0,a1,,an1]a[0 \ldots n - 1] = [a_0, a_1, \ldots, a_{n - 1}] of zeroes and ones only. Note that in this problem, unlike the others, the array indexes are numbered from zero, not from one.

In one step, the array aa is replaced by another array of length nn according to the following rules:

  1. First, a new array ada^{\rightarrow d} is defined as a cyclic shift of the array aa to the right by dd cells. The elements of this array can be defined as aid=a(i+nd)modna^{\rightarrow d}_i = a_{(i + n - d) \bmod n} , where (i+nd)modn(i + n - d) \bmod n is the remainder of integer division of i+ndi + n - d by nn . It means that the whole array ada^{\rightarrow d} can be represented as a sequence $$$$a^{\rightarrow d} = [a_{n - d}, a_{n - d + 1}, \ldots, a_{n - 1}, a_0, a_1, \ldots, a_{n - d - 1}] $$

  2. Then each element of the array a_ia\_i is replaced by a\_i \\,\\&\\, a^{\\rightarrow d}\_i , where \\& is a logical "AND" operator.

For example, if a = \[0, 0, 1, 1\] and d=1d = 1 , then a^{\\rightarrow d} = \[1, 0, 0, 1\] and the value of aa after the first step will be \[0 \\,\\&\\, 1, 0 \\,\\&\\, 0, 1 \\,\\&\\, 0, 1 \\,\\&\\, 1\] , that is \[0, 0, 0, 1\] .

The process ends when the array stops changing. For a given array aa$$, determine whether it will consist of only zeros at the end of the process. If yes, also find the number of steps the process will take before it finishes.

输入格式

The first line contains an integer tt ( 1t10001 \leq t \leq 1000 ) — the number of test cases.

The next 2t2t lines contain descriptions of the test cases.

The first line of each test case description contains two integers: nn ( 1n1061 \le n \le 10^6 ) — array size and dd ( 1dn1 \le d \le n ) — cyclic shift offset. The second line of the description contains nn space-separated integers aia_i ( 0ai10 \le a_i \le 1 ) — elements of the array.

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6 .

输出格式

Print tt lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer — the number of steps after which the array will contain only zeros for the first time. If there are still elements equal to 11 in the array after the end of the process, print -1.

输入输出样例

  • 输入#1

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

    输出#1

    1
    1
    3
    -1
    0

说明/提示

In the third sample test case the array will change as follows:

  1. At the beginning a=[1,1,0,1,0]a = [1, 1, 0, 1, 0] , and a2=[1,0,1,1,0]a^{\rightarrow 2} = [1, 0, 1, 1, 0] . Their element-by-element "AND" is equal to $$$$[1 ,&, 1, 1 ,&, 0, 0 ,&, 1, 1 ,&, 1, 0 ,&, 0] = [1, 0, 0, 1, 0] $$
  2. Now a = \[1, 0, 0, 1, 0\] , then a^{\\rightarrow 2} = \[1, 0, 1, 0, 0\] . Their element-by-element "AND" equals to $$ [1 ,&, 1, 0 ,&, 0, 0 ,&, 1, 1 ,&, 0, 0 ,&, 0] = [1, 0, 0, 0, 0] $$
  3. And finally, when a = \[1, 0, 0, 0, 0\] we get a^{\\rightarrow 2} = \[0, 0, 1, 0, 0\] . Their element-by-element "AND" equals to $$ [1 ,&, 0, 0 ,&, 0, 0 ,&, 1, 0 ,&, 0, 0 ,&, 0] = [0, 0, 0, 0, 0] $$
Thus, the answer is 33 steps.

In the fourth sample test case, the array will not change as it shifts by 22 to the right, so each element will be calculated as 0 \\,\\&\\, 0 or 1 \\,\\&\\, 1$$ thus not changing its value. So the answer is -1, the array will never contain only zeros.

首页