HDU5738 Eureka

题目链接:HDU5738

Eureka

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2058    Accepted Submission(s): 613


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,vP,uv)  are called best pair, if for every  wP f(u,v)g(u,v,w) , where  f(u,v)=(xuxv)2+(yuyv)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   (1n1000)  -- then number of points.

Each of the following  n  lines contains two integers  xi  and  yi   (109xi,yi109)  -- 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
 
题意:求给出的点中有多少组满足上面的公式。
官方题解:

xjb推导一下可以知道best set一定是一些共线的点, 于是问题变成问有多少个子集共线. 首先, 把所有点按照(x,y)(x,y)双关键字排序, 然后枚举最左边的点ii, 那么其他点jj一定满足j>ij>i. 把在这个点右边的点都做下极角排序(按照1gcd(dx,dy)(dx,dy)gcd(dx,dy)1(dx,dy)排序), 统计下共线的就好了. 需要注意下对重点的处理.

题目分析:公式不是难点,f是距离的公式,移过去就是了,题目时要求共线的点对,考虑重点的情况,这里把两点的斜率用向量(结构体)表示出来。(0,0)显然就是重点了。统计出重点个数后就分情况讨论了,我们的目的是对所有的重点一次性讨论完,这样就不用一个一个算浪费时间了,设有a个重点,b个非重点在一条直线上。
1 对于a个重点,组合数为2^(a+1)-a-2
2 对于b个共线点,组合数为 (2^a-1)*(2^(b+1)-1)
统计出来套公式就行。
PS:对于map内定义<的内容,我当时写的是friend bool operator,结果蜜汁超时,换成网上给的bool operator 后就过了,,,话说这俩不是一个东西吗?

为什么把官方题解贴出来了呢,我对题解前3个字母稍微研究了一下, 我还是纯洁的白色,,,

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
long long mod=1e9+7;
long long gcd(long long  x,long long y)
{
    return(y==0)?x:gcd(y,x%y);
}
long long mod_exp(long long a,long long b,long long c)
{
    long long res,t;
    res=1%c;
    t=a%c;
    while(b)
    {
        if(b&1)
        {
            res=res*t%c;
        }
        t=t*t%c;
        b/=2;
    }
    return res;
}
struct node
{
    long long x,y;
    bool operator<(const node&x)const{
        if(this->x==x.x)
            return this->y<x.y;
        else
            return this->x<x.x;
    }
}s[1100];
map<node,long long>f;
map<node,long long>::iterator it;
map<node,long long>g;
int main()
{
    long long T,n,ans;
    scanf("%lld",&T);
    while(T--)
    {
        g.clear();
        f.clear();
        ans=0;
        scanf("%lld",&n);
        for(long long i=0;i<n;i++)
        {
            scanf("%lld%lld",&s[i].x,&s[i].y);
        }
        sort(s,s+n);
        for(long long i=0;i<n;i++)
        {
            
            if(g[s[i]]) continue;
            f.clear();
            for(long long j=i+1;j<n;j++)
            {
                long long x=s[j].x-s[i].x;
                long long y=s[j].y-s[i].y;
                if(x<0)
                {
                    x=-x;y=-y;
                }
                long long GCD=gcd(x,abs(y));
                if(x!=0&&y!=0)
                {
                    if(x==0) y=1;
                    if(y==0) x=1;
                }
                if(GCD!=0)
                {
                    x/=GCD;y/=GCD;
                }
                node temp;
                temp.x=x; temp.y=y;
                f[temp]++;
            }
            long long k;
            node buf;
            buf.x=0;
            buf.y=0;
            long long temp;
            temp=f[buf];
            for(it=f.begin();it!=f.end();it++)
            {
                if(it->first.x==0&&it->first.y==0)
                {
                    k=mod_exp(2, it->second+1,mod);
                    k=k-it->second-2;
                    ans+=k;
                    ans%=mod;
                }
                else
                {
                    k=mod_exp(2,it->second,mod);
                    k=k-1;
                    ans+=(k*(mod_exp(2,temp+1,mod)-1));
                    ans%=mod;
                }
            }
            g[s[i]]=1;
        }
        ans%=mod;
        printf("%lld\n",ans);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值