E - Squares POJ - 2002

#include<cstdio>
#include<cstring>
#define maxl 1010
#define mod 1000007

int n,ans;
struct node 
{
	int x,y;
};
node a[maxl],hsh[mod];
bool cov[mod];

int find(int x,int y)
{
	int s=(x*x+y*y)%mod;
	if(s<0) s+=mod;
	while(cov[s] && (hsh[s].x!=x || hsh[s].y!=y))
		s=(s+1)%mod;
	return s;
}

void prework()
{
	int s;
	memset(cov,0,sizeof(cov));
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i].x,&a[i].y);
		s=find(a[i].x,a[i].y);
		cov[s]=true;hsh[s].x=a[i].x;hsh[s].y=a[i].y;
	}
}

void mainwork()
{
	int s1,s2;
	ans=0;int x1,y1,x2,y2,x3,y3,x4,y4;
	for(int i=1;i<=n;i++)
		for(int j=i+1;j<=n;j++)
		{
			x1=a[i].x;x2=a[j].x;y1=a[i].y;y2=a[j].y;
			x3=x2+(y2-y1);y3=y2-(x2-x1);
			x4=x1+(y2-y1);y4=y1-(x2-x1);
			s1=find(x3,y3);s2=find(x4,y4);
			if(cov[s1] && cov[s2])
				ans++;
			x3=x2-(y2-y1);y3=y2+(x2-x1);
			x4=x1-(y2-y1);y4=y1+(x2-x1);
			s1=find(x3,y3);s2=find(x4,y4);
			if(cov[s1] && cov[s2])
				ans++;
		}
}

void print()
{
	printf("%d\n",ans/4);
}

int main()
{
	while(~scanf("%d",&n))
	{
		if(!n)
			break;
		prework();
		mainwork();
		print();
	}
	return 0;
}

记住一个教训,vector把数组开出来不添加值也会占空间,vector<int > e;  e.clear(),无法减少空间,必须用vector<int> (e).swap(e),才能释放内存空间,所以以后做hash题,最好不用vector存公共溢出区,直接开放寻址法,省时省空间。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值