题意:

如今有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. Unity3d dotween

    位置 1. 移动到指定位置 obj.transform.DOMove(, , ), 2f); a. 单方向可以用DOMoveX.DOMoveY.DOMoveZ b. 本地坐标系版本:DOLocalMo ...

  2. JavaScript中的 函数splice() 的使用。

    大二接触JavaScript初期,学习函数中有一道题: 定义一个2个参数的函数.第1个参数是一个数组,第2个参数是需要删除的元素.函数功能,在第1个实参数组中查找第2个实参提供的值,找到则删除该元素( ...

  3. Android文件操作报open failed: EBUSY (Device or resource busy)

    Android删除文件后重新创建时偶尔出现 open failed: EBUSY (Device or resource busy)错误,该错误是Android系统的一个bug,大概的意思类似于win ...

  4. 查看/进入mac根目录的方式

    1.通过“前往文件夹”快捷键组合 (1)打开finder,点击上部菜单栏“前往”,然后“个人”,直接跳转. (2)快捷键组合:command + shift + G:注意:打开finder后,再快捷键 ...

  5. Qt 窗体间传值(代码备份)

    刚开始看的时候看的云里雾里的,现在稍微明白一点了.现在假设有一个form,一个MainWindow,如图所示: 实现点击PushButton,将文本框中的内容传输到MainWindow中,显示为Lab ...

  6. (转) Hibernate检索方式概述

    http://blog.csdn.net/yerenyuan_pku/article/details/70554816 Hibernate检索方式概述 我们在对数据库的操作中,最常用的是select, ...

  7. jquery 实现 单选框点击取消

    <label for="1" class="z-label"> <input type="radio" class=&qu ...

  8. react typescript 父组件调用子组件

    //父组件import * as React from 'react'import { Input } from 'antd'const Search = Input.Searchimport &qu ...

  9. 27.7 并行语言集成查询(PLinq)

    static void Main() { ObsoleteMethods(Assembly.Load("mscorlib.dll")); Console.ReadKey(); } ...

  10. JavaScript day4(逻辑运算符)

    逻辑运算符 逻辑运算符用于测定变量或值之间的逻辑 逻辑与运算符:&&.同时满足(and). 通过if语句的嵌套来实现: if (num > 5) { if (num < 1 ...