题目链接: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. hdu1331(记忆化搜索)

    #include<iostream> #include<stdio.h> #include<string.h> using namespace std; typed ...

  2. Maven基础命令

    Maven 参数 -D 传入属性参数 -P 使用pom中指定的配置 -e 显示maven运行出错的信息 -o 离线执行命令,即不去远程仓库更新包 -X 显示maven允许的debug信息 -U 强制去 ...

  3. easyui添加删除tooltip

    /** * 扩展两个方法 */$.extend($.fn.datagrid.methods, { /** * 开打提示功能 * @param {} jq * @param {} params 提示消息 ...

  4. java深度搜索与广度优先搜索

    图结构展示: 实现过程: 首先,我们来看看图结构在代码中的实现.有三块逻辑: 1.图中的节点: public class GraphNode { public List<GraphEdge> ...

  5. [转]Loadrunner随机生成15位数字串

    Loadrunner随机生成15位数字串 PS:http://www.51testing.com/html/43/6343-19789.html 今天看到一个网友的问题,是想生成一个15位的数字串来进 ...

  6. VS编译错误:fatal error C1859:unexpected precompiled header error, simply rerunning the compiler might fix this problem

    fatal error C1859:unexpected precompiled header error, simply rerunning the compiler might fix this ...

  7. Docker 如何把镜像上传到docker hub

    1 首先你得准备一个hub 的帐号, 去 https://hub.docker.com 注册吧! 2 在hub那里新建一个仓库, 这个就类似于github那边的..create ---> cre ...

  8. Numpy库解析

    python中的axis 其实问题理解axis有问题,df.mean其实是在每一行上取所有列的均值,而不是保留每一列的均值.也许简单的来记就是axis=0代表往跨行(down),而axis=1代表跨列 ...

  9. Hibernate_day04--QBC查询

    QBC查询 1 使用hql查询需要写hql语句实现,但是使用qbc时候,不需要写语句了,使用方法实现 2 使用qbc时候,操作实体类和属性 3 使用qbc,使用Criteria对象实现 查询所有 1 ...

  10. 二、Android Studio使用——导入jar包,运行、debug都不是问题

    [新建AndroidStudio工程,lib导入jar包]   我们的项目代码都在app里面,可以看作是一个Model.   src 下面除了我们的代码之外,还有单元测试. 把JAR复制到libs文件 ...