题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5738
Eureka
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3059 Accepted Submission(s): 867
Problem Description
Professor Zhang draws n points on the plane, which are conveniently labeled by 1,2,…,n. The i-th point is at (xi,yi). Professor Zhang wants to know the number of best sets. As the value could be very large, print it modulo 109+7.
A set P (P contains the label of the points) is called best set if and only if there are at least one best pair in P. Two numbers u and v (u,v∈P,u≠v) are called best pair, if for every w∈P, f(u,v)≥g(u,v,w), where f(u,v)=(xu−xv)2+(yu−yv)2−−−−−−−−−−−−−−−−−−√ and g(u,v,w)=f(u,v)+f(v,w)+f(w,u)2.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤1000) – then number of points.
Each of the following n lines contains two integers xi and yi (−109≤xi,yi≤109) – coordinates of the i-th point.
Output
For each test case, output an integer denoting the answer.
Sample Input
3
3
1 1
1 1
1 1
3
0 0
0 1
1 0
1
0 0
Sample Output
4
3
0
Author
zimpha
Source
2016 Multi-University Training Contest 2
题意:
给你平面上n个点的坐标,问你共线的点的集合有多少个?(集合至少包括两个点)
分析:
对于集合P满足题目的公式的话,换句话说就是P集合中的所有的点都共线。
这题的最关键点就是对与重复点和重复边的处理
先把给的这些点按坐标逆时针排序,然后按顺序选一个点i,这个点i作为一定选到集合里面的点,然后再选枚举这个点之后的点j(对于和i相同的点要特殊处理),计算出每个点j和i之间的向量存在q[]数组里面,然后使每个向量变成最简(即除以最大公约数),然后排序所有的向量那么相等的向量即在同一条直线上。
有n个元素的集合的子集的数目为2^n个
cnt记录i之后和i相同的点的数目
q[]为化简后的向量数组,q[x]~q[y]相等
所以对于这个点集合组成的集合有这些部分:
1:重点和i自身组成的集合,i算一个点,其余cnt个元素集合取出一个非空的集合即可:: pw[cnt]-1
2:在y-x个点组成的集合中取出一个非空子集:pw[y-x]-1;在cnt个重点集合中取出一个子集(可为空):pw[cnt];两者之极即为所有组合
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 1010,mod = 1e9+7;
int pw[maxn];
struct point
{
int x,y;
point(){}
point(int xx,int yy):x(xx),y(yy){}
point operator - (const point &p) {return point(x-p.x,y-p.y);}
bool operator < (const point &p)const {return x<p.x || x == p.x && y < p.y;}
bool operator == (const point &p) {return x==p.x && y==p.y;}
void reduce() {int g=__gcd(abs(x),abs(y)); x/=g; y/=g;}//化简向量
}p[maxn],q[maxn];
void init()
{
pw[0] = 1;
for(int i=1;i<maxn;i++) pw[i] = pw[i-1]*2%mod;
}
int main()
{
int t,n,ans;
init();
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
ans = 0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
}
sort(p,p+n);
for(int i=0;i<n;i++)
{
int m=0,cnt=0;//向量的个数,和端点相同的点的个数
for(int j=i+1;j<n;j++)
{
if(p[i] == p[j]) cnt++;
else q[m++] = p[j]-p[i];
}
for(int j=0;j<m;j++) q[j].reduce();//向量化简
sort(q,q+m);
ans = (ans+pw[cnt]-1)%mod; //端点重点自身所能组成的集合数
for(int x=0,y;x<m;x=y)
{
for(y=x;y<m && q[y]==q[x];y++);//下标从x~y的向量相等即在同一条直线上
ans = (ans + (long long)pw[cnt]*(pw[y-x]-1)%mod)%mod;//由于cnt表示在端点后与端点相同的点,所以pw[cnt]不用减1
}
}
printf("%d\n",ans);
}
return 0;
}