PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000023. Đồ thị khổng lồ (Large Graph)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đồ thị khổng lồ (Large Graph)

Large Graph

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array of positive integers a1,a2,…,ana_1,a_2,\ldots,a_n and an integer kk. Construct an n×nn\times n matrix bb by circular shifts: the first row is aa, the second row is aa shifted one position to the right, the third row is shifted two positions, and so on. An element moving past the right end reappears at the left end.

Consider an undirected graph with one vertex for each matrix cell, thus n2n^2 vertices in total. Two different cells (i1,j1)(i_1,j_1) and (i2,j2)(i_2,j_2) are joined by an edge if both ∣i1−i2∣+∣j1−j2∣≤k|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. The distance is ordinary Manhattan distance on matrix coordinates; it does not wrap across matrix boundaries.

Determine the number of connected components of this graph. Only the count is required; you do not need to output the components.

Input

The first line contains integers n,kn,k. The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n.

Output

Print one integer: the number of connected components.

Subtasks

  • Subtask 1 (20 points): 2≤n≤302\le n\le30, 2≤k≤602\le k\le60, 1≤ai≤10001\le a_i\le1000.
  • Subtask 2 (30 points): 2≤n≤50002\le n\le5000, 2≤k≤1042\le k\le10^4, 1≤ai≤1061\le a_i\le10^6.
  • Subtask 3 (50 points): 2≤n≤1062\le n\le10^6, 2≤k≤2⋅1062\le k\le2\cdot10^6, 1≤ai≤1061\le a_i\le10^6.

Examples

Input

3 3
3 4 9

Output

2

Explanation

The matrix consists of cyclic shifts of [3,4,9][3,4,9]. Cells holding 33 and 99 are connected through steps with gcd greater than one; cells holding 44 form the other component. The answer is 22.