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;
}

JZOJ 3508. 【NOIP2013模拟11.5B组】好元素的更多相关文章

  1. JZOJ 3509. 【NOIP2013模拟11.5B组】倒霉的小C

    3509. [NOIP2013模拟11.5B组]倒霉的小C(beats) (File IO): input:beats.in output:beats.out Time Limits: 1000 ms ...

  2. JZOJ 3518. 【NOIP2013模拟11.6A组】进化序列(evolve)

    3518. [NOIP2013模拟11.6A组]进化序列(evolve) (File IO): input:evolve.in output:evolve.out Time Limits: 1000 ...

  3. JZOJ 3505. 【NOIP2013模拟11.4A组】积木(brick)

    3505. [NOIP2013模拟11.4A组]积木(brick) (File IO): input:brick.in output:brick.out Time Limits: 1000 ms Me ...

  4. JZOJ 3526. 【NOIP2013模拟11.7A组】不等式(solve)

    3526. [NOIP2013模拟11.7A组]不等式(solve) (File IO): input:solve.in output:solve.out Time Limits: 1000 ms M ...

  5. [jzoj]3506.【NOIP2013模拟11.4A组】善良的精灵(fairy)(深度优先生成树)

    Link https://jzoj.net/senior/#main/show/3506 Description 从前有一个善良的精灵. 一天,一个年轻人B找到她并请他预言他的未来.这个精灵透过他的水 ...

  6. JZOJ【NOIP2013模拟联考14】隐藏指令

    JZOJ[NOIP2013模拟联考14]隐藏指令 题目 Description 在d维欧几里得空间中,指令是一个长度为2N的串.串的每一个元素为d个正交基的方向及反方向之一.例如,d = 1时(数轴) ...

  7. [jzoj 5343] [NOIP2017模拟9.3A组] 健美猫 解题报告 (差分)

    题目链接: http://172.16.0.132/senior/#main/show/5343 题目: 题解: 记旋转i次之后的答案为$ans_i$,分别考虑每个元素对ans数组的贡献 若$s_i& ...

  8. [JZOJ 4307] [NOIP2015模拟11.3晚] 喝喝喝 解题报告

    题目链接: http://172.16.0.132/senior/#main/show/4307 题目: 解题报告: 题目询问我们没出现坏对的连续区间个数 我们考虑从左到有枚举右端点$r$,判断$a[ ...

  9. JZOJ 3487. 【NOIP2013模拟联考11】剑与魔法(dragons)

    3487. [NOIP2013模拟联考11]剑与魔法(dragons) (Standard IO) Time Limits: 1000 ms  Memory Limits: 131072 KB  De ...

随机推荐

  1. pat1084. Broken Keyboard (20)

    1084. Broken Keyboard (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue On a ...

  2. KBEngine warring项目源码阅读(二) 登录和baseapp的负载均衡

    原本不打算把登录拿出来写的,但是阅读登录部分的代码的时候发现登录和注册还不太一回事,因为登录涉及到分配baseapp的ip,负载均衡的实现,所以水一下. 流程图: 和上次一样,先是找unity控件 找 ...

  3. springmvc之Hello World及常用注解

    步骤: 加入jar包 在web.xml 中配置DispacherServlet 加入SpringMVC 配置文件springmvc.xml 编写请求处理器(action/controller) 编写视 ...

  4. [LeetCode]9. Palindrome Number回文数

    Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same back ...

  5. javascript中call()、apply()、bind()的用法理解

    一.bind的用法 第一个:obj.showInfo('arg','arg_18');中传的2个参数通过showInfo方法改变的是obj下中的name和age 第二个:obj.showInfo.bi ...

  6. poj3334(Connected Gheeves)

    Connected Gheeves Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1008   Accepted: 368 ...

  7. 获取hudson持续构建编译结果的一种方法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 很多时候使用hudson结合VisualStudio进行持续构建后需要获取持续构建的编译结果,通过编译结果来知道哪些项 ...

  8. php编译安装过程中遇到问题

    编译安装PHP时遇到的问题 问题1: configure: error: xml2-config not found. Please check your libxml2 installation. ...

  9. 不该被忽视的CoreJava细节(三)

    一.不该被遗忘的移位位运算 本文主要介绍移位运算(Shift Operation), 适当介绍一下其它相关的位运算. 甭说计算机刚发明那会,就连21世纪初那段日子,计算机内存都是KB/MB计算的.编写 ...

  10. PowerShell (407) Proxy Authentication Required

    $Client = New-Object -TypeName System.Net.WebClient $Client.Proxy.Credentials = [System.Net.Credenti ...