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

题目连接:https://codeforces.com/problemset/problem/567/Cios

 

题意:c++

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

 

题解:code

首先倒着遍历,用map记录曾经出现过的每一个数字的出现次数,而后再用另外一个map来记录曾经出现过的全部知足 $(x,kx)$ 的二元组的数目,最后就直接维护答案便可。blog

 

AC代码:ci

#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;
}
相关文章
相关标签/搜索