Codeforces Round #Pi (Div. 2) —— C-Geometric Progression
题意:
如今有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的更多相关文章
- map Codeforces Round #Pi (Div. 2) C. Geometric Progression
题目传送门 /* 题意:问选出3个数成等比数列有多少种选法 map:c1记录是第二个数或第三个数的选法,c2表示所有数字出现的次数.别人的代码很短,思维巧妙 */ /***************** ...
- 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 ...
- 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 ...
- 构造 Codeforces Round #Pi (Div. 2) B. Berland National Library
题目传送门 /* 题意:给出一系列读者出行的记录,+表示一个读者进入,-表示一个读者离开,可能之前已经有读者在图书馆 构造:now记录当前图书馆人数,sz记录最小的容量,in数组标记进去的读者,分情况 ...
- Codeforces Round #Pi (Div. 2)(A,B,C,D)
A题: 题目地址:Lineland Mail #include <stdio.h> #include <math.h> #include <string.h> #i ...
- codeforces Round #Pi (div.2) 567ABCD
567A Lineland Mail题意:一些城市在一个x轴上,他们之间非常喜欢写信交流.送信的费用就是两个城市之间的距离,问每个城市写一封信给其它城市所花费的最小费用和最大的费用. 没什么好说的.直 ...
- Codeforces Round #Pi (Div. 2) ABCDEF已更新
A. Lineland Mail time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #392 (Div. 2) F. Geometrical Progression
原题地址:http://codeforces.com/contest/758/problem/F F. Geometrical Progression time limit per test 4 se ...
- 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 ...
随机推荐
- node.js安装及其环境配置
nodejs: 实际上是采用google的chrome浏览器V8引擎,由C++编写的 本质上是一个javascript的运行环境 浏览器引擎可以解析js代码 nodejs可以解析js代码,没有浏览器端 ...
- Server Tomcat v8.0 Server at localhost failed to start 问题解决方法?
bi编程jsp servlet 第一个程序: HelloServlet 运行错误 404: 十月 28, 2017 11:25:14 上午 org.apache.tomcat.util.digest ...
- 【MySQL】二进制分发安装
操作系统:Red Hat Enterprise Linux Server release 6.5 Mysql安装包:mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz ...
- XML——读与写
XML写入 private static void writeXml() { using (XmlTextWriter xml = new XmlTextWriter(@"C:\Users\ ...
- python调用java API
JPype documentation JPype is an effort to allow python programs full access to java class libraries. ...
- Python语言之变量1(数值,字符串,布尔)
1.数值 整数:2, -2 长整数:2**1024, 2**2048(真的可以很~长~~~,手残算了个2**100000,IDLE还真给打出来了,ORZ) 浮点数:7.05, 1E2(100.0), ...
- 离线安装Selenium
https://blog.csdn.net/poem_ruru/article/details/79032140
- python读取单个文件操作
python读取单个文件,参考<笨方法学python>的第15节. 运行方式是采用:python python文件名 要读取的文件名 代码中 script, filename = argv ...
- k[原创]Faster R-CNN论文翻译
物体检测论文翻译系列: 建议从前往后看,这些论文之间具有明显的延续性和递进性. R-CNN SPP-net Fast R-CNN Faster R-CNN Faster R-CNN论文翻译 原文地 ...
- (转)Hibernate中的多表操作
http://blog.csdn.net/yerenyuan_pku/article/details/70556208 Hibernate中的多表操作 在实际开发中,我们不可能只是简简单单地去操作单表 ...