fork(1) download
  1. #include <cmath>
  2. #include <algorithm>
  3. #include <numeric>
  4. #include <vector>
  5. #include <iostream>
  6. #include <set>
  7. #include <string>
  8. #include <map>
  9. #include <unordered_map>
  10. #include <unordered_set>
  11. #include <limits>
  12. #include <functional>
  13. #include <stack>
  14. #include <queue>
  15. #include <random>
  16. using namespace std;
  17.  
  18.  
  19. struct Point2D
  20. {
  21. int x;
  22. int y;
  23.  
  24. bool operator==(const Point2D& other) const
  25. {
  26. return x * other.y == y * other.x;
  27. }
  28. };
  29.  
  30. static int gcd (int a, int b){
  31. a = abs(a); b = abs(b);
  32. return (b==0) ? a : gcd(b, a%b);
  33. }
  34.  
  35. namespace std
  36. {
  37. template <>
  38. struct hash<Point2D>
  39. {
  40. std::size_t operator()(const Point2D& k) const
  41. {
  42. // Compute individual hash values for first,
  43. // second and combine them using XOR
  44. // and bit shifting:
  45. int g = gcd(k.x, k.y);
  46. int a = k.x/g, b = k.y/g;
  47. return a ^ b;
  48.  
  49. /*
  50.   int i1 = k.x;
  51.   int i2 = k.y;
  52.   size_t ret = i1;
  53.   ret *= 2654435761U;
  54.   return ret ^ i2;
  55.   */
  56.  
  57. /*
  58.   return 111;
  59.   */
  60. }
  61. };
  62.  
  63. }
  64.  
  65. int solution(vector<Point2D> &A)
  66. {
  67. unordered_set<Point2D> pointsOnTheSameLine;
  68.  
  69. for (auto& point : A)
  70. {
  71. pointsOnTheSameLine.insert(point);
  72. }
  73.  
  74. return static_cast<int>(pointsOnTheSameLine.size());
  75. }
  76.  
  77. int main()
  78. {
  79. std::random_device rd; // only used once to initialise (seed) engine
  80. std::mt19937 rng(rd()); // random-number engine used (Mersenne-Twister in this case)
  81. std::uniform_int_distribution<int> uni(-10000, 10000); // guaranteed unbiased
  82.  
  83. int x = 13;
  84. int y = 7;
  85. vector<Point2D> v; // { {0, 1}, {0, 2}, {1, 1}, {-1, -1} };
  86. for (int i = 0; i < 1000; ++i)
  87. {
  88. auto random_integer = uni(rng);
  89. const Point2D curr{x * random_integer, y * random_integer};
  90. v.push_back(curr);
  91.  
  92. //cout << "is equal: " << (Point2D{x, y} == curr) << endl;
  93. }
  94.  
  95.  
  96.  
  97. cout << solution(v) << endl;
  98.  
  99. return 0;
  100. }
  101.  
Success #stdin #stdout 0s 4508KB
stdin
Standard input is empty
stdout
1