解题思路
题意为求直线数目,重叠直线算同一条直线 直线表达: 两点式方程:(x - x1 )/(x2 - x1)=(y - y1)/(y2 - y1) 化为Ax+By+c=0形式为:(y2 - y1)x + (x2 - x1)y - x1y2 + x2y1 = 0 储存A、B、C(除以公约数后的),并利用集合set去重
ac代码
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
typedef pair<int, int> P2;
typedef pair<P2, int> P3;
set<P3> s;
vector<P2> v;
int main() {
int x, y;
cin >> x >> y;
for(int i = 0; i < x; i++) {
for(int j = 0; j < y; j++) {
v.push_back({i,j});
}
}
for(int i = 0; i < v.size(); i++) {
for(int j = i + 1; j < v.size(); j++) {
int x1 = v[i].first, y1 = v[i].second;
int x2 = v[j].first, y2 = v[j].second;
int A = y1 - y2, B = x2 - x1, C = -x1 * y2 + x2 * y1;
int gcd = __gcd(__gcd(A,B) , C);
s.insert({{B / gcd, A / gcd}, C / gcd});
}
}
cout << s.size();
return 0;
}
|