C. Geometric Progression
Time Limit: 2 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/567/problem/C
Description
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.
Sample Input
5 2
1 1 2 2 4
Sample Output
4
HINT
题意
找出 给定序列中 三个数 使得 成等比数列
题解:
枚举中值,map暴力就可以
代码
1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <ctime> 5 #include <iostream> 6 #include <algorithm> 7 #include <set> 8 #include <vector> 9 #include <queue> 10 #include <typeinfo> 11 #include <map> 12 #include <stack> 13 typedef __int64 ll; 14 #define inf 1000000000000 15 using namespace std; 16 inline ll read() 17 { 18 ll x=0,f=1; 19 char ch=getchar(); 20 while(ch<'0'||ch>'9') 21 { 22 if(ch=='-')f=-1; 23 ch=getchar(); 24 } 25 while(ch>='0'&&ch<='9') 26 { 27 x=x*10+ch-'0'; 28 ch=getchar(); 29 } 30 return x*f; 31 } 32 33 //************************************************************************************** 34 map<ll ,ll >mp; 35 map<ll ,ll >mp2; 36 ll a[200005]; 37 int main() 38 { 39 ll n,k; 40 scanf("%I64d%I64d",&n,&k); 41 ll sum=0; 42 ll f1,f2; 43 for(int i=1; i<=n; i++) 44 { 45 scanf("%I64d",&a[i]); 46 if(mp.count(a[i])==0) 47 mp[a[i]]=1; 48 else mp[a[i]]++; 49 } 50 if(mp2.count(a[n])==0) 51 mp2[a[n]]=1; 52 else mp2[a[n]]++; 53 for(int i=n-1; i>1; i--) 54 { 55 if(mp2.count(a[i])==0) 56 mp2[a[i]]=1; 57 else mp2[a[i]]++; 58 ll t; 59 if(a[i]==a[i]*k)t=mp2[a[i]*k]-1; 60 else t=mp2[a[i]*k]; 61 if(a[i]%k==0) 62 sum+=((mp[a[i]/k]-mp2[a[i]/k])*(t)); 63 } 64 cout<<sum<<endl; 65 return 0; 66 }