正题
大意
一个序列A,求满足
这个要求的 的个数
解题思路
我们先移一个项
然后用hash表储存 的所有答案,然后到达一个数的时候枚举 就可以 求出答案
代码
#include<cstdio>
#include<algorithm>
#define maxn 25000004
using namespace std;
int n,s,hash[maxn+10],a[5001];
bool v[maxn+10];
int hashmath(int x)
{return (x%maxn+maxn)%maxn;}
int locate(int x)//查找位置
{
int i=0,w=hashmath(x);
while (i<maxn&&v[(w+i)%maxn]&&hash[(w+i)%maxn]!=x)
i++;
return (w+i)%maxn;
}
void ins(int x)//插入
{
int w=locate(x);
hash[w]=x;v[w]=true;
}
bool find(int x)//查找
{
int w=locate(x);
if (hash[w]==x&&v[w]) return true;
else return false;
}
int main()
{
//freopen("good.in","r",stdin);
//freopen("good.out","w",stdout);
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
for (int j=1;j<=i;j++)
{
if (i!=j&&find(a[i]-a[j]))//查找
{
s++;//统计答案
break;//退出循环
}
}
for (int j=1;j<=i;j++)
ins(a[i]+a[j]);//加入新的答案
}
printf("%d",s);
}