3508. 【NOIP2013模拟11.5B组】好元素(good) 
(File IO): input:good.in output:good.out

Time Limits: 2000 ms  Memory Limits: 262144 KB  Detailed Limits  

Goto ProblemSet

Description

小A一直认为,如果在一个由N个整数组成的数列An中,存在Am + An + Ap = Ai(1 <= m, n, p < i)(m, n, p可以相同)的话,Ai就是一个“好元素”。现在,小A有一个数列,他想知道这个数列中有多少个“好元素”,请你帮帮他。
 

Input

第一行只有一个正整数N,意义如上。

第二行包含N个整数,表示数列An。

Output

输出一个整数,表示这个数列中“好元素”的个数。
 

Sample Input

输入1:
2
1 3
输入2:
6
1 2 3 5 7 10
输入3:
3
-1 2 0

Sample Output

输出1:
1
输出2:
4
输出3:
1
 

Data Constraint

对于10%的数据  1<=N<=10

对于40%的数据  1<=N<=500    -10^5<=Ai<=10^5

对于70%的数据  1<=N<=5000   -10^6<=Ai<=10^6

对于100%的数据 1<=N<=5000   -10^9<=Ai<=10^9

 
做法:预处理ai + aj可以达到的值,用hash存起来,然后枚举l, k就好了
 
代码如下:

 #include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#define N 5007
#define mo 14150547
#define z 2000000000
#define LL long long
using namespace std;
LL n, a[N], h[mo + ];
int ans;
bool f; LL hash(LL x)
{
LL p = x % mo;
while (h[p] != && h[p] != x - z && p <= mo) p = (p + ) % mo;
return p;
} void work()
{
for (int i = ; i <= n; i++)
{
for (int j = ; j < i; j++)
if (h[hash(a[i] - a[j] + z)] == a[i] - a[j] && a[i] - a[j] != )
{
ans++;
break;
}
else if (a[i] - a[j] == )
{
if (f)
{
ans++;
break;
}
}
for (int j = ; j <= i; j++)
{
LL p = (a[i] + a[j] + z) % mo;
while (h[p] != a[i] + a[j] && h[p] != && p <= mo) p = (p + ) % mo;
if (a[i] + a[j] != ) h[p] = a[i] + a[j];
else f = true;
}
}
} int main()
{
freopen("good.in", "r", stdin);
freopen("good.out", "w", stdout);
scanf("%lld", &n);
for (int i = ; i <= n; i++)
scanf("%lld", &a[i]);
work();
cout << ans;
}

最新文章

  1. golang笔记——流程控制
  2. XP系统电脑带安卓手机上网教程(无需adhoc补丁)
  3. ReactNative学习-滑动查看图片第三方组件react-native-swiper
  4. 移动页面缩放方法之(二)控制HTML
  5. bestcoder.hdu.edu.cn
  6. 剑指offer第三天
  7. mssql sqlserver 索引专题
  8. 如何推进企业流程体系建设?_K2 BPM
  9. kettle删除移动文件
  10. JavaScript的对象详解
  11. Ch05 类 - 练习
  12. 获取目录文件.bat
  13. python --- 25 模块和包
  14. 几种always块的形态
  15. vi 命令集
  16. spring boot 中用@value给static变量赋值
  17. 什么是Asp.net Core?和 .net core有什么区别?(转)
  18. C++Builder 也有StringBuilder
  19. openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 四
  20. xargs在linux中的使用详解-乾颐堂

热门文章

  1. (转)Linux 文件和目录的属性
  2. SpringBoot源码篇:深度分析SpringBoot如何省去web.xml
  3. Java 内存模型(一)
  4. 汇编语言版本的HelloWorld
  5. java向上取整向下取整
  6. 什么是SpringBoot
  7. n宫格的实现方法
  8. php 03
  9. jQuerychicun
  10. Retrofit 2.0 轻松实现多文件/图片上传/Json字符串/表单