Advertisement
dmkozyrev

a.cpp

Apr 17th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <cassert>
  6.  
  7. using namespace std;
  8.  
  9. inline int gcd(int a, int b) {
  10.     return b == 0 ? a : gcd(b, a % b);
  11. }
  12.  
  13. struct Line {
  14.     // a*x+b*y+c=0
  15.     int a, b, c;
  16.    
  17.     Line(int x1, int y1, int x2, int y2) {
  18.         // a*(x-x1)+b*(y-y1)=0
  19.         // a*x+b*y-(a*x1+b*y1)=0
  20.         int dx = x2-x1;
  21.         int dy = y2-y1;
  22.         a = -dy;
  23.         b = dx;
  24.         c = -(a*x1+b*y1);
  25.         int d = gcd(abs(a), abs(b));
  26.         assert(abs(a) % d == 0 && abs(b) % d == 0 && abs(c) % d == 0);
  27.         a /= d; b /= d; c /= d;
  28.         if (a < 0 || (a == 0 && b < 0)) {
  29.             a *= -1; b *= -1; c *= -1;
  30.         }
  31.     }
  32. };
  33.  
  34. inline bool operator<(const Line& left, const Line& right) {
  35.     return left.a < right.a || (left.a == right.a && (left.b < right.b || (left.b == right.b && left.c < right.c)));
  36. }
  37.  
  38. inline bool operator==(const Line& left, const Line& right) {
  39.     return left.a == right.a && left.b == right.b && left.c == right.c;
  40. }
  41.  
  42. int solve(int n) {
  43.     vector<pair<int,int>> points;
  44.     for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) points.push_back({i,j});
  45.    
  46.     vector<Line> lines;
  47.     for (int i = 0; i < n*n; ++i)
  48.         for (int j = i+1; j < n*n; ++j) {
  49.             auto& a = points[i];
  50.             auto& b = points[j];
  51.             lines.push_back(Line(a.first, a.second, b.first, b.second));
  52.         }
  53.     sort(lines.begin(), lines.end());
  54.     lines.erase(unique(lines.begin(), lines.end()), lines.end());
  55.     return lines.size();
  56. }
  57.  
  58. int main() {
  59.     for (int n = 2; n <= 100; ++n) {
  60.         cout << "answ(" << n << ")=" << solve(n) << endl;
  61.     }
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement