题目链接:http://codeforces.com/contest/567/problem/C

题意就是有n个数现在要让 ai aj  ak 构成公比为K的等比数列(i < j < k);求有多少种组合方法;

我们可以对 a[i] 找 a[i]/k 和 a[i]*k ,设a[i]前面有 x个a[i]/k ;后面有y个 a[i]*k ;那么答案就是所有的x*y的和;由于数据比较大无法引用下标, 所以用map离散一下

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <map>
#include <string>
using namespace std;
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
#define N 200005
typedef long long LL; int main()
{
int n; LL k, a[N]; while(scanf("%d %I64d", &n, &k)!=EOF)
{
map<LL, LL> mp1, mp2; for(int i=; i<=n; i++)
{
scanf("%I64d", &a[i]); mp1[a[i]]++;
} LL ans = ; for(int i=n; i>=; i--)///倒着来便于统计mp2[x]的个数,即倒着来的数的个数;
{
mp1[a[i]] --;///正着来的要减减; if(a[i]%k==) ans += mp1[a[i]/k] * mp2[a[i]*k]; mp2[a[i]] ++;
} printf("%I64d\n", ans);
}
return ;
}

这是一个用二分写的,不用map离散化的代码;

LL a[maxn], b[maxn], l[maxn], r[maxn], n, m;
LL bin_sreach (LL x)
{
LL high = m-, low = ;
while (high >= low)
{
LL mid = (high + low) / ;
if (b[mid] == x)
return mid;
if (b[mid] < x)
low = mid + ;
else
high = mid -;
}
return n;
}
int main ()
{
LL k;
while (scanf ("%I64d %I64d", &n, &k) != EOF)
{
for (LL i=; i<n; i++)
{
scanf ("%I64d", &a[i]);
b[i] = a[i];
}
sort (b, b+n);
m = unique(b, b+n) - b;
memset (l, , sizeof(l));
memset (r, , sizeof(r));
for (LL i=; i<n; i++)
{
LL x = bin_sreach(a[i]);
l[x] ++;
}
__int64 ans = ;
for (LL i=n-; i>=; i--)
{
LL x, y, z;
x = y = n;
z = bin_sreach(a[i]);
l[z] --;
if (a[i] % k == )
x = bin_sreach(a[i]/k);
y = bin_sreach(a[i]*k);
ans += l[x] * r[y];
r[z] ++;
}
printf ("%I64d\n", ans);
}
return ;
}

Geometric Progression---cf 567C(求组合方式,map离散)的更多相关文章

  1. CodeForces 567C. Geometric Progression(map 数学啊)

    题目链接:http://codeforces.com/problemset/problem/567/C C. Geometric Progression time limit per test 1 s ...

  2. CodeForces 567C Geometric Progression

    Geometric Progression Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I ...

  3. 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 ...

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

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

  5. CF 567C(Geometric Progression-map)

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

  6. Codeforces 567C:Geometric Progression(DP)

    time limit per test : 1 second memory limit per test : 256 megabytes input : standard input output : ...

  7. 加和求不同的组合方式数目(dp)

    描述 有n个正整数,找出其中和为t(t也是正整数)的可能的组合方式.如: n=5,5个数分别为1,2,3,4,5,t=5: 那么可能的组合有5=1+4和5=2+3和5=5三种组合方式. 输入 输入的第 ...

  8. hdu 5278 Geometric Progression 高精度

    Geometric Progression Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contes ...

  9. 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 ...

随机推荐

  1. mouseover mouseenter mouseout mouseleave

    mouseover与mouseenter 不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件. 只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件. mouseou ...

  2. 10个Python 统计报表/图表图形类库

    matplotlib,官网:http://matplotlib.sourceforge.net/,Matplotlib 是一个由 John Hunter 等开发的,用以绘制二维图形的 Python 模 ...

  3. JSF request参数传递

    转载自:http://blog.csdn.net/duankaige/article/details/6711044 1:JSF页面之间传参 方法1: <h:outputLink value=& ...

  4. ssh配置authorized_keys后仍然需要输入密码的问题

    前阵子搭建Hadoop时,配置了本机(localhost)的ssh的公钥到authorized_keys文件中,但是在ssh连接localhost时仍然提示需要输入密码,后来发现是$HOME/.ssh ...

  5. gin入门

    Download and install it: $ go get github.com/gin-gonic/gin Import it in your code: import "gith ...

  6. Spring Framework 官方文档学习(三)之Resource

    起因 标准JDK中使用 java.net.URL 来处理资源,但有很多不足,例如不能限定classpath,不能限定 ServletContext 路径. 所以,Spring提供了 Resource ...

  7. php -- instanceof、class_exists、insterface_exists、method_exists、get_class、get_parent_class

    class_exists:类是否存在 在创建对象之前判断类是否存在,如果不存在就应该先加载类,再创建对象,容错. interface_exists:接口是否存在 method_exists:方法是否存 ...

  8. 纪念,BZOJ AC 100题!

    虽然说有将近50+是usaco,然后还有很多水题T_T 看来我还是刷水题.... 看来我还是那么弱. T_T 但是好歹也要留个纪念..

  9. 如何在ChemDraw中打出符号π

    很多人日常使用ChemDraw是一款非常优秀的化学绘图软件,在其绘制化学结构式或者反应式的过程中,常常需要添加各种符号.比如有的用户会需要输入希腊字符π,但是不知道用什么方法添加.本教程就来给大家介绍 ...

  10. Python+selenium之获取文本值和下拉框选择数据

    Python+selenium之获取文本值和下拉框选择数据 一.结合实例进行描述 1. 实例如下所示: #新增标签操作 def func_labels(self): self.driver.find_ ...