题意:

如今有n个数,然后给出一个数k(代表的是等比数列中的那个公比),然后第二行给出n个数,代表的是这个序列。

最后的问题是叫你找出在这个序列中满足公比为k的三个数有几种。并输出方案总数。

思路:

这道题非常巧妙啊,用到了map。

首先我们先记录下每种数出现过了几次。这里由于数太大了。直接用数组存会爆掉,所以改用map。

我们须要两个map,分别记为mp1。mp2.

然后在for的过程中。我们是以当前的那个数为第二项,然后去寻找它的mp1[a[i]*k](也就是第三项)。寻找它的mp2[a[i]/k](也就是第一项)。

这里为什么先找第一项,然后第二,三项呢?用样例来说吧:1 1 1; 由于假设我们找了第一项。那么最后的结果为2(也就是1出现过的次数)。所以这样的解法是不正确的。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<stack>
using namespace std;
#define me rng_58
#define maxn 200011
typedef __int64 ll;
ll a[maxn];
map<ll,ll> mp1,mp2;
int main(){
int n,k;
ll sum=0;
scanf("%d%d",&n,&k);
mp1.clear();
mp2.clear();
for(int i=1;i<=n;i++){
scanf("%I64d",&a[i]);
mp1[a[i]]++;
}
//以a[i]为中间项;
//mp1记录的是a[i]后面有几个。mp2记录a[i]前面有几个;
for(int i=1;i<=n;i++){
mp1[a[i]]--;
if(a[i]%k==0) //!
sum+=mp1[a[i]*k]*mp2[a[i]/k];
mp2[a[i]]++;
}
printf("%I64d\n",sum);
}
/*
4 1
3 3 3 3
*/

Codeforces Round #Pi (Div. 2) —— C-Geometric Progression的更多相关文章

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

    题目传送门 /* 题意:问选出3个数成等比数列有多少种选法 map:c1记录是第二个数或第三个数的选法,c2表示所有数字出现的次数.别人的代码很短,思维巧妙 */ /***************** ...

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

    C. Geometric Progression Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

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

    C. Geometric Progression time limit per test 1 second memory limit per test 256 megabytes input stan ...

  4. 构造 Codeforces Round #Pi (Div. 2) B. Berland National Library

    题目传送门 /* 题意:给出一系列读者出行的记录,+表示一个读者进入,-表示一个读者离开,可能之前已经有读者在图书馆 构造:now记录当前图书馆人数,sz记录最小的容量,in数组标记进去的读者,分情况 ...

  5. Codeforces Round #Pi (Div. 2)(A,B,C,D)

    A题: 题目地址:Lineland Mail #include <stdio.h> #include <math.h> #include <string.h> #i ...

  6. codeforces Round #Pi (div.2) 567ABCD

    567A Lineland Mail题意:一些城市在一个x轴上,他们之间非常喜欢写信交流.送信的费用就是两个城市之间的距离,问每个城市写一封信给其它城市所花费的最小费用和最大的费用. 没什么好说的.直 ...

  7. Codeforces Round #Pi (Div. 2) ABCDEF已更新

    A. Lineland Mail time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...

  8. Codeforces Round #392 (Div. 2) F. Geometrical Progression

    原题地址:http://codeforces.com/contest/758/problem/F F. Geometrical Progression time limit per test 4 se ...

  9. Codeforces Round #Pi (Div. 2) D. One-Dimensional Battle Ships set乱搞

    D. One-Dimensional Battle ShipsTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...

随机推荐

  1. python常见的加密方式

    1.前言 我们所说的加密方式都是对二进制编码的格式进行加密,对应到python中,则是我妈们的bytes. 所以当我们在Python中进行加密操作的时候,要确保我们的操作是bytes,否则就会报错. ...

  2. GitLab Runner and CICD

    # Linux x86-64 sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaw ...

  3. sql 列集合转换成逗号分隔的字符类型

    CREATE function [dbo].[getGroupPath](@groupId int) returns nvarchar(2000) as begin declare @path nva ...

  4. 【1】Jdk1.8中的HashMap实现原理

    HashMap概述 HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特别是它不保证该顺序恒久不变. 内部实现 ...

  5. no斜体 背景图片坐标

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. Android项目实战_手机安全卫士splash界面

    - 根据代码的类型组织包结构 1. 界面 com.hb.mobilesafe.activities 2. 服务 com.hb.mobilesafe.services 3. 业务逻辑 com.hb.mo ...

  7. Android Thermal-engine

    Thermal Engine Thermal 相关的东西主要在Vendor/qcom/proprietary/thermal-engine 目录下: thermal-engine.conf 文件可以用 ...

  8. spring编程框架

    spring boot, spring data, spring framework spring / spring boot @Profile('prod'|'dev'|'other')(伴随@Be ...

  9. 使用Hexo搭建个人博客配置全过程

    大致过程分为: 1.搭建Node.js 环境 2. 搭建Git 环境 3.安装配置Hexo 4.GitHub 注册和配置 5. 关联Hexo 与 GitHub Pages 7.Hexo的常用操作 下面 ...

  10. JS——input标签注册事件

    注意:淘宝的lable是用定位制作的,事件是oninput事件 <!DOCTYPE html> <html> <head lang="en"> ...