Codeforces 567C:Geometric Progression(DP)
time limit per test : 1 second
memory limit per test : 256 megabytes
input : standard input
output : standard output
Problem Description
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 \(i_1, i_2, i_3\), that \(1 ≤ i_1 < i_2 < i_3 ≤ 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·k^0, b·k^1, ..., b·k^{r - 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·10^5)\), showing how many numbers Polycarp's sequence has and his favorite number.
The second line contains \(n\) integers \(a_1, a_2, ..., a_n ( - 10^9 ≤ a_i ≤ 10^9)\) — 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\).
Examples
input
5 2
1 1 2 2 4
output
4
input
3 1
1 1 1
output
1
input
10 3
1 2 6 2 3 6 9 18 3 9
output
6
Note
In the first sample test the answer is four, as any of the two 1s can be chosen as the first element, the second element can be any of the 2s, and the third element of the subsequence must be equal to 4.
题意
在\(n\)个数中选出三个数,要求组成的序列公比为\(k\),并且不改变这三个数的前后关系,问有多少种选择方案
思路
\(DP\),因为\(- 10^9 ≤ a_i ≤ 10^9\) ,所以还需要用到\(map\)
定义\(dp[i][j]\)表示把数字\(j\)放在等比数列第\(i\)个位置的方案数
\(dp[i][j]+=dp[i-1][j/k]\)
将所有长度为\(3\)的方案数加起来即可
代码
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+10;
const int mod=1e9+7;
const int maxm=1e3+10;
using namespace std;
ll a[maxn];
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("/home/wzy/in", "r", stdin);
freopen("/home/wzy/out", "w", stdout);
srand((unsigned int)time(NULL));
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n,k;
cin>>n>>k;
map<int,ll>dp[4];
ll ans=0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
if(a[i]%k==0)
{
ans+=dp[2][a[i]/k];
dp[2][a[i]]+=dp[1][a[i]/k];
}
dp[1][a[i]]++;
}
cout<<ans<<endl;
#ifndef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<" s."<<endl;
#endif
return 0;
}
Codeforces 567C:Geometric Progression(DP)的更多相关文章
- CodeForces - 24D :Broken robot (DP+三对角矩阵高斯消元 随机)
pro:给定N*M的矩阵,以及初始玩家位置. 规定玩家每次会等概率的向左走,向右走,向下走,原地不动,问走到最后一行的期望.保留4位小数. sol:可以列出方程,高斯消元即可,发现是三角矩阵,O(N* ...
- Codeforces Gym101201B:Buggy Robot(BFS + DP)
题目链接 题意 给出一个n*m的地图,还有一个操作序列,你原本是要按照序列执行操作的,但是你可以修改操作:删除某些操作或者增加某些操作,问从'R'到'E'最少需要多少次修改操作. 思路 和上次比赛做的 ...
- hdu 5429 Geometric Progression(存个大数模板)
Problem Description Determine whether a sequence is a Geometric progression or not. In mathematics, ...
- 习题:烽火传递(DP+单调队列)
烽火传递[题目描述]烽火台又称烽燧,是重要的防御设施,一般建在险要处或交通要道上.一旦有敌情发生,白天燃烧柴草,通过浓烟表达信息:夜晚燃烧干柴,以火光传递军情.在某两座城市之间有n个烽火台,每个烽火台 ...
- Codeforces 567C Geometric Progression(思路)
题目大概说给一个整数序列,问里面有几个包含三个数字的子序列ai,aj,ak,满足ai*k*k=aj*k=ak. 感觉很多种做法的样子,我想到这么一种: 枚举中间的aj,看它左边有多少个aj/k右边有多 ...
- CodeForces 567C Geometric Progression 类似dp的递推统计方案数
input n,k 1<=n,k<=200000 a1 a2 ... an 1<=ai<=1e9 output 数组中选三个数,且三个数的下标严格递增,凑成形如b,b*k,b* ...
- CodeForces - 1073E :Segment Sum (数位DP)
You are given two integers l l and r r (l≤r l≤r ). Your task is to calculate the sum of numbers from ...
- Codeforces Gym100962J:Jimi Hendrix(树型DP)
http://codeforces.com/gym/100962/attachments 题意:有一个n个节点的字母树,给出n-1条边的信息,代表边上有一个字母,然后给出长度为m的字符串,问是否能在这 ...
- Codeforces 189A:Cut Ribbon(完全背包,DP)
time limit per test : 1 second memory limit per test : 256 megabytes input : standard input output : ...
随机推荐
- 取gridview中textbox的值【C#】
<asp:GridView ID="gridView" runat="server" OnRowCommand="gridView_RowCom ...
- 为什么重写equals必须重写hashCode
目录 equals常见面试题 为什么要重写equals 重写equals不重写hashCode会存在什么问题 总结 equals常见面试题 在开始聊之前,我们先看几个常见的面试题,看看你能不能都回答上 ...
- 大数据学习day33----spark13-----1.两种方式管理偏移量并将偏移量写入redis 2. MySQL事务的测试 3.利用MySQL事务实现数据统计的ExactlyOnce(sql语句中出现相同key时如何进行累加(此处时出现相同的单词))4 将数据写入kafka
1.两种方式管理偏移量并将偏移量写入redis (1)第一种:rdd的形式 一般是使用这种直连的方式,但其缺点是没法调用一些更加高级的api,如窗口操作.如果想更加精确的控制偏移量,就使用这种方式 代 ...
- 哪里可以下载支付宝demo或者sdk
http://club.alipay.com/read-htm-tid-9976972.html 这里有所有的demo和sdk包括移动产品的demo.在他的论坛里面呢 真心恶心啊.不放到主页.
- 【Linux】【Services】【SaaS】 kubeadm安装kubernetes
1. 简介 2. 环境 2.1. OS: CentOS Linux release 7.5.1804 (Core) 2.2. Ansible: 2.6.2-1.el7 2.3. docker: 2. ...
- 【Linux】【Services】【DNS】bind基础
1. 概念 1.1. DNS: Domain Name Service, 应用层协议,占用53/udp, 53/tcp 1.2. tld(顶级域):Top Level Domain 组织域:.com, ...
- 【Linux卷管理】LVM原理
LVM 简介 每个Linux使用者在安装Linux时 都会遇到这样的困境:在为系统分区时,如何精确评估和分配各个硬盘分区的容量,因为系统管理员不但要考虑到当前某个分区需要的容量,还要预见该分区以后可能 ...
- windows下安装linux虚拟机(wsl2),并安装docker。
一.windows terminal(重要工具,但也可以不装) 这是微软官方推荐的终端工具,类似mac的iterm2,可同时开启多个终端,最开始默认有power shall,cmd,可下载gsudo集 ...
- dart系列之:实时通讯,在浏览器中使用WebSockets
目录 简介 dart:html中的WebSockets 创建一个WebSocket WebSocket的状态 发送消息 处理WebSocket事件 总结 简介 web客户端和服务器端通信有两种方式,一 ...
- 项目管理的基本概念(Project)
<Project2016 企业项目管理实践>张会斌 董方好 编著 关于项目管理的基本概念,我看了好久,也迷糊了好久--原谅我实在不是个善于理解概念的妖,最终我决定,就记些简单的东东吧,具体 ...