Advertisement
Guest User

Untitled

a guest
Feb 1st, 2019
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <string>
  6. #include <limits>
  7.  
  8. namespace pr {
  9.     struct Point {
  10.         double x,
  11.                y;
  12.  
  13.         Point(double x, double y) noexcept : x(x), y(y) {}
  14.     };
  15.  
  16.     std::ostream& operator<<(std::ostream& o, const Point p) {
  17.         o << "(" << p.x << ", " << p.y << ")";
  18.  
  19.         return o;
  20.     }
  21.  
  22.     inline
  23.     auto dist(Point a, Point b) noexcept {
  24.         return std::sqrt(
  25.             (a.x - b.x) * (a.x - b.x) +
  26.             (a.y - b.y) * (a.y - b.y)  
  27.         );
  28.     }
  29.  
  30.     inline
  31.     auto perimeter(const std::vector<Point>& points) noexcept {
  32.         double sum = 0.;
  33.  
  34.         for (auto it = points.begin(); it != points.end() - 1; ++it)
  35.             sum += dist(*it, *(it + 1));
  36.  
  37.         return sum;
  38.     }
  39.  
  40.     inline
  41.     auto is_prime(double num) {
  42.         if (num > (double) std::numeric_limits<unsigned long long>::max())
  43.             return false;
  44.  
  45.         unsigned long long n = num;
  46.  
  47.         if (n == 2)
  48.             return true;
  49.         else if (n < 2 || n % 2 == 0)
  50.             return false;
  51.         else
  52.             for (unsigned long long i = 3; i < n / 2; ++i)
  53.                 if (n % i == 0)
  54.                     return false;
  55.  
  56.         return true;
  57.     }
  58.  
  59.     inline
  60.     auto ccw(Point a, Point b, Point c) noexcept {
  61.         return a.x * (b.y - c.y) +
  62.                b.x * (c.y - a.y) +
  63.                c.x * (a.y - b.y);
  64.     }
  65.  
  66.     inline
  67.     auto ch(std::vector<Point>& points) noexcept {
  68.         std::vector<Point> upper, lower;
  69.         std::size_t size;
  70.  
  71.         std::sort(points.begin(), points.end(),
  72.             [](const auto& l, const auto& r) -> bool {
  73.                 return l.x < r.x || (l.x == r.x && l.y < r.y);
  74.             }
  75.         );
  76.  
  77.         for (auto&& point : points) {
  78.             while ((size = upper.size()) >= 2 && ccw(upper[size - 2], upper[size - 1], point) >= 0)
  79.                 upper.pop_back();
  80.  
  81.             upper.emplace_back(point);
  82.         }
  83.  
  84.         upper.pop_back();
  85.  
  86.         for (auto r_it = points.rbegin(); r_it != points.rend(); ++r_it) {
  87.             while ((size = lower.size()) >= 2 && ccw(lower[size - 2], lower[size - 1], *r_it) >= 0)
  88.                 lower.pop_back();
  89.  
  90.             lower.emplace_back(*r_it);
  91.         }
  92.  
  93.         std::copy(lower.begin(), lower.end(), std::back_inserter(upper));
  94.  
  95.         return upper;
  96.     }
  97.  
  98.     inline
  99.     void pe(const char* msg) {
  100.         std::cout << msg << std::endl;
  101.  
  102.         std::exit(EXIT_FAILURE);
  103.     }
  104. }
  105.  
  106. int main() {
  107.     std::ios_base::sync_with_stdio(false);
  108.  
  109.     std::pair<double, char> key;
  110.  
  111.     std::cout << "key in form of double, char pair: ";
  112.     std::cin >> key.first >> key.second;
  113.  
  114.     std::string pass;
  115.  
  116.     std::cout << "pass with atleast 16 chars: ";
  117.     std::cin >> pass;
  118.  
  119.     if (pass.length() < 16)
  120.         pr::pe("pass is too short");
  121.  
  122.     pass = pass.length() % 2 ? std::move(pass.substr(1)) : pass;
  123.  
  124.     std::vector<pr::Point> points;
  125.                            points.reserve(pass.length() / 2);
  126.  
  127.     for (std::size_t i = 0; i < pass.length(); i += 2)
  128.         points.emplace_back(
  129.             (double) (pass[i] - key.second) * key.first,
  130.             (double) (pass[i + 1] - key.second) * key.first
  131.         );
  132.  
  133.     points = std::move(pr::ch(points));
  134.     auto perimeter =  pr::perimeter(points);
  135.  
  136.     std::cout << (
  137.         points.size() == 5 && pr::is_prime(perimeter) ?
  138.             "success"
  139.                 :
  140.             "failure"
  141.     ) << std::endl;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement