CF BUDDY
← Problems·

1978F · Large Graph

2400 · data structures, dfs and similar, dsu

Problem: Given an array aa of length nn. Let's construct a square matrix bb of size n×nn \times n, in which the ii-th row contains the array aa cyclically shifted to the right by (i1)(i - 1). For example, for the array a=[3,4,5]a = [3, 4, 5], the obtained matrix is

b=[345534453]b = \begin{bmatrix} 3 & 4 & 5 \\ 5 & 3 & 4 \\ 4 & 5 & 3 \end{bmatrix}

Let's construct the following graph:

  • The graph contains n2n^2 vertices, each of which corresponds to one of the elements of the matrix. Let's denote the vertex corresponding to the element bi,jb_{i, j} as (i,j)(i, j).
  • We will draw an edge between vertices (i1,j1)(i_1, j_1) and (i2,j2)(i_2, j_2) if i1i2+j1j2k|i_1 - i_2| + |j_1 - j_2| \le k and gcd(bi1,j1,bi2,j2)>1\gcd(b_{i_1, j_1}, b_{i_2, j_2}) > 1, where gcd(x,y)\gcd(x, y) denotes the greatest common divisor of integers xx and yy.

Your task is to calculate the number of connected components^{\dagger} in the obtained graph.

^{\dagger}A connected component of a graph is a set of vertices in which any vertex is reachable from any other via edges, and adding any other vertex to the set violates this rule.

Input Format: Each test consists of multiple test cases. The first line contains a single integer tt (1t1051 \leq t \leq 10^5) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn and kk (2n1062 \le n \le 10^6, 2k21062 \le k \le 2 \cdot 10^6) — the length of the array and the parameter kk.

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai1061 \le a_i \le 10^6) — the elements of the array aa.

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

Output Format: For each test case, output a single integer — the number of connected components in the obtained graph.

Note: In the first test case, the matrix bb is given in the statement. The first connected component contains the vertices (1,1)(1, 1), (2,2)(2, 2), and (3,3)(3, 3). The second connected component contains the vertices (1,2)(1, 2), (2,3)(2, 3), and (3,1)(3, 1). The third connected component contains the vertices (1,3)(1, 3), (2,1)(2, 1), and (3,2)(3, 2). Thus, the graph has 33 connected components.

In the second test case, the following matrix is obtained:

b=[349934493]b = \begin{bmatrix} 3 & 4 & 9 \\ 9 & 3 & 4 \\ 4 & 9 & 3 \end{bmatrix}

The first connected component contains all vertices corresponding to elements with values 33 and 99. The second connected component contains all vertices corresponding to elements with the value 44.

In the fourth test case, all vertices are in one connected component.

Sample Cases

Case 1

Input

6
3 3
3 4 5
3 3
3 4 9
3 2
3 4 9
2 2
2 8
5 3
8 27 5 4 3
4 10
2 2 2 2

Output

3
2
3
1
4
1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback