Codeforces 567C - Geometric Progression - [map维护]

题目链接:https://codeforces.com/problemset/problem/567/C

 

题意:

给出长度为 $n$ 的序列 $a[1:n]$,给出公比 $k$,要求你个给出该序列中,长度为 $3$ 的等比子序列的数目。

 

题解:

首先倒着遍历,用map记录曾经出现过的每个数字的出现次数,然后再用另一个map来记录曾经出现过的所有满足 $(x,kx)$ 的二元组的数目,最后就直接维护答案即可。

 

AC代码:

#include<bits/stdc++.h>
#define IO (ios::sync_with_stdio(0),cin.tie(0),cout.tie(0))
#define mk make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const int maxn=2e5+10;
int n;
ll k,a[maxn];
map<ll,ll> mp1;
map<P,ll> mp2;

int main()
{
    IO;

    cin>>n>>k;

    ll mx=-1e10, mn=1e10;
    for(int i=1;i<=n;i++)
        cin>>a[i], mx=max(a[i],mx), mn=min(a[i],mn);

    ll ans=0;
    for(int i=n;i>=1;i--)
    {
        if(mn/(k*k)<=a[i] && a[i]<=mx/(k*k))
            ans+=mp2[mk(a[i]*k,a[i]*k*k)];

        if(mn/k<=a[i] && a[i]<=mx/k)
            mp2[mk(a[i],a[i]*k)]+=mp1[a[i]*k];

        mp1[a[i]]++;
    }
    cout<<ans<<endl;
}

 

posted @ 2019-05-02 10:55  Dilthey  阅读(231)  评论(0编辑  收藏  举报