poj1971 哈希
来源:互联网 发布:apache官网下载 编辑:程序博客网 时间:2024/05/17 21:46
题目大意
给你一些点,求从其中取出4个点能组成平行四边形的点对数。
题目分析
一开始想的是枚举两个点,然后储存可以用来算斜率与长度的数值,如果一样就可以组成平行四边形…but…如果这4个点在同一条直线上呢啊喂?连样例都过不了的啊喂。
于是看了一下网上的题解,枚举两个点,算中点,因为对角线互相平分的四边形是平行四边形…
好吧…
代码
#include<algorithm>#include<iostream>#include<cstring>#include<climits>#include<cstdio>using namespace std;#define LL long longLL read(){ LL q=0,w=1;char ch=' '; while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar(); if(ch=='-')w=-1,ch=getchar(); while(ch>='0'&&ch<='9')q=q*10+(LL)(ch-'0'),ch=getchar(); return q*w;}const int N=1005;int T,tot,n;LL ans,mod=999997;LL x[N],y[N];int h[1000000],ne[N*N];LL xx[N*N],yy[N*N],num[N*N];//数组要开n*nvoid add(LL a,LL b){ LL kl=(a*a%mod+b)%mod; if(kl<0)kl=-kl;//注意哈希值为负数 for(int i=h[kl];i!=-1;i=ne[i])//链式哈希 if(xx[i]==a&&yy[i]==b) {ans+=num[i],++num[i];return;} ++tot;xx[tot]=a,yy[tot]=b,num[tot]=1,ne[tot]=h[kl],h[kl]=tot;}int main(){ int i,j; T=read(); while(T--){ n=read(); memset(h,-1,sizeof(h));tot=0; for(i=1;i<=n;++i)x[i]=read(),y[i]=read(); ans=0; for(i=1;i<=n;++i) for(j=i+1;j<=n;++j)add(x[i]+x[j],y[i]+y[j]); printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- poj1971 哈希
- poj1971 Parallelogram Counting
- Parallelogram Counting(POJ1971)
- POJ1971 Parallelogram Counting(hash)
- poj1971:Parallelogram Counting
- 0.8poj1971(排序||数学题)
- 哈希
- 哈希
- 哈希
- 哈希
- 哈希
- 哈希
- 哈希
- 哈希
- 哈希
- 哈希
- 哈希
- 哈希
- 装饰者模式
- Android应用层(View体系)一
- Spring Aware 接口读取*.txt文件
- 驼峰字符串
- 翻转链表
- poj1971 哈希
- Linux系统操作(5)———操作命令
- 程序员心路
- [cs231n之第一课]Introduction
- 优化网页性能方法汇总
- bzoj2654 tree(二分+kruskal)
- 无偏估计
- 2017年Java开发岗位面试题归类
- Android应用层(View体系)二