CodeForces 567C Geometric Progression

经历了一番波折之后终于A了,爽, 有收获

产生的疑惑:1. 刚做的时候看了样例怎么也搞不明白第一个样例和第三个样例这不是明显的矛盾吗?后来观察了样例3才明白,等比数列的公比为k的时候,数列元素不一定是看k的倍数。题目理解偏差啊。

2.问了自己一下n为2*100000, 暴力的话能过吗?悬,不妨试一发,TLE,我的预算还是准确的。

3.怎么优化呢。后来想到预处理,学会了用map数据结构保存ai值出现的次数,用的时候直接调用(map的强大哦,可以处理计算很大数据出现的次数,避免了开数     组),处理完成后又数据溢出。

4.改了溢出数据有wrong了,what!!!没想到n和k在乘的时候也会溢出,真是没注意。改了就ok了。

5.此题目让我学会了在CF上看错误数据,高兴啊。

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

Input
5 2
1 1 2 2 4
Output
4
Input
3 1
1 1 1
Output
1
Input
10 3
1 2 6 2 3 6 9 18 3 9
Output
6

Sample Output

Hint

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.


待AC代码(CF又炸了):

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define LOCAL
#define INF 0x3f3f3f3f
#define MAX_N 200000

int rat[MAX_N];
long long l[MAX_N], r[MAX_N];
int main()
{
	#ifdef LOCAL
		freopen("b:\\data.in.txt", "r", stdin);
	#endif
    long long n, k;
    map mp, mpl;
    scanf("%I64d%I64d", &n, &k);
    for(int i = 0; i < n; i++) {
        scanf("%d", &rat[i]);
            mp[rat[i]]++;
    }

//    map::iterator it;
//    for(it = mp.begin(); it != mp.end(); it++)
//        cout << (*it).first << " - " <<(*it).second << endl;
    for(int i = 0; i < n ; i++) {

        l[i] = mpl[rat[i] / k ];
        mpl[rat[i]]++;
        r[i] = mp[rat[i] * k] - mpl[rat[i] * k];

//        cout << l[i] << " " << r[i] << endl;
    }

    long long ans = 0;
    for(int i = 1; i < n - 1; i++) {
        if(rat[i] % k != 0 )
            continue;
        ans += l[i] * r[i];
    }
    cout << ans << endl;

    return 0;
}

你可能感兴趣的:(ACM)