JZOJ 3508. 【NOIP2013模拟11.5B组】好元素
2024-09-28 21:28:59
3508. 【NOIP2013模拟11.5B组】好元素(good)
(File IO): input:good.in output:good.out
Time Limits: 2000 ms Memory Limits: 262144 KB Detailed Limits
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;
}
最新文章
- golang笔记——流程控制
- XP系统电脑带安卓手机上网教程(无需adhoc补丁)
- ReactNative学习-滑动查看图片第三方组件react-native-swiper
- 移动页面缩放方法之(二)控制HTML
- bestcoder.hdu.edu.cn
- 剑指offer第三天
- mssql sqlserver 索引专题
- 如何推进企业流程体系建设?_K2 BPM
- kettle删除移动文件
- JavaScript的对象详解
- Ch05 类 - 练习
- 获取目录文件.bat
- python --- 25 模块和包
- 几种always块的形态
- vi 命令集
- spring boot 中用@value给static变量赋值
- 什么是Asp.net Core?和 .net core有什么区别?(转)
- C++Builder 也有StringBuilder
- openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 四
- xargs在linux中的使用详解-乾颐堂