C. Geometric Progression
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 ≤ i1 < i2 < i3 ≤ 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·k0, b·k1, ..., b·kr - 1.
Polycarp is only three years old, so he can not calculate this number himself. Help him to do it.
Input
The first line of the input contains two integers, n and k (1 ≤ n, k ≤ 2·105), showing how many numbers Polycarp's sequence has and his favorite number.
The second line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — elements of the sequence.
Output
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.
Examples
input
Copy
5 2 1 1 2 2 4
output
Copy
4
input
Copy
3 1 1 1 1
output
Copy
1
input
Copy
10 3 1 2 6 2 3 6 9 18 3 9
output
Copy
6
Note
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.
给出一系列数,问能找出多少个数ai aj ak,i<j<k,使得其成为等比数列,比为p。
先统计每个数出现的次数,利用m2数组,然后枚举三个数之间的那个数,当到中间那个数v时,看v/p出现了几次,利用m1数组,m1数组每次求答案时才统计某个数出现的位置,同时求解答案时m2数组减去-,消除前面的数产生的影响。
从而得到前面的数v/p和后面的数v*p出现的准确次数。相乘得到答案。
#include <algorithm> //STL通用算法
#include <bitset> //STL位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex> //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque> //STL双端队列容器
#include <exception> //异常处理类
#include <fstream>
#include <functional> //STL定义运算函数(代替运算符)
#include <limits>
#include <list> //STL线性列表容器
#include <map> //STL 映射容器
#include <iomanip>
#include <ios> //基本输入/输出支持
#include<iosfwd> //输入/输出系统使用的前置声明
#include <iostream>
#include <istream> //基本输入流
#include <ostream> //基本输出流
#include <queue> //STL队列容器
#include <set> //STL 集合容器
#include <sstream> //基于字符串的流
#include <stack> //STL堆栈容器
#include <stdexcept> //标准异常类
#include <streambuf> //底层输入/输出支持
#include <string> //字符串类
#include <utility> //STL通用模板类
#include <vector> //STL动态数组容器
#include <cwchar>
#include <cwctype>
#define ll long long
using namespace std;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 200000+66;
const ll mod=1e9+7;
int n,k;
map<ll,ll> m1;
map<ll,ll> m2;
ll a[maxn];
int main()
{
scanf("%d %d", &n,&k);
for(ll i=1; i<=n; i++)
{
ll v;
scanf("%lld",&a[i]);
m2[a[i]]++;
}
ll ans=0;
for(int i=1;i<n;i++)
{
ll v=a[i];
m2[a[i]]--;
if(v%k==0)
{
ans+=m1[v/k]*m2[v*k];
}
m1[a[i]]++;
}
printf("%lld\n",ans);
return 0;
}