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

来源:互联网 发布:ubuntu 反应慢 编辑:程序博客网 时间:2024/06/08 01:21

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 21 1 2 2 4
output
4
input
3 11 1 1
output
1
input
10 31 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;}




0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 保温杯撞 凹了怎么办 乐扣微波打不开怎么办 乐扣密封条发霉怎么办 橡胶牙刷柄发霉怎么办 乐扣密封条发黑怎么办 喝了姜枣茶发热怎么办 老干妈盖子太紧打不开怎么办 酵素桶变色了怎么办 酵素比例没弄好怎么办 离合器弹不起来怎么办 货车打不上气干燥器漏气怎么办 排气阀一直漏气怎么办 五个月宝宝长牙怎么办 宝宝不喜欢吃粥怎么办 孕晚期严重贫血怎么办 小孩子特别瘦该怎么办 胃病越来越瘦该怎么办 宝宝不愿吃米粉怎么办 米粉吃凉了怎么办 夏季给宝宝断奶怎么办 宝宝长牙牙痒怎么办 宝宝不喜欢吃肉怎么办 孩子不喜欢吃肉怎么办 小孩不喜欢吃肉怎么办 戒奶第二天很痛怎么办 宝宝不吃磨牙棒怎么办 金毛急性肠炎怎么办 狗狗病毒性肠炎怎么办 狗呕吐白色泡沫怎么办 甲减引起水肿怎么办 猫咪呕吐加拉稀怎么办 猫一直吐白沫怎么办 小猫吐白沫拉稀怎么办 大鳄不吃泥鳅怎么办 六个月宝宝不消化怎么办 两周岁宝宝拉稀怎么办 三岁宝宝拉肚子怎么办 混合痔老是出血怎么办 宝宝大便黑色怎么办呢 婴儿大便有粘液怎么办? 十个半月宝宝拉肚子怎么办