Covered Points CodeForces - 1036E

http://codeforces.com/contest/1036/problem/E

卡B题时间太长 E没来及写。。

因为都是整数点 所以线段上的整数点数量就是横坐标之差与纵坐标之差的gcd+1

然后就是去重 n^2的求出所有交点 map去重

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair <ll,ll>
#define ppi pair <pll,int>
const double eps=1e-6;

struct node1
{
    double x,y;
};

struct node2
{
    node1 s,e;
};

map <ppi,bool> mp;
node2 seg[1010];
int n;

ll getabs(ll x)
{
    if(x>0) return x;
    else return -x;
}

ll getgcd(ll a,ll b)
{
    ll t;
    while(b>0)
    {
        t=b;
        b=a%b;
        a=t;
    }
    return a;
}

double cal(node1 a,node1 b,node1 c)//AB X AC
{
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}

double getk(node1 p1,node1 p2)
{
    return (double)(p2.y-p1.y)/(double)(p2.x-p1.x);
}

bool judge(node1 a,node1 b,node1 c,node1 d)
{
    if(cal(a,b,c)*cal(a,b,d)<eps&&cal(c,d,a)*cal(c,d,b)<eps&&getk(a,b)!=getk(c,d)) return true;
    else return false;
}

node1 getpoint(node1 a,node1 b,node1 c,node1 d)
{
    node1 res;
    double a1,b1,c1,a2,b2,c2;
    a1=a.y-b.y,b1=b.x-a.x,c1=a.x*b.y-b.x*a.y;
    a2=c.y-d.y,b2=d.x-c.x,c2=c.x*d.y-d.x*c.y;
    res.x=(b1*c2-b2*c1)/(a1*b2-a2*b1);
    res.y=-(a1*c2-a2*c1)/(a1*b2-a2*b1);
    return res;
}

bool isinterger(double x)
{
    double t1;
    ll t2;
    t2=x;
    t1=t2;
    if(fabs(x-t1)<eps) return true;
    else return false;
}

int main()
{
    node1 res;
    ll ans,x1,y1,x2,y2;
    int i,j;
    scanf("%d",&n);
    ans=0;
    for(i=1;i<=n;i++)
    {
        scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
        seg[i].s.x=(double)x1,seg[i].e.x=(double)x2,seg[i].s.y=(double)y1,seg[i].e.y=(double)y2;
        ans+=(getgcd(getabs(x1-x2),getabs(y1-y2))+1);
    }
    for(i=1;i<=n;i++)
    {
        for(j=i+1;j<=n;j++)
        {
            if(judge(seg[i].s,seg[i].e,seg[j].s,seg[j].e))
            {
                res=getpoint(seg[i].s,seg[i].e,seg[j].s,seg[j].e);
                if(isinterger(res.x)&&isinterger(res.y))
                {
                    x1=res.x,y1=res.y;
                    if(!mp[make_pair(make_pair(x1,y1),i)]||!mp[make_pair(make_pair(x1,y1),j)])
                    {
                        mp[make_pair(make_pair(x1,y1),i)]=true;
                        mp[make_pair(make_pair(x1,y1),j)]=true;
                        ans--;
                    }
                }
            }
        }
    }
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sunyutian1998/article/details/82526151