# Codeforces Round #Pi (Div. 2) C. Geometric Progression

```public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
int p = in.readInt();
IntPair[] nums = new IntPair[n];
for (int i = 0; i < nums.length; i++) {
nums[i] = new IntPair(in.readInt(),i);
}
Arrays.sort(nums);
int count = 0;
for (int i = 1; i < nums.length-1; i++) {
int j = i - 1;
int k = i + 1;
while (j >= 0 && k < nums.length) {
if (nums[j].first * nums[k].first > nums[i].first*nums[i].first)
j--;
else if (nums[j].first * nums[k].first < nums[i].first*nums[i].first)
k++;
else {
if ((nums[i].first/nums[j].first) != p && (nums[k].first/nums[i].first) != p)
continue;
count++;
while (j > 0 && nums[j].first == nums[j - 1].first && nums[j].second < nums[i].second && nums[i].second <nums[k].second) {
j--;
count++;
}
while (k < nums.length - 1 && nums[k].first == nums[k + 1].first && nums[j].second < nums[i].second && nums[i].second <nums[k].second) {
k++;
count++;
}
j--;
k++;
}
}
}
out.print(count);
}
}```

```public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
int p = in.readInt();
Map<Long,Long> l = new HashMap<>();
Map<Long,Long> r = new HashMap<>();
long res = 0;
long[] nums = new long[n];
for (int i = 0; i < n; i++) {
nums[i] = in.readLong();
if (r.containsKey(nums[i]))
r.put(nums[i], r.get(nums[i])+1);
else
r.put(nums[i],1l);
l.put(nums[i],0l);
}
for (int i = 0; i < n; i++) {
r.put(nums[i],r.get(nums[i])-1l);
if (nums[i] % p == 0){
if (l.containsKey(nums[i]/p) && r.containsKey(nums[i]*p))
res += l.get(nums[i]/p) * r.get(nums[i]*p);
}
l.put(nums[i],l.get(nums[i])+1l);
}
out.print(res);
}```