计算几何线段判交点+容斥——cf1036E
不知道为什么tag里会有个fft。。
#include<bits/stdc++.h> using namespace std; #define N 2005 #define ll long long typedef double db; const db eps=1e-8; const db pi=acos(-1); int sign(db k){ if (k>eps) return 1; else if (k<-eps) return -1; return 0; } int cmp(db k1,db k2){return sign(k1-k2);} int inmid(db k1,db k2,db k3){return sign(k1-k3)*sign(k2-k3)<=0;}// k3 在 [k1,k2] 内 struct point{ db x,y; point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};} point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};} point operator * (db k1) const{return (point){x*k1,y*k1};} point operator / (db k1) const{return (point){x/k1,y/k1};} int operator == (const point &k1) const{return cmp(x,k1.x)==0&&cmp(y,k1.y)==0;} db abs(){return sqrt(x*x+y*y);} db abs2(){return x*x+y*y;} db dis(point k1){return ((*this)-k1).abs();} point unit(){db w=abs(); return (point){x/w,y/w};} }; int inmid(point k1,point k2,point k3){return inmid(k1.x,k2.x,k3.x)&&inmid(k1.y,k2.y,k3.y);} db cross(point k1,point k2){return k1.x*k2.y-k1.y*k2.x;} db dot(point k1,point k2){return k1.x*k2.x+k1.y*k2.y;} db rad(point k1,point k2){return atan2(cross(k1,k2),dot(k1,k2));} point getLL(point k1,point k2,point k3,point k4){ db w1=cross(k1-k3,k4-k3),w2=cross(k4-k3,k2-k3); return (k1*w2+k2*w1)/(w1+w2); } int intersect(db l1,db r1,db l2,db r2){ if (l1>r1) swap(l1,r1); if (l2>r2) swap(l2,r2); return cmp(r1,l2)!=-1&&cmp(r2,l1)!=-1; } int checkSS(point k1,point k2,point k3,point k4){ return intersect(k1.x,k2.x,k3.x,k4.x)&&intersect(k1.y,k2.y,k3.y,k4.y)&& sign(cross(k3-k1,k4-k1))*sign(cross(k3-k2,k4-k2))<=0&& sign(cross(k1-k3,k2-k3))*sign(cross(k1-k4,k2-k4))<=0; } point A[N],B[N]; ll n,ans; map<pair<int,int>,int> mp; int main(){ cin>>n; for(int i=1;i<=n;i++){ int x1,y1,x2,y2; cin>>x1>>y1>>x2>>y2; A[i]=(point){1.0*x1,1.0*y1}; B[i]=(point){1.0*x2,1.0*y2}; ans+=abs(__gcd(x1-x2,y1-y2))+1; } for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++){ if(checkSS(A[i],B[i],A[j],B[j])){ point k=getLL(A[i],B[i],A[j],B[j]); pair<int,int> p=make_pair(round(k.x),round(k.y)); if(sign(p.first-k.x)==0 && sign(p.second-k.y)==0) //判断是整数点 mp[p]++; } } //x条直线经过该点,那么这个点被统计x(x-1)/2次,解出这个x,答案里减去x-1 for(auto p:mp){ int cnt=p.second; ans-=(1+sqrt(1+8*cnt))/2-1; } cout<<ans<<'\n'; }