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 ...
随机推荐
- 解决eclipse用maven install打包报错问题:-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.
1.添加M2_HOME的环境变量 2.Preference->Java->Installed JREs->Edit 选择一个jdk, 添加 -Dmaven.multiModuleP ...
- Android:EditText属性大全
一.inputType属性inputType属性在EditText输入值时启动的虚拟键盘的风格有着重要的作用.比如有时需要虚拟键盘只为字符或只为数字. <span style="fon ...
- 316 Remove Duplicate Letters 去除重复字母
给定一个仅包含小写字母的字符串,去除重复的字母使得所有字母出现且仅出现一次.你必须保证返回结果是所有可能结果中的以字典排序的最短结果.例如:给定 "bcabc"返回 "a ...
- Unity学习-预制(四)
预制即克隆 比如要模拟一个下雨的场景,天下掉下来一颗一颗的雨滴.如果此时,我们给每一个雨滴创建一个对象的话,那会很浪费资源,而且也没必要,因为所有的雨滴是相同的.这个时候就使用到了预制,一种可以被重复 ...
- Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'empid' in 'class cn.happy.entity.Emp'
org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.apache.i ...
- 【java并发容器】并发容器之CopyOnWriteArrayList
原文链接: http://ifeve.com/java-copy-on-write/ Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容 ...
- Android彻底组件化方案实践
本文提出的组件化方案demo已经开源,参见文章Android彻底组件化方案开源. 文末有罗辑思维"得到app"的招聘广告,欢迎各路牛人加入!! 一.模块化.组件化与插件化 项目发展 ...
- UVALive 3026(KMP算法)
UVALive 3026 KMP中next[]数组的应用: 题意:给出一个字符串,问该字符串每个前缀首字母的位置和该前缀的周期. 思路:裸KMP直接上就是了: 设该字符串为str,str字符串 ...
- 二、Scrapy命令行工具
本文转载自以下链接:https://scrapy-chs.readthedocs.io/zh_CN/latest/topics/commands.html Scrapy是通过 scrapy 命令行工具 ...
- shell 读取目录指定文件并截取拼接
shell脚本读取指定文件并拼接成指定的版本信息