EDU 50 E. Covered Points 利用克莱姆法则计算线段交点

E. Covered Points

利用克莱姆法则计算线段交点。n^2枚举,最后把个数开方,从ans中减去。

ans加上每个线段的定点数, 定点数用gcs(△x , △y)+1计算。

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>

using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "
";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '
'
//#define R register
#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 1e9+7;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;


template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}


/*-----------------------showtime----------------------*/

            const int maxn = 1009;
            struct node{
                ll x1,y1,x2,y2;
            }a[maxn];
            ll ans = 0;
            map<pii, int> mp;

            void cal(int i,int j){

                    ll x1 = a[i].x1,y1 = a[i].y1,x2 = a[i].x2,y2 = a[i].y2;

                    ll x3 = a[j].x1,y3 = a[j].y1,x4 = a[j].x2,y4 = a[j].y2;

                    ll a = (y1-y2) * (x4-x3) - (x2-x1)*(y3-y4);
                    ll t = (x2*y1 - x1*y2)*(x4-x3) - (x2-x1)*(x4*y3 - x3*y4);
                    ll p = (y1-y2) * (x4*y3-x3*y4) - (x2*y1-x1*y2)*(y3-y4);

                    if(a == 0)return;
                    if(t % a || p % a) return;
                    t = t/a; p = p/a;

                    if(x1 > x2) swap (x1,x2);   if(t < x1 || t > x2) return;

                    if(x3 > x4) swap (x3,x4);   if(t < x3 || t > x4) return;

                    if(y1 > y2) swap(y1,y2);    if(p < y1 || p > y2) return;

                    if(y3 > y4) swap(y3,y4);    if(p < y3 || p > y4) return;

                    mp[pii(t,p)] ++;
            }
int main(){
            int n;  scanf("%d", &n);
            for(int i=1; i<=n; i++){
                ll x1,y1,x2,y2;
                scanf("%lld%lld%lld%lld", &x1, &y1, &x2, & y2);
                a[i] = (node){x1,y1,x2,y2};
                ll d1 = abs(x1 - x2);
                ll d2 = abs(y1 - y2);
                ans += __gcd(d1, d2) + 1;
            }

            for(int i=1; i<=n; i++){
                for(int j=1; j<=n; j++){
                    if(i == j) continue;
                    cal(i,j);
                }
            }

            for(auto p : mp){
                ans -= (int)sqrt(p.se);
            }
            printf("%lld
", ans);

            return 0;
}
原文地址:https://www.cnblogs.com/ckxkexing/p/10303299.html