Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <cmath>
- #include <cassert>
- using namespace std;
- inline int gcd(int a, int b) {
- return b == 0 ? a : gcd(b, a % b);
- }
- struct Line {
- // a*x+b*y+c=0
- int a, b, c;
- Line(int x1, int y1, int x2, int y2) {
- // a*(x-x1)+b*(y-y1)=0
- // a*x+b*y-(a*x1+b*y1)=0
- int dx = x2-x1;
- int dy = y2-y1;
- a = -dy;
- b = dx;
- c = -(a*x1+b*y1);
- int d = gcd(abs(a), abs(b));
- assert(abs(a) % d == 0 && abs(b) % d == 0 && abs(c) % d == 0);
- a /= d; b /= d; c /= d;
- if (a < 0 || (a == 0 && b < 0)) {
- a *= -1; b *= -1; c *= -1;
- }
- }
- };
- inline bool operator<(const Line& left, const Line& right) {
- return left.a < right.a || (left.a == right.a && (left.b < right.b || (left.b == right.b && left.c < right.c)));
- }
- inline bool operator==(const Line& left, const Line& right) {
- return left.a == right.a && left.b == right.b && left.c == right.c;
- }
- int solve(int n) {
- vector<pair<int,int>> points;
- for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) points.push_back({i,j});
- vector<Line> lines;
- for (int i = 0; i < n*n; ++i)
- for (int j = i+1; j < n*n; ++j) {
- auto& a = points[i];
- auto& b = points[j];
- lines.push_back(Line(a.first, a.second, b.first, b.second));
- }
- sort(lines.begin(), lines.end());
- lines.erase(unique(lines.begin(), lines.end()), lines.end());
- return lines.size();
- }
- int main() {
- for (int n = 2; n <= 100; ++n) {
- cout << "answ(" << n << ")=" << solve(n) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement