Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <algorithm>
- #include <string>
- #include <limits>
- namespace pr {
- struct Point {
- double x,
- y;
- Point(double x, double y) noexcept : x(x), y(y) {}
- };
- std::ostream& operator<<(std::ostream& o, const Point p) {
- o << "(" << p.x << ", " << p.y << ")";
- return o;
- }
- inline
- auto dist(Point a, Point b) noexcept {
- return std::sqrt(
- (a.x - b.x) * (a.x - b.x) +
- (a.y - b.y) * (a.y - b.y)
- );
- }
- inline
- auto perimeter(const std::vector<Point>& points) noexcept {
- double sum = 0.;
- for (auto it = points.begin(); it != points.end() - 1; ++it)
- sum += dist(*it, *(it + 1));
- return sum;
- }
- inline
- auto is_prime(double num) {
- if (num > (double) std::numeric_limits<unsigned long long>::max())
- return false;
- unsigned long long n = num;
- if (n == 2)
- return true;
- else if (n < 2 || n % 2 == 0)
- return false;
- else
- for (unsigned long long i = 3; i < n / 2; ++i)
- if (n % i == 0)
- return false;
- return true;
- }
- inline
- auto ccw(Point a, Point b, Point c) noexcept {
- return a.x * (b.y - c.y) +
- b.x * (c.y - a.y) +
- c.x * (a.y - b.y);
- }
- inline
- auto ch(std::vector<Point>& points) noexcept {
- std::vector<Point> upper, lower;
- std::size_t size;
- std::sort(points.begin(), points.end(),
- [](const auto& l, const auto& r) -> bool {
- return l.x < r.x || (l.x == r.x && l.y < r.y);
- }
- );
- for (auto&& point : points) {
- while ((size = upper.size()) >= 2 && ccw(upper[size - 2], upper[size - 1], point) >= 0)
- upper.pop_back();
- upper.emplace_back(point);
- }
- upper.pop_back();
- for (auto r_it = points.rbegin(); r_it != points.rend(); ++r_it) {
- while ((size = lower.size()) >= 2 && ccw(lower[size - 2], lower[size - 1], *r_it) >= 0)
- lower.pop_back();
- lower.emplace_back(*r_it);
- }
- std::copy(lower.begin(), lower.end(), std::back_inserter(upper));
- return upper;
- }
- inline
- void pe(const char* msg) {
- std::cout << msg << std::endl;
- std::exit(EXIT_FAILURE);
- }
- }
- int main() {
- std::ios_base::sync_with_stdio(false);
- std::pair<double, char> key;
- std::cout << "key in form of double, char pair: ";
- std::cin >> key.first >> key.second;
- std::string pass;
- std::cout << "pass with atleast 16 chars: ";
- std::cin >> pass;
- if (pass.length() < 16)
- pr::pe("pass is too short");
- pass = pass.length() % 2 ? std::move(pass.substr(1)) : pass;
- std::vector<pr::Point> points;
- points.reserve(pass.length() / 2);
- for (std::size_t i = 0; i < pass.length(); i += 2)
- points.emplace_back(
- (double) (pass[i] - key.second) * key.first,
- (double) (pass[i + 1] - key.second) * key.first
- );
- points = std::move(pr::ch(points));
- auto perimeter = pr::perimeter(points);
- std::cout << (
- points.size() == 5 && pr::is_prime(perimeter) ?
- "success"
- :
- "failure"
- ) << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement