Codeforces 567C Geometric Progression

本文介绍了解决特定几何级数问题的算法。通过固定中间元素并使用两个关联数组来计算序列数量,实现复杂度优化。示例代码展示了算法实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Geometric Progression

题目链接:

http://codeforces.com/problemset/problem/567/C

解题思路:

Let's solve this problem for fixed middle element of progression. This means that if we fix element ai then the progression must consist ofai / k and ai·k elements. It could not be possible, for example, if ai is not divisible by k ().

For fixed middle element one could find the number of sequences by counting how many ai / k elements are placed left from fixed element and how many ai·k are placed right from it, and then multiplying this numbers. To do this, one could use two associative arrays Al and Ar, where for each key x will be stored count of occurences of x placed left (or right respectively) from current element. This could be done with map structure.

Sum of values calculated as described above will give the answer to the problem.

AC代码:

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;

typedef long long ll;
map<ll,ll> L,R,num;
ll a[200005];


int main(){
    int n,k;
    while(~scanf("%d%d",&n,&k)){
        ll sum = 0;
        L.clear();R.clear();
        for(int i = 0; i < n; i++){
            scanf("%lld",&a[i]);
            R[a[i]]++;
        }
        for(int i = 0; i < n; i++){
            R[a[i]]--;
            if(a[i] % k == 0)
                sum += L[a[i]/k]*R[a[i]*k];
            L[a[i]]++;
        }
        printf("%lld\n",sum);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值