topic source
2021 Blue Bridge Cup Provincial
Topic link: http://acm.mangata.ltd/p/P1485
test center
math, violence
Video explanation
https://www.bilibili.com/video/BV1BY411J795
ideas
Because the selected points are all integer coordinates, so we select at most 21 × 20 = 420 21\times 20 = 42021×20=4 20 points , then we can select each two points as a straight line, but in this case, there may be repeated selection of a line, so we need to de-duplicate, so how to de-duplicate? We know that the standard equation for a line is:A x + By + C = 0 Ax+By+C = 0Ax+By+C=0 Then that means we only need to determineA , B , CA, B, CA , B , C is fine, but here you need to pay attention toA , B , CA, B, CA , B , C need to be simplified, otherwise we will not be able to achieve the effect of de-duplication. For the de-duplication of the structure of three or more elements, we can choose to write a structure by ourselves, but we can also use it directlyvector
. Then throw eachvector
into set
itA , B , CA, B, CWhat about A , B , C ? , we all know the equation of the line determined by two points:( y − y 1 ) y 1 − y 2 = x − x 1 x 1 − x 2 \frac{(y - y_1)}{y_1-y_2} = \frac{ x-x_1}{x_1-x_2}Y1− and2( and − and1)=x1−x2x−x1Then we can get: ( y 2 − y 1 ) x + ( x 1 − x 2 ) y + ( x 2 y 1 − x 1 y 2 ) = 0 (y_2 - y_1)x + ( x_1-x_2)y + (x_2y_1-x_1y_2) = 0( and2−Y1)x+(x1−x2) and+(x2Y1−x1Y2)=0
We can get: A = y 2 − y 1 , B = x 1 − x 2 , C = x 2 y 1 − x 1 y 2 A=y_2 - y_1, B=x_1-x_2, C =x_2y_1-x_1y_2A=Y2−Y1、B=x1−x2、C=x2Y1−x1Y2, then we remove the common factors of the three and put them vector
in , and then put them in set
to complete the calculation of a line. Finally, we only need to output set
the size of the container.
code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define endl "\n"
#define PII pair<int,int>
#define INF 0x3f3f3f3f
const double eps = 0.0000001;
int main()
{
vector<PII> Point;
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n = 20,m = 21;
// cin>>n>>m;
for(int x = 0;x < n; ++x)
for(int y = 0;y < m; ++y)
Point.push_back({
x,y});
int l = Point.size();
set<vector<int>> ans;
for(int i = 0;i < l; ++i) {
for(int j = i + 1;j < l; ++j) {
vector<int> t;
int x1 = Point[i].first,y1 = Point[i].second;
int x2 = Point[j].first,y2 = Point[j].second;
int a,b,c;
a = y2 - y1;
b = x1 - x2;
c = x2 * y1 - x1 * y2;
int d = __gcd(__gcd(a,b),c);
t.push_back(a/d);
t.push_back(b/d);
t.push_back(c/d);
ans.insert(t);
}
}
cout<<ans.size()<<endl;
return 0;
}
/*
ans = 40257
*/