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.
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 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.
5 21 1 2 2 4
4
3 11 1 1
1
10 31 2 6 2 3 6 9 18 3 9
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)
- Codeforces Round #Pi (Div. 2) C. Geometric Progression dp
- Codeforces Round #Pi (Div. 2) C. Geometric Progression
- C. Geometric Progression- Codeforces Round #Pi (Div. 2)-
- Codeforces Round #Pi (Div. 2) —— C-Geometric Progression
- Codeforces Round #Pi (Div. 2)567C Geometric Progression(map暴力)
- Codeforces Round #Pi (Div. 2)567C Geometric Progression(dp + map)
- CodeForces #Pi C.Geometric Progression
- codeforces 567C Geometric Progression (map维护)
- CodeForces 567C Geometric Progression【思维+map】
- Geometric Progression----(BestCoder Round #54 (div.2))
- codeforce 567c Geometric Progression(map)
- CodeForces 567C. Geometric Progression(map 数学啊)
- codeforces(567C)-- Geometric Progression
- codeforces 567C Geometric Progression
- Codeforces 567C Geometric Progression
- CodeForces 567C Geometric Progression
- CodeForces 567C Geometric Progression
- ServerSocket 、Socket 创建Java服务器与客户端以JSON格式聊天
- Maven学习
- 很详细的:java中文数字转换成阿拉伯数字
- 2015 Multi-University Training Contest 5(hdu5343 - 5352)网络流+yy+(哈夫曼树+dp)
- Protocol - 2
- Codeforces Round #Pi (Div. 2) C. Geometric Progression (map)
- UVALive - 3661 Animal Run (平面图+最小割+对偶图+最短路)
- 9.5位操作(二)——给定一个介于0和1之间的实数,类型为double,打印它的二进制表示
- C++标准模板库STL算法与自适应容器(栈和队列)
- Android学习第六弹之Touch事件的处理
- ios各种动画效果
- SVG -椭圆曲线简介
- poj 2245 Lotto
- popupwindow的源码分析