又一次写起了几何。。。。
特殊处理在于有可能出现多条线段交于一点的情况,每次考虑时,对每条线段与其他所有线段的交点存在一个set里,对每一个set,每次删除set.size()即可
重点在于判断两条线段的交点是否是一个整数点,需要特殊考虑,平行和y=kx+b关系式不能成立的情况
我的代码中没有判断除数为0的情况(因为不会报错,我也就没写23333,但不影响结果
#include<iostream> #include<cstdio> #include<cmath> #include<queue> #include<vector> #include<string.h> #include<cstring> #include<algorithm> #include<set> #include<map> #include<fstream> #include<cstdlib> #include<ctime> #include<list> #include<climits> #include<bitset> #include<random> #include <ctime> #include <cassert> #include <complex> #include <cstring> #include <chrono> using namespace std; #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("input.in", "r", stdin);freopen("output.in", "w", stdout); #define left asfdasdasdfasdfsdfasfsdfasfdas1 #define tan asfdasdasdfasdfasfdfasfsdfasfdas mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef long long ll; typedef unsigned int un; const int desll[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; const int mod=1e9+7; const int maxn=5e5+7; const int maxm=1e5+7; const double eps=1e-8; int n,k,m; int ar[maxn][5]; int xl[5],yl[5]; set<pair<int,int>>se; bool is_interge(double x){ return fabs(x-round(x))<=eps; } bool equal(double a,double b){ return fabs(a-b)<=eps; } void getKB(int x,double& k,double& b) { k=(double)(ar[x][3]-ar[x][1])/(ar[x][2]-ar[x][0]); b=(double)ar[x][1]-k*ar[x][0]; } void func(int i,int j) { if(ar[i][0]==ar[i][2]&&ar[j][0]==ar[j][2])return ; if(ar[i][1]==ar[i][3]&&ar[j][1]==ar[j][3])return ; if(equal((double)(ar[i][3]-ar[i][1])/(ar[i][2]-ar[i][0]), (double)(ar[j][3]-ar[j][1])/(ar[j][2]-ar[j][0])))return ; double k1,k2,b1,b2; getKB(i,k1,b1); getKB(j,k2,b2); double x,y; if(ar[i][0]==ar[i][2]){ x=ar[i][0]; y=k2*x+b2; } else if(ar[j][0]==ar[j][2]){ x=ar[j][0]; y=k1*x+b1; } else{ x=(b2-b1)/(k1-k2); y=k1*x+b1; } if(!is_interge(x) || !is_interge(y))return ; int xx=round(x),yy=round(y); if(xx>=min(ar[i][0],ar[i][2])&&xx<=max(ar[i][0],ar[i][2])&& yy>=min(ar[i][1],ar[i][3])&&yy<=max(ar[i][1],ar[i][3])&& xx>=min(ar[j][0],ar[j][2])&&xx<=max(ar[j][0],ar[j][2])&& yy>=min(ar[j][1],ar[j][3])&&yy<=max(ar[j][1],ar[j][3])) se.insert(make_pair(xx,yy)); } int main() { scanf("%d",&n); for(int i=0;i<n;i++){ for(int j=0;j<4;j++)scanf("%d",&ar[i][j]); } ll ans=0; for(int i=0;i<n;i++){ int xx=abs(ar[i][2]-ar[i][0]); int yy=abs(ar[i][3]-ar[i][1]); ans += __gcd(xx,yy)+1; } for(int i=0;i<n;i++){ se.clear(); for(int j=i+1;j<n;j++){ func(i,j); } ans-=se.size(); } printf("%I64d ",ans); return 0; }