2732.Geometric Progression

Time Limit: 1s Memory Limit: 256MB

Polycarp loves geometric progressions very much. Since he was only three years old, he loves only the progressions of length three. He also has a favorite integer k and a sequence a, consisting of n integers.

He wants to know how many subsequences of length three can be selected from a, so that they form a geometric progression with common ratio k.

A subsequence of length three is a combination of three such indexes i1,i2,i3, that 1 \le i1 \lt i2 \lt i3 \le n. That is, a subsequence of length three are such groups of three elements that are not necessarily consecutive in the sequence, but their indexes are strictly increasing.

A geometric progression with common ratio k is a sequence of numbers of the form b \cdot k0,b \cdot k1,...,b \cdot kr-1.

Polycarp is only three years old, so he can not calculate this number himself. Help him to do it.

Input Format(From the terminal/stdin)

The first line of the input contains two integers, n and k (1 \le n,k \le 2 \cdot 105), showing how many numbers Polycarp's sequence has and his favorite number.

The second line contains n integers a1,a2,...,an (-109 \le ai \le 109) - elements of the sequence.

Output Format(To the terminal/stdout)

Output a single number - the number of ways to choose a subsequence of length three, such that it forms a geometric progression with a common ratio k.

Sample Input 1

Copy
5 2
1 1 2 2 4
 · \n
 · · · · \n

Sample Output 1

Copy
4
 \n

Sample Input 2

Copy
3 1
1 1 1
 · \n
 · · \n

Sample Output 2

Copy
1
 \n

Sample Input 3

Copy
10 3
1 2 6 2 3 6 9 18 3 9
  · \n
 · · · · · · ·  · · \n

Sample Output 3

Copy
6
 \n

Hints

In the first sample test the answer is four, as any of the two 1s can be chosen as the first element, the second element can be any of the 2s, and the third element of the subsequence must be equal to 4.

Submit

请先 登录

© 2025 FAQs