Geometric Progression---cf 567C(求组合方式,map离散)
题目链接: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离散)的更多相关文章
- CodeForces 567C. Geometric Progression(map 数学啊)
题目链接:http://codeforces.com/problemset/problem/567/C C. Geometric Progression time limit per test 1 s ...
- CodeForces 567C Geometric Progression
Geometric Progression Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I ...
- 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 ...
- map Codeforces Round #Pi (Div. 2) C. Geometric Progression
题目传送门 /* 题意:问选出3个数成等比数列有多少种选法 map:c1记录是第二个数或第三个数的选法,c2表示所有数字出现的次数.别人的代码很短,思维巧妙 */ /***************** ...
- CF 567C(Geometric Progression-map)
C. Geometric Progression time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces 567C:Geometric Progression(DP)
time limit per test : 1 second memory limit per test : 256 megabytes input : standard input output : ...
- 加和求不同的组合方式数目(dp)
描述 有n个正整数,找出其中和为t(t也是正整数)的可能的组合方式.如: n=5,5个数分别为1,2,3,4,5,t=5: 那么可能的组合有5=1+4和5=2+3和5=5三种组合方式. 输入 输入的第 ...
- hdu 5278 Geometric Progression 高精度
Geometric Progression Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contes ...
- 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 ...
随机推荐
- lua文件读写
lua里的文件读写模型来自C语言,分为完整模型(和C一样).简单模型. 1.简单模型 io.input([file]) 设置默认的输入文件,file为文件名(此时会以文本读入)或文件句柄(可以理解为 ...
- Project Euler:Problem 34 Digit factorials
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are ...
- Mybatis Batch 批量操作
Mybatis Batch 批量操作 http://www.blogjava.net/diggbag/articles/mybatis.html
- ES-PHP向ES批量添加文档报No alive nodes found in your cluster
ES-PHP向ES批量添加文档报No alive nodes found in your cluster 2016年12月14日 12:31:40 阅读数:2668 参考文章phpcurl 请求Chu ...
- MapReduce程序的工作过程
转自:http://www.aboutyun.com/thread-15494-1-2.html 问题导读1.HDFS框架组成是什么?2.HDFS文件的读写过程是什么?3.MapReduce框架组成是 ...
- 更新加子查询加相同的表解决办法 mysql
UPDATE ofuser SET auid = '0' WHERE uid in (SELECT uid FROM (select tmp.* from ofuser tmp)a WHERE aui ...
- /sys/kernel/debug/gpio
在使用GPIO的时候,有时候不知道GPIO的状态,也不知道在内核中GPIO是否申请成功. 可以通过/sys/kernel/debug/gpio这个文件来查看.这个文件显示了申请成功的GPIO的输入输出 ...
- android onTouchEvent
触摸屏幕时,没搞懂每个事件的启动顺序.本文记录onTouchEvent发生时,每个事件启动的顺序. 测试代码 @Override public boolean onTouchEvent(MotionE ...
- import是page指令的一个属性。
以下不属于JSP的标准指令的是.(选择1项) A.Taglib B.Include C.Import D.Page 解答:C
- Visual Studio Code调试node.js:无法在PATH上找到运行时的node
首先,环境变量Path中加入nodejs的路径: 验证nodejs是否已经加入环境变量: 接着,重新启动Visual Studio Code, 试一下,是不是好了~ 附录:Visual Studi ...