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

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 test(s)
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


题意

     找出 给定序列中 三个数 使得 成等比数列

题解

      枚举中值,map暴力就可以


//提议:在一个串中找三个等比数字。
//从中间开始枚举 



#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
int main()
{
	int n,m,i,j;
	long long ans;
	while(scanf("%d%d",&n,&m)!=EOF){
		ans=0;
		map<long long,long long>a;
		map<long long,long long>pre;
		map<long long,long long>count;
		for(i=1;i<=n;i++) cin>>a[i];
		for(i=1;i<=n;i++) count[a[i]]++;
		for(i=1;i<=n;i++){
			count[a[i]]--;
			if(a[i]%m==0 && count[a[i]*m]) ans+=pre[a[i]/m]*count[a[i]*m];
			pre[a[i]]++;
		}
		printf("%lld\n",ans);
	}
	return 0;
}

DP思想
#include <bits/stdc++.h>
using namespace std;

int n;
long long k;

long long tab[1000007];

long long wyn;

map <long long,long long> mapa1;
map <long long,long long> mapa2;

int main()
{
    scanf("%d%lld", &n, &k);
    for (int i=1; i<=n; i++)
    {
        scanf("%lld", &tab[i]);
        if (!(tab[i]%(k*k)))
        wyn+=mapa2[tab[i]/k];
        if (!(tab[i]%k))
        mapa2[tab[i]]+=mapa1[tab[i]/k];
        mapa1[tab[i]]++;
    }
    printf("%lld\n", wyn);
    return 0;
}




你可能感兴趣的:(Codeforces Round #Pi (Div. 2) C. Geometric Progression (map))