output
stringlengths
52
181k
instruction
stringlengths
296
182k
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; const long long EPS = 0; template <class T> struct cplx { T x, y; cplx() { x = 0.0; y = 0.0; } cplx(T nx, T ny = 0) { x = nx; y = ny; } cplx operator+(const cplx& c) const { return {x + c.x, y + c.y}; } cplx operator-(const cplx& c) const { return {x - c.x, y - c.y}; } cplx operator*(const cplx& c) const { return {x * c.x - y * c.y, x * c.y + y * c.x}; } cplx& operator*=(const cplx& c) { return *this = {x * c.x - y * c.y, x * c.y + y * c.x}; } template <class U> cplx operator*(const U& c) const { return {x * c, y * c}; } template <class U> cplx operator/(const U& c) const { return {x / c, y / c}; } }; long long cp(const cplx<long long>& a, const cplx<long long>& b) { return a.x * b.y - b.x * a.y; } long long dp(const cplx<long long>& a, const cplx<long long>& b) { return a.x * b.x + a.y * b.y; } inline long long abs(const cplx<long long>& a) { return a.x * a.x + a.y * a.y; } inline bool eq(const cplx<long long>& a, const cplx<long long>& b) { return abs(a - b) <= EPS; } inline long long sgn(const long long& x) { return abs(x) <= EPS ? 0 : x / abs(x); } inline bool cmp_lex(const cplx<long long>& a, const cplx<long long>& b) { return a.x < b.x - EPS || (a.x <= b.x + EPS && a.y < b.y - EPS); } inline bool cmp_lex_i(const cplx<long long>& a, const cplx<long long>& b) { return a.y < b.y - EPS || (a.y <= b.y + EPS && a.x < b.x - EPS); } vector<cplx<long long>> chull(vector<cplx<long long>> p) { sort(p.begin(), p.end(), cmp_lex_i); int top = 0, bot = 1; vector<cplx<long long>> ch(2 * p.size()); for (int i = 0, d = 1; i < p.size() && i >= 0; i += d) { while (top > bot && cp(ch[top - 1] - ch[top - 2], p[i] - ch[top - 2]) <= 0) top--; ch[top++] = p[i]; if (i == p.size() - 1) d = -1, bot = top; } ch.resize(max(1, top - 1)); return ch; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<cplx<long long>> a(n), b(m); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; a[i] = cplx<long long>(x, y); } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; b[i] = cplx<long long>(x, y); } a = chull(a); b = chull(b); if (a.size() != b.size()) { puts("NO"); return 0; } n = a.size(); vector<pair<long long, long long>> alol(n), blol(n); pair<long long, long long> best; int besti; for (int i = 0; i < n; i++) { alol[i] = {abs(a[(i + 1) % n] - a[i]), dp(a[(i + 2) % n] - a[(i + 1) % n], a[i] - a[(i + 1) % n])}; if (best < alol[i]) { best = alol[i]; besti = i; } } rotate(alol.begin(), alol.begin() + besti, alol.end()); best = {0, 0}; for (int i = 0; i < n; i++) { blol[i] = {abs(b[(i + 1) % n] - b[i]), dp(b[(i + 2) % n] - b[(i + 1) % n], b[i] - b[(i + 1) % n])}; if (best < blol[i]) { best = blol[i]; besti = i; } } rotate(blol.begin(), blol.begin() + besti, blol.end()); puts(alol == blol ? "YES" : "NO"); return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; const long long EPS = 0; template <class T> struct cplx { T x, y; cplx() { x = 0.0; y = 0.0; } cplx(T nx, T ny = 0) { x = nx; y = ny; } cplx operator+(const cplx& c) const { return {x + c.x, y + c.y}; } cplx operator-(const cplx& c) const { return {x - c.x, y - c.y}; } cplx operator*(const cplx& c) const { return {x * c.x - y * c.y, x * c.y + y * c.x}; } cplx& operator*=(const cplx& c) { return *this = {x * c.x - y * c.y, x * c.y + y * c.x}; } template <class U> cplx operator*(const U& c) const { return {x * c, y * c}; } template <class U> cplx operator/(const U& c) const { return {x / c, y / c}; } }; long long cp(const cplx<long long>& a, const cplx<long long>& b) { return a.x * b.y - b.x * a.y; } long long dp(const cplx<long long>& a, const cplx<long long>& b) { return a.x * b.x + a.y * b.y; } inline long long abs(const cplx<long long>& a) { return a.x * a.x + a.y * a.y; } inline bool eq(const cplx<long long>& a, const cplx<long long>& b) { return abs(a - b) <= EPS; } inline long long sgn(const long long& x) { return abs(x) <= EPS ? 0 : x / abs(x); } inline bool cmp_lex(const cplx<long long>& a, const cplx<long long>& b) { return a.x < b.x - EPS || (a.x <= b.x + EPS && a.y < b.y - EPS); } inline bool cmp_lex_i(const cplx<long long>& a, const cplx<long long>& b) { return a.y < b.y - EPS || (a.y <= b.y + EPS && a.x < b.x - EPS); } vector<cplx<long long>> chull(vector<cplx<long long>> p) { sort(p.begin(), p.end(), cmp_lex_i); int top = 0, bot = 1; vector<cplx<long long>> ch(2 * p.size()); for (int i = 0, d = 1; i < p.size() && i >= 0; i += d) { while (top > bot && cp(ch[top - 1] - ch[top - 2], p[i] - ch[top - 2]) <= 0) top--; ch[top++] = p[i]; if (i == p.size() - 1) d = -1, bot = top; } ch.resize(max(1, top - 1)); return ch; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<cplx<long long>> a(n), b(m); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; a[i] = cplx<long long>(x, y); } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; b[i] = cplx<long long>(x, y); } a = chull(a); b = chull(b); if (a.size() != b.size()) { puts("NO"); return 0; } n = a.size(); vector<pair<long long, long long>> alol(n), blol(n); pair<long long, long long> best; int besti; for (int i = 0; i < n; i++) { alol[i] = {abs(a[(i + 1) % n] - a[i]), dp(a[(i + 2) % n] - a[(i + 1) % n], a[i] - a[(i + 1) % n])}; if (best < alol[i]) { best = alol[i]; besti = i; } } rotate(alol.begin(), alol.begin() + besti, alol.end()); best = {0, 0}; for (int i = 0; i < n; i++) { blol[i] = {abs(b[(i + 1) % n] - b[i]), dp(b[(i + 2) % n] - b[(i + 1) % n], b[i] - b[(i + 1) % n])}; if (best < blol[i]) { best = blol[i]; besti = i; } } rotate(blol.begin(), blol.begin() + besti, blol.end()); puts(alol == blol ? "YES" : "NO"); return 0; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> pair<T, T> operator-(pair<T, T> a, pair<T, T> b) { return make_pair(a.first - b.first, a.second - b.second); } template <typename T> T cross(pair<T, T> a, pair<T, T> b) { return a.first * b.second - a.second * b.first; } template <typename T> T dist(pair<T, T> a, pair<T, T> b) { return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); } template <typename T> vector<pair<T, T>> getCH(vector<pair<T, T>> v) { long long n = v.size(); sort(v.begin(), v.end()); vector<pair<T, T>> hull; for (long long i = 0; i < 2; i++) { long long t = hull.size(); for (auto x : v) { while (hull.size() - t >= 2 && cross(hull[hull.size() - 1] - hull[hull.size() - 2], x - hull[hull.size() - 2]) <= 0) hull.pop_back(); hull.push_back(x); } hull.pop_back(); reverse(v.begin(), v.end()); } return hull; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m; cin >> n >> m; vector<pair<long long, long long>> p1, p2; for (long long i = 0; i < n; i++) { long long x, y; cin >> x >> y; p1.push_back({x, y}); } for (long long i = 0; i < m; i++) { long long x, y; cin >> x >> y; p2.push_back({x, y}); } vector<pair<long long, long long>> CH1 = getCH(p1), CH2 = getCH(p2); n = CH1.size(), m = CH2.size(); if (n != m) { cout << "NO\n"; return 0; } for (long long k = 0; k < n; k++) { bool ok = true; for (long long i = 0; i < n; i++) { pair<long long, long long> pA, pB, pC, pD, pE, pF; pA = CH1[i % n], pB = CH1[(i + 1) % n], pC = CH1[(i + 2) % n]; pD = CH2[(i + k) % n], pE = CH2[(i + k + 1) % n], pF = CH2[(i + k + 2) % n]; long long dist1 = dist(pA, pB), dist2 = dist(pB, pC), dist3 = dist(pC, pA); long long dist4 = dist(pD, pE), dist5 = dist(pE, pF), dist6 = dist(pF, pD); if (dist1 != dist4 || dist2 != dist5 || dist3 != dist6) { ok = false; break; } } if (ok) { cout << "YES\n"; return 0; } } cout << "NO\n"; }
### Prompt Your challenge is to write a CPP solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> pair<T, T> operator-(pair<T, T> a, pair<T, T> b) { return make_pair(a.first - b.first, a.second - b.second); } template <typename T> T cross(pair<T, T> a, pair<T, T> b) { return a.first * b.second - a.second * b.first; } template <typename T> T dist(pair<T, T> a, pair<T, T> b) { return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); } template <typename T> vector<pair<T, T>> getCH(vector<pair<T, T>> v) { long long n = v.size(); sort(v.begin(), v.end()); vector<pair<T, T>> hull; for (long long i = 0; i < 2; i++) { long long t = hull.size(); for (auto x : v) { while (hull.size() - t >= 2 && cross(hull[hull.size() - 1] - hull[hull.size() - 2], x - hull[hull.size() - 2]) <= 0) hull.pop_back(); hull.push_back(x); } hull.pop_back(); reverse(v.begin(), v.end()); } return hull; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m; cin >> n >> m; vector<pair<long long, long long>> p1, p2; for (long long i = 0; i < n; i++) { long long x, y; cin >> x >> y; p1.push_back({x, y}); } for (long long i = 0; i < m; i++) { long long x, y; cin >> x >> y; p2.push_back({x, y}); } vector<pair<long long, long long>> CH1 = getCH(p1), CH2 = getCH(p2); n = CH1.size(), m = CH2.size(); if (n != m) { cout << "NO\n"; return 0; } for (long long k = 0; k < n; k++) { bool ok = true; for (long long i = 0; i < n; i++) { pair<long long, long long> pA, pB, pC, pD, pE, pF; pA = CH1[i % n], pB = CH1[(i + 1) % n], pC = CH1[(i + 2) % n]; pD = CH2[(i + k) % n], pE = CH2[(i + k + 1) % n], pF = CH2[(i + k + 2) % n]; long long dist1 = dist(pA, pB), dist2 = dist(pB, pC), dist3 = dist(pC, pA); long long dist4 = dist(pD, pE), dist5 = dist(pE, pF), dist6 = dist(pF, pD); if (dist1 != dist4 || dist2 != dist5 || dist3 != dist6) { ok = false; break; } } if (ok) { cout << "YES\n"; return 0; } } cout << "NO\n"; } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native") using namespace std; const int MOD = 1000000007; const int UNDEF = -1; const int INF = 1 << 30; template <typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; } template <typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; } int rint(); char rch(); long long rlong(); long long len2(pair<int, int> a, pair<int, int> b) { long long dx = a.first - b.first; long long dy = a.second - b.second; return dx * dx + dy * dy; } long long ccw(pair<int, int> a, pair<int, int> b, pair<int, int> c) { b.first -= a.first; b.second -= a.second; c.first -= a.first; c.second -= a.second; return b.first * (long long)c.second - c.first * (long long)b.second; } long long dot(pair<int, int> a, pair<int, int> b, pair<int, int> c) { return (a.first - b.first) * (long long)(c.first - b.first) + (a.second - b.second) * (long long)(c.second - b.second); } vector<pair<int, int> > convexHull(vector<pair<int, int> > dat) { vector<pair<int, int> > upper, lower; sort(dat.begin(), dat.end()); for (int i = 0; i < dat.size(); i++) { while (upper.size() >= 2 && ccw(*++upper.rbegin(), *upper.rbegin(), dat[i]) >= 0) upper.pop_back(); while (lower.size() >= 2 && ccw(*++lower.rbegin(), *lower.rbegin(), dat[i]) <= 0) lower.pop_back(); upper.emplace_back(dat[i]); lower.emplace_back(dat[i]); } upper.insert(upper.end(), ++lower.rbegin(), --lower.rend()); return upper; } vector<pair<long long, long long> > serialize(vector<pair<int, int> > v) { int n = v.size(); vector<pair<long long, long long> > vans; for (int i = 0; i < n; i++) { int k = (i + 1); if (k >= n) k -= n; int p = (i - 1); if (p < 0) p += n; long long cw = dot(v[p], v[i], v[k]); long long len = len2(v[i], v[k]); vans.push_back(make_pair(cw, len)); } return vans; } vector<pair<long long, long long> > b; int bins(pair<long long, long long> key) { int imin = 0, imax = b.size(); while (imin < imax) { int imid = (imin + imax) >> 1; if (b[imid] < key) imin = imid + 1; else imax = imid; } return imin; } class KMP { public: vector<int> fail; vector<int> p; int gm; int dfa(int j, int x) { ++j; while (j > 0 && x != p[j - 1]) { j = fail[j]; } return j; } void buildfail() { int m = gm; int j = 0; for (int i = 1; i <= m; i++) { fail[i] = j; while (j > 0 && p[i - 1] != p[j - 1]) { j = fail[j]; } j++; } fail[m + 1] = j; } void init(vector<int> pattern) { int m = pattern.size(); gm = m; fail.resize(m + 2); p = pattern; buildfail(); } }; KMP kmp; int n[2]; vector<pair<long long, long long> > g[2]; vector<int> q[2]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); for (int t = 0; t < 2; t++) n[t] = rint(); for (int t = 0; t < 2; t++) { vector<pair<int, int> > v; v.reserve(n[t]); for (int i = 0; i < n[t]; i++) { v.push_back(make_pair(rint(), rint())); } g[t] = serialize(convexHull(v)); } b = g[0]; for (auto &w : g[1]) b.push_back(w); sort(b.begin(), b.end()); auto it = unique(b.begin(), b.end()); b.resize(it - b.begin()); for (int t = 0; t < 2; t++) { q[t].reserve(g[t].size()); for (auto &w : g[t]) { q[t].push_back(bins(w)); } } kmp.init(q[0]); int x = 0; for (int k = 0; k < 2; k++) { for (auto &w : q[1]) { x = kmp.dfa(x, w); if (x == q[0].size()) { printf("YES\n"); exit(0); } } } printf("NO\n"); } static char stdinBuffer[1024]; static char *stdinDataEnd = stdinBuffer + sizeof(stdinBuffer); static const char *stdinPos = stdinDataEnd; void readAhead(size_t amount) { size_t remaining = stdinDataEnd - stdinPos; if (remaining < amount) { memmove(stdinBuffer, stdinPos, remaining); size_t sz = fread(stdinBuffer + remaining, 1, sizeof(stdinBuffer) - remaining, stdin); stdinPos = stdinBuffer; stdinDataEnd = stdinBuffer + remaining + sz; if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0; } } int rint() { readAhead(16); int x = 0; bool neg = false; while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos; if (*stdinPos == '-') { ++stdinPos; neg = true; } while (*stdinPos >= '0' && *stdinPos <= '9') { x *= 10; x += *stdinPos - '0'; ++stdinPos; } return neg ? -x : x; } char rch() { readAhead(16); while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos; char ans = *stdinPos; ++stdinPos; return ans; } long long rlong() { readAhead(32); long long x = 0; bool neg = false; while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos; if (*stdinPos == '-') { ++stdinPos; neg = true; } while (*stdinPos >= '0' && *stdinPos <= '9') { x *= 10; x += *stdinPos - '0'; ++stdinPos; } return neg ? -x : x; }
### Prompt Please formulate a cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native") using namespace std; const int MOD = 1000000007; const int UNDEF = -1; const int INF = 1 << 30; template <typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; } template <typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; } int rint(); char rch(); long long rlong(); long long len2(pair<int, int> a, pair<int, int> b) { long long dx = a.first - b.first; long long dy = a.second - b.second; return dx * dx + dy * dy; } long long ccw(pair<int, int> a, pair<int, int> b, pair<int, int> c) { b.first -= a.first; b.second -= a.second; c.first -= a.first; c.second -= a.second; return b.first * (long long)c.second - c.first * (long long)b.second; } long long dot(pair<int, int> a, pair<int, int> b, pair<int, int> c) { return (a.first - b.first) * (long long)(c.first - b.first) + (a.second - b.second) * (long long)(c.second - b.second); } vector<pair<int, int> > convexHull(vector<pair<int, int> > dat) { vector<pair<int, int> > upper, lower; sort(dat.begin(), dat.end()); for (int i = 0; i < dat.size(); i++) { while (upper.size() >= 2 && ccw(*++upper.rbegin(), *upper.rbegin(), dat[i]) >= 0) upper.pop_back(); while (lower.size() >= 2 && ccw(*++lower.rbegin(), *lower.rbegin(), dat[i]) <= 0) lower.pop_back(); upper.emplace_back(dat[i]); lower.emplace_back(dat[i]); } upper.insert(upper.end(), ++lower.rbegin(), --lower.rend()); return upper; } vector<pair<long long, long long> > serialize(vector<pair<int, int> > v) { int n = v.size(); vector<pair<long long, long long> > vans; for (int i = 0; i < n; i++) { int k = (i + 1); if (k >= n) k -= n; int p = (i - 1); if (p < 0) p += n; long long cw = dot(v[p], v[i], v[k]); long long len = len2(v[i], v[k]); vans.push_back(make_pair(cw, len)); } return vans; } vector<pair<long long, long long> > b; int bins(pair<long long, long long> key) { int imin = 0, imax = b.size(); while (imin < imax) { int imid = (imin + imax) >> 1; if (b[imid] < key) imin = imid + 1; else imax = imid; } return imin; } class KMP { public: vector<int> fail; vector<int> p; int gm; int dfa(int j, int x) { ++j; while (j > 0 && x != p[j - 1]) { j = fail[j]; } return j; } void buildfail() { int m = gm; int j = 0; for (int i = 1; i <= m; i++) { fail[i] = j; while (j > 0 && p[i - 1] != p[j - 1]) { j = fail[j]; } j++; } fail[m + 1] = j; } void init(vector<int> pattern) { int m = pattern.size(); gm = m; fail.resize(m + 2); p = pattern; buildfail(); } }; KMP kmp; int n[2]; vector<pair<long long, long long> > g[2]; vector<int> q[2]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); for (int t = 0; t < 2; t++) n[t] = rint(); for (int t = 0; t < 2; t++) { vector<pair<int, int> > v; v.reserve(n[t]); for (int i = 0; i < n[t]; i++) { v.push_back(make_pair(rint(), rint())); } g[t] = serialize(convexHull(v)); } b = g[0]; for (auto &w : g[1]) b.push_back(w); sort(b.begin(), b.end()); auto it = unique(b.begin(), b.end()); b.resize(it - b.begin()); for (int t = 0; t < 2; t++) { q[t].reserve(g[t].size()); for (auto &w : g[t]) { q[t].push_back(bins(w)); } } kmp.init(q[0]); int x = 0; for (int k = 0; k < 2; k++) { for (auto &w : q[1]) { x = kmp.dfa(x, w); if (x == q[0].size()) { printf("YES\n"); exit(0); } } } printf("NO\n"); } static char stdinBuffer[1024]; static char *stdinDataEnd = stdinBuffer + sizeof(stdinBuffer); static const char *stdinPos = stdinDataEnd; void readAhead(size_t amount) { size_t remaining = stdinDataEnd - stdinPos; if (remaining < amount) { memmove(stdinBuffer, stdinPos, remaining); size_t sz = fread(stdinBuffer + remaining, 1, sizeof(stdinBuffer) - remaining, stdin); stdinPos = stdinBuffer; stdinDataEnd = stdinBuffer + remaining + sz; if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0; } } int rint() { readAhead(16); int x = 0; bool neg = false; while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos; if (*stdinPos == '-') { ++stdinPos; neg = true; } while (*stdinPos >= '0' && *stdinPos <= '9') { x *= 10; x += *stdinPos - '0'; ++stdinPos; } return neg ? -x : x; } char rch() { readAhead(16); while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos; char ans = *stdinPos; ++stdinPos; return ans; } long long rlong() { readAhead(32); long long x = 0; bool neg = false; while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos; if (*stdinPos == '-') { ++stdinPos; neg = true; } while (*stdinPos >= '0' && *stdinPos <= '9') { x *= 10; x += *stdinPos - '0'; ++stdinPos; } return neg ? -x : x; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 2; const int INF = 1e9 + 7; const double EPS = 1e-10; struct Point { int x, y; Point() {} Point(int x, int y) : x(x), y(y) {} friend Point operator-(Point p1, Point p2) { return Point(p1.x - p2.x, p1.y - p2.y); } friend bool operator<(Point p1, Point p2) { return (p1.y == p2.y ? p1.x < p2.x : p1.y < p2.y); } }; double norma(Point v) { return sqrt(1LL * v.x * v.x + 1LL * v.y * v.y); } double cross(Point v1, Point v2) { return 1LL * v1.x * v2.y - 1LL * v2.x * v1.y; } vector<Point> ConvexHull(vector<Point> P) { if (P.size() < 3) return P; Point p = *(min_element(P.begin(), P.end())); sort(P.begin(), P.end(), [p](Point p1, Point p2) { double area = cross(p1 - p, p2 - p); if (area == 0) return norma(p1 - p) < norma(p2 - p); return area > 0; }); vector<Point> C; C.push_back(P[0]); C.push_back(P[1]); int it = 2, szC = 2; while (it < P.size()) { if (szC > 1 && cross(P[it] - C[szC - 1], C[szC - 2] - C[szC - 1]) <= 0) { C.pop_back(); szC--; } else { C.push_back(P[it]); szC++; it++; } } return C; } struct data { double a, b, c; friend bool operator==(data d1, data d2) { bool eq1 = abs(d1.a - d2.a) <= EPS; bool eq2 = abs(d1.b - d2.b) <= EPS; bool eq3 = abs(d1.c - d2.c) <= EPS; return eq1 && eq2 && eq3; } friend bool operator!=(data d1, data d2) { return !(d1 == d2); } }; vector<int> F; void buildF(vector<data> &P) { F = vector<int>(P.size(), 0); for (int itP = 1, len = 0; itP < P.size(); itP++) { for (; len != -1 && P[itP] != P[len]; len = len ? F[len - 1] : -1) ; F[itP] = ++len; } } bool KMP(vector<data> &P, vector<data> &T) { buildF(P); for (int itT = 0, itP = 0; itT < T.size(); itT++) { for (; itP != -1 && T[itT] != P[itP]; itP = itP ? F[itP - 1] : -1) ; if (P.size() == ++itP) { return true; } } return false; } vector<data> getData(vector<Point> &P) { int sz = P.size(); vector<data> Data; for (int i = 0; i < sz; i++) { Data.push_back({norma(P[i] - P[(i + 1) % sz]), norma(P[(i + 1) % sz] - P[(i + 2) % sz]), norma(P[(i + 2) % sz] - P[i])}); } return Data; } bool equals(vector<Point> v1, vector<Point> v2) { if (v1.size() != v2.size()) return false; if (v1.size() == 2) { return norma(v1[0] - v1[1]) == norma(v2[0] - v2[1]); } for (int i = 0; i < v1.size(); i++) { v2.push_back(v2[i]); } vector<data> P = getData(v1); vector<data> T = getData(v2); return KMP(P, T); } int main() { int n, m; cin >> n >> m; vector<Point> P1(n); for (int i = 0; i < n; i++) { scanf("%d %d", &P1[i].x, &P1[i].y); } vector<Point> P2(m); for (int i = 0; i < m; i++) { scanf("%d %d", &P2[i].x, &P2[i].y); } vector<Point> v1 = ConvexHull(P1); vector<Point> v2 = ConvexHull(P2); if (equals(v1, v2)) cout << "YES"; else cout << "NO"; return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 2; const int INF = 1e9 + 7; const double EPS = 1e-10; struct Point { int x, y; Point() {} Point(int x, int y) : x(x), y(y) {} friend Point operator-(Point p1, Point p2) { return Point(p1.x - p2.x, p1.y - p2.y); } friend bool operator<(Point p1, Point p2) { return (p1.y == p2.y ? p1.x < p2.x : p1.y < p2.y); } }; double norma(Point v) { return sqrt(1LL * v.x * v.x + 1LL * v.y * v.y); } double cross(Point v1, Point v2) { return 1LL * v1.x * v2.y - 1LL * v2.x * v1.y; } vector<Point> ConvexHull(vector<Point> P) { if (P.size() < 3) return P; Point p = *(min_element(P.begin(), P.end())); sort(P.begin(), P.end(), [p](Point p1, Point p2) { double area = cross(p1 - p, p2 - p); if (area == 0) return norma(p1 - p) < norma(p2 - p); return area > 0; }); vector<Point> C; C.push_back(P[0]); C.push_back(P[1]); int it = 2, szC = 2; while (it < P.size()) { if (szC > 1 && cross(P[it] - C[szC - 1], C[szC - 2] - C[szC - 1]) <= 0) { C.pop_back(); szC--; } else { C.push_back(P[it]); szC++; it++; } } return C; } struct data { double a, b, c; friend bool operator==(data d1, data d2) { bool eq1 = abs(d1.a - d2.a) <= EPS; bool eq2 = abs(d1.b - d2.b) <= EPS; bool eq3 = abs(d1.c - d2.c) <= EPS; return eq1 && eq2 && eq3; } friend bool operator!=(data d1, data d2) { return !(d1 == d2); } }; vector<int> F; void buildF(vector<data> &P) { F = vector<int>(P.size(), 0); for (int itP = 1, len = 0; itP < P.size(); itP++) { for (; len != -1 && P[itP] != P[len]; len = len ? F[len - 1] : -1) ; F[itP] = ++len; } } bool KMP(vector<data> &P, vector<data> &T) { buildF(P); for (int itT = 0, itP = 0; itT < T.size(); itT++) { for (; itP != -1 && T[itT] != P[itP]; itP = itP ? F[itP - 1] : -1) ; if (P.size() == ++itP) { return true; } } return false; } vector<data> getData(vector<Point> &P) { int sz = P.size(); vector<data> Data; for (int i = 0; i < sz; i++) { Data.push_back({norma(P[i] - P[(i + 1) % sz]), norma(P[(i + 1) % sz] - P[(i + 2) % sz]), norma(P[(i + 2) % sz] - P[i])}); } return Data; } bool equals(vector<Point> v1, vector<Point> v2) { if (v1.size() != v2.size()) return false; if (v1.size() == 2) { return norma(v1[0] - v1[1]) == norma(v2[0] - v2[1]); } for (int i = 0; i < v1.size(); i++) { v2.push_back(v2[i]); } vector<data> P = getData(v1); vector<data> T = getData(v2); return KMP(P, T); } int main() { int n, m; cin >> n >> m; vector<Point> P1(n); for (int i = 0; i < n; i++) { scanf("%d %d", &P1[i].x, &P1[i].y); } vector<Point> P2(m); for (int i = 0; i < m; i++) { scanf("%d %d", &P2[i].x, &P2[i].y); } vector<Point> v1 = ConvexHull(P1); vector<Point> v2 = ConvexHull(P2); if (equals(v1, v2)) cout << "YES"; else cout << "NO"; return 0; } ```
#include <bits/stdc++.h> using namespace std; struct dn { int x, y; } a[100001], b[100001]; dn operator-(dn u, dn v) { return (dn){u.x - v.x, u.y - v.y}; } long long operator*(dn u, dn v) { return 1ll * u.x * v.y - 1ll * v.x * u.y; } long long cd(dn u, dn v) { return 1ll * u.x * v.x + 1ll * u.y * v.y; } bool cmp(dn u, dn v) { return u.x ^ v.x ? u.x < v.x : u.y < v.y; } struct da { long long l; double a; } x[200001], y[400001]; bool operator==(da u, da v) { return u.l == v.l && abs(u.a - v.a) < 1e-10; } int s[100001], ss[100001]; int f[100001]; int main() { int n, m, nn, mm; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].x, &a[i].y); for (int i = 1; i <= m; i++) scanf("%d%d", &b[i].x, &b[i].y); sort(a + 1, a + n + 1, cmp); sort(b + 1, b + m + 1, cmp); s[s[0] = 1] = 1; for (int i = 1; i <= n; i++) { for (; s[0] > 1 && (a[i] - a[s[s[0] - 1]]) * (a[s[s[0]]] - a[s[s[0] - 1]]) <= 0; s[0]--) ; s[++s[0]] = i; } ss[ss[0] = 1] = 1; for (int i = 1; i <= n; i++) { for (; ss[0] > 1 && (a[i] - a[ss[ss[0] - 1]]) * (a[ss[ss[0]]] - a[ss[ss[0] - 1]]) >= 0; ss[0]--) ; ss[++ss[0]] = i; } for (int i = ss[0] - 1; i > 1; i--) s[++s[0]] = ss[i]; for (int i = 1; i <= s[0]; i++) { dn u = a[s[i]], v = a[s[i % s[0] + 1]], w = a[s[(i + s[0] - 2) % s[0] + 1]]; x[i] = (da){cd(u - v, u - v), 1.0 * cd(v - u, w - u) * cd(v - u, w - u) / (1.0 * cd(v - u, v - u) * cd(w - u, w - u))}; } nn = s[0]; s[s[0] = 1] = 1; for (int i = 1; i <= m; i++) { for (; s[0] > 1 && (b[i] - b[s[s[0] - 1]]) * (b[s[s[0]]] - b[s[s[0] - 1]]) <= 0; s[0]--) ; s[++s[0]] = i; } ss[ss[0] = 1] = 1; for (int i = 1; i <= m; i++) { for (; ss[0] > 1 && (b[i] - b[ss[ss[0] - 1]]) * (b[ss[ss[0]]] - b[ss[ss[0] - 1]]) >= 0; ss[0]--) ; ss[++ss[0]] = i; } for (int i = ss[0] - 1; i > 1; i--) s[++s[0]] = ss[i]; for (int i = 1; i <= s[0]; i++) { dn u = b[s[i]], v = b[s[i % s[0] + 1]], w = b[s[(i + s[0] - 2) % s[0] + 1]]; y[i] = (da){cd(u - v, u - v), 1.0 * cd(v - u, w - u) * cd(v - u, w - u) / (1.0 * cd(v - u, v - u) * cd(w - u, w - u))}; } mm = s[0]; for (int i = mm + 1; i <= 2 * mm; i++) y[i] = y[i - mm]; for (int i = 2; i <= nn; i++) { int j = f[i - 1]; while (j && !(x[i] == x[j + 1])) j = f[j]; f[i] = j + (x[i] == x[j + 1]); } bool ok = 0; for (int i = 1, j = 0; i <= 2 * mm; i++) { if (j == nn) { ok = 1; break; } while (j && !(y[i] == x[j + 1])) j = f[j]; j += y[i] == x[j + 1]; if (j == nn) { ok = 1; break; } } printf(ok ? "YES\n" : "NO\n"); }
### Prompt Your challenge is to write a cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct dn { int x, y; } a[100001], b[100001]; dn operator-(dn u, dn v) { return (dn){u.x - v.x, u.y - v.y}; } long long operator*(dn u, dn v) { return 1ll * u.x * v.y - 1ll * v.x * u.y; } long long cd(dn u, dn v) { return 1ll * u.x * v.x + 1ll * u.y * v.y; } bool cmp(dn u, dn v) { return u.x ^ v.x ? u.x < v.x : u.y < v.y; } struct da { long long l; double a; } x[200001], y[400001]; bool operator==(da u, da v) { return u.l == v.l && abs(u.a - v.a) < 1e-10; } int s[100001], ss[100001]; int f[100001]; int main() { int n, m, nn, mm; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].x, &a[i].y); for (int i = 1; i <= m; i++) scanf("%d%d", &b[i].x, &b[i].y); sort(a + 1, a + n + 1, cmp); sort(b + 1, b + m + 1, cmp); s[s[0] = 1] = 1; for (int i = 1; i <= n; i++) { for (; s[0] > 1 && (a[i] - a[s[s[0] - 1]]) * (a[s[s[0]]] - a[s[s[0] - 1]]) <= 0; s[0]--) ; s[++s[0]] = i; } ss[ss[0] = 1] = 1; for (int i = 1; i <= n; i++) { for (; ss[0] > 1 && (a[i] - a[ss[ss[0] - 1]]) * (a[ss[ss[0]]] - a[ss[ss[0] - 1]]) >= 0; ss[0]--) ; ss[++ss[0]] = i; } for (int i = ss[0] - 1; i > 1; i--) s[++s[0]] = ss[i]; for (int i = 1; i <= s[0]; i++) { dn u = a[s[i]], v = a[s[i % s[0] + 1]], w = a[s[(i + s[0] - 2) % s[0] + 1]]; x[i] = (da){cd(u - v, u - v), 1.0 * cd(v - u, w - u) * cd(v - u, w - u) / (1.0 * cd(v - u, v - u) * cd(w - u, w - u))}; } nn = s[0]; s[s[0] = 1] = 1; for (int i = 1; i <= m; i++) { for (; s[0] > 1 && (b[i] - b[s[s[0] - 1]]) * (b[s[s[0]]] - b[s[s[0] - 1]]) <= 0; s[0]--) ; s[++s[0]] = i; } ss[ss[0] = 1] = 1; for (int i = 1; i <= m; i++) { for (; ss[0] > 1 && (b[i] - b[ss[ss[0] - 1]]) * (b[ss[ss[0]]] - b[ss[ss[0] - 1]]) >= 0; ss[0]--) ; ss[++ss[0]] = i; } for (int i = ss[0] - 1; i > 1; i--) s[++s[0]] = ss[i]; for (int i = 1; i <= s[0]; i++) { dn u = b[s[i]], v = b[s[i % s[0] + 1]], w = b[s[(i + s[0] - 2) % s[0] + 1]]; y[i] = (da){cd(u - v, u - v), 1.0 * cd(v - u, w - u) * cd(v - u, w - u) / (1.0 * cd(v - u, v - u) * cd(w - u, w - u))}; } mm = s[0]; for (int i = mm + 1; i <= 2 * mm; i++) y[i] = y[i - mm]; for (int i = 2; i <= nn; i++) { int j = f[i - 1]; while (j && !(x[i] == x[j + 1])) j = f[j]; f[i] = j + (x[i] == x[j + 1]); } bool ok = 0; for (int i = 1, j = 0; i <= 2 * mm; i++) { if (j == nn) { ok = 1; break; } while (j && !(y[i] == x[j + 1])) j = f[j]; j += y[i] == x[j + 1]; if (j == nn) { ok = 1; break; } } printf(ok ? "YES\n" : "NO\n"); } ```
#include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 100; struct Point { long long x, y; Point() {} Point(long long _x, long long _y) : x(_x), y(_y) {} Point operator+(const Point &a) const { return Point(x + a.x, y + a.y); } Point operator-(const Point &a) const { return Point(x - a.x, y - a.y); } long long int operator%(const Point &a) const { return x * a.x + y * a.y; } long long int operator*(const Point &a) const { return x * a.y - y * a.x; } double length() { return sqrt(x * x + y * y); } long long len2() { return *this % *this; } bool operator<(const Point &a) const { return x < a.x || (x == a.x && y < a.y); } void scan() { scanf("%lld%lld", &x, &y); } void eprint() { 42; } }; Point minP; bool cmpPoints(const Point &a, const Point &b) { Point v = a - minP; Point u = b - minP; if (v * u != 0) return v * u > 0; return v.length() < u.length(); } vector<Point> graham(vector<Point> wall) { int n = (int)wall.size(); minP = *min_element(wall.begin(), wall.end()); sort(wall.begin(), wall.end(), cmpPoints); vector<Point> st(n); int r = 0; for (int i = 0; i < n; i++) { while (r > 1 && (st[r - 1] - st[r - 2]) * (wall[i] - st[r - 2]) <= 0) r--; st[r++] = wall[i]; } st.resize(r); return st; } int n[2]; vector<Point> P[2]; void readP(int id) { P[id].resize(n[id]); for (int i = 0; i < n[id]; i++) P[id][i].scan(); P[id] = graham(P[id]); 42; for (auto x : P[id]) x.eprint(); 42; } const int SZ = 10 * N; long long ar[SZ]; int z[SZ]; void putAr(int id, int &sz) { int psz = (int)P[id].size(); for (int i = 0; i < psz; i++) { int ni = (i + 1) % psz; int pi = (i + psz - 1) % psz; ar[sz++] = (P[id][ni] - P[id][i]).len2(); ar[sz++] = (P[id][ni] - P[id][i]) % (P[id][i] - P[id][pi]); } } void calcZ(int sz) { int wl = 0, wr = 0; for (int i = 1; i < sz; i++) { if (i < wr) z[i] = min(z[i - wl], wr - i); while (ar[z[i]] == ar[i + z[i]]) z[i]++; if (i + z[i] > wr) { wl = i; wr = i + z[i]; } } } int main() { scanf("%d%d", &n[0], &n[1]); readP(0); readP(1); if (P[0].size() != P[1].size()) { printf("NO\n"); return 0; } int sz = 0; putAr(0, sz); int need = sz; putAr(1, sz); putAr(1, sz); calcZ(sz); for (int i = need; i < sz; i++) if (z[i] >= need) { printf("YES\n"); return 0; } printf("NO\n"); return 0; }
### Prompt Develop a solution in CPP to the problem described below: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 100; struct Point { long long x, y; Point() {} Point(long long _x, long long _y) : x(_x), y(_y) {} Point operator+(const Point &a) const { return Point(x + a.x, y + a.y); } Point operator-(const Point &a) const { return Point(x - a.x, y - a.y); } long long int operator%(const Point &a) const { return x * a.x + y * a.y; } long long int operator*(const Point &a) const { return x * a.y - y * a.x; } double length() { return sqrt(x * x + y * y); } long long len2() { return *this % *this; } bool operator<(const Point &a) const { return x < a.x || (x == a.x && y < a.y); } void scan() { scanf("%lld%lld", &x, &y); } void eprint() { 42; } }; Point minP; bool cmpPoints(const Point &a, const Point &b) { Point v = a - minP; Point u = b - minP; if (v * u != 0) return v * u > 0; return v.length() < u.length(); } vector<Point> graham(vector<Point> wall) { int n = (int)wall.size(); minP = *min_element(wall.begin(), wall.end()); sort(wall.begin(), wall.end(), cmpPoints); vector<Point> st(n); int r = 0; for (int i = 0; i < n; i++) { while (r > 1 && (st[r - 1] - st[r - 2]) * (wall[i] - st[r - 2]) <= 0) r--; st[r++] = wall[i]; } st.resize(r); return st; } int n[2]; vector<Point> P[2]; void readP(int id) { P[id].resize(n[id]); for (int i = 0; i < n[id]; i++) P[id][i].scan(); P[id] = graham(P[id]); 42; for (auto x : P[id]) x.eprint(); 42; } const int SZ = 10 * N; long long ar[SZ]; int z[SZ]; void putAr(int id, int &sz) { int psz = (int)P[id].size(); for (int i = 0; i < psz; i++) { int ni = (i + 1) % psz; int pi = (i + psz - 1) % psz; ar[sz++] = (P[id][ni] - P[id][i]).len2(); ar[sz++] = (P[id][ni] - P[id][i]) % (P[id][i] - P[id][pi]); } } void calcZ(int sz) { int wl = 0, wr = 0; for (int i = 1; i < sz; i++) { if (i < wr) z[i] = min(z[i - wl], wr - i); while (ar[z[i]] == ar[i + z[i]]) z[i]++; if (i + z[i] > wr) { wl = i; wr = i + z[i]; } } } int main() { scanf("%d%d", &n[0], &n[1]); readP(0); readP(1); if (P[0].size() != P[1].size()) { printf("NO\n"); return 0; } int sz = 0; putAr(0, sz); int need = sz; putAr(1, sz); putAr(1, sz); calcZ(sz); for (int i = need; i < sz; i++) if (z[i] >= need) { printf("YES\n"); return 0; } printf("NO\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; struct coord_t { long long x, y; coord_t() = default; coord_t(long long x, long long y) : x(x), y(y) {} bool operator<(const coord_t& that) const { return x < that.x || (x == that.x && y < that.y); } bool operator==(const coord_t& that) const { return x == that.x && y == that.y; } }; struct tri_t { long long a, b, c; tri_t() = default; tri_t(long long a, long long b, long long c) : a(a), b(b), c(c) {} bool operator==(const tri_t& that) const { return a == that.a && b == that.b && c == that.c; } }; vector<coord_t> p1, p2; vector<coord_t> convex_hull(vector<coord_t>); long long sqdist(coord_t, coord_t); bool is_subseq(vector<tri_t>, vector<tri_t>); int main(void) { int i; int n1, n2; scanf("%d%d", &n1, &n2); for (i = 0; i < n1; ++i) { int x, y; scanf("%d%d", &x, &y); p1.emplace_back(x, y); } for (i = 0; i < n2; ++i) { int x, y; scanf("%d%d", &x, &y); p2.emplace_back(x, y); } p1 = convex_hull(p1); p1.insert(p1.end(), p1.begin(), p1.end()); n1 = (int)p1.size(); p2 = convex_hull(p2); n2 = (int)p2.size(); vector<tri_t> tri1; for (i = 0; i < n1; ++i) { auto v1 = p1[i]; auto v2 = p1[(i + n1 - 1) % n1]; auto v3 = p1[(i + 1) % n1]; tri1.emplace_back(sqdist(v1, v2), sqdist(v2, v3), sqdist(v3, v1)); } vector<tri_t> tri2; for (i = 0; i < n2; ++i) { auto v1 = p2[i]; auto v2 = p2[(i + n2 - 1) % n2]; auto v3 = p2[(i + 1) % n2]; tri2.emplace_back(sqdist(v1, v2), sqdist(v2, v3), sqdist(v3, v1)); } printf("%s\n", is_subseq(tri1, tri2) ? "YES" : "NO"); exit(EXIT_SUCCESS); } long long ccw(coord_t p0, coord_t p1, coord_t p2) { return (p0.x * p1.y + p1.x * p2.y + p2.x * p0.y) - (p1.x * p0.y + p2.x * p1.y + p0.x * p2.y); } vector<coord_t> convex_hull(vector<coord_t> ps) { sort(ps.begin(), ps.end()); vector<coord_t> lower; for (auto p : ps) { while (lower.size() >= 2 && ccw(*next(lower.rbegin()), *lower.rbegin(), p) <= 0LL) { lower.pop_back(); } lower.push_back(p); } lower.pop_back(); vector<coord_t> upper; reverse(ps.begin(), ps.end()); for (auto p : ps) { while (upper.size() >= 2 && ccw(*next(upper.rbegin()), *upper.rbegin(), p) <= 0LL) { upper.pop_back(); } upper.push_back(p); } upper.pop_back(); lower.insert(lower.end(), upper.begin(), upper.end()); return lower; } long long sqdist(coord_t p, coord_t q) { return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y); } bool is_subseq(vector<tri_t> s1, vector<tri_t> s2) { int i, j; vector<int> fail({-1}); for (i = 1, j = 0; i < (int)s2.size(); ++i, ++j) { if (s2[i] == s2[j]) { fail.push_back(fail[j]); } else { fail.push_back(j); j = fail[j]; for (; j >= 0 && fail[i] != fail[j]; j = fail[j]) ; } } fail.push_back(j); for (i = 0, j = 0; i < (int)s1.size();) { if (s2[j] == s1[i]) { ++i; ++j; if (j == (int)s2.size()) { return true; } } else { j = fail[j]; if (j < 0) { ++i; ++j; } } } return false; }
### Prompt Your challenge is to write a Cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct coord_t { long long x, y; coord_t() = default; coord_t(long long x, long long y) : x(x), y(y) {} bool operator<(const coord_t& that) const { return x < that.x || (x == that.x && y < that.y); } bool operator==(const coord_t& that) const { return x == that.x && y == that.y; } }; struct tri_t { long long a, b, c; tri_t() = default; tri_t(long long a, long long b, long long c) : a(a), b(b), c(c) {} bool operator==(const tri_t& that) const { return a == that.a && b == that.b && c == that.c; } }; vector<coord_t> p1, p2; vector<coord_t> convex_hull(vector<coord_t>); long long sqdist(coord_t, coord_t); bool is_subseq(vector<tri_t>, vector<tri_t>); int main(void) { int i; int n1, n2; scanf("%d%d", &n1, &n2); for (i = 0; i < n1; ++i) { int x, y; scanf("%d%d", &x, &y); p1.emplace_back(x, y); } for (i = 0; i < n2; ++i) { int x, y; scanf("%d%d", &x, &y); p2.emplace_back(x, y); } p1 = convex_hull(p1); p1.insert(p1.end(), p1.begin(), p1.end()); n1 = (int)p1.size(); p2 = convex_hull(p2); n2 = (int)p2.size(); vector<tri_t> tri1; for (i = 0; i < n1; ++i) { auto v1 = p1[i]; auto v2 = p1[(i + n1 - 1) % n1]; auto v3 = p1[(i + 1) % n1]; tri1.emplace_back(sqdist(v1, v2), sqdist(v2, v3), sqdist(v3, v1)); } vector<tri_t> tri2; for (i = 0; i < n2; ++i) { auto v1 = p2[i]; auto v2 = p2[(i + n2 - 1) % n2]; auto v3 = p2[(i + 1) % n2]; tri2.emplace_back(sqdist(v1, v2), sqdist(v2, v3), sqdist(v3, v1)); } printf("%s\n", is_subseq(tri1, tri2) ? "YES" : "NO"); exit(EXIT_SUCCESS); } long long ccw(coord_t p0, coord_t p1, coord_t p2) { return (p0.x * p1.y + p1.x * p2.y + p2.x * p0.y) - (p1.x * p0.y + p2.x * p1.y + p0.x * p2.y); } vector<coord_t> convex_hull(vector<coord_t> ps) { sort(ps.begin(), ps.end()); vector<coord_t> lower; for (auto p : ps) { while (lower.size() >= 2 && ccw(*next(lower.rbegin()), *lower.rbegin(), p) <= 0LL) { lower.pop_back(); } lower.push_back(p); } lower.pop_back(); vector<coord_t> upper; reverse(ps.begin(), ps.end()); for (auto p : ps) { while (upper.size() >= 2 && ccw(*next(upper.rbegin()), *upper.rbegin(), p) <= 0LL) { upper.pop_back(); } upper.push_back(p); } upper.pop_back(); lower.insert(lower.end(), upper.begin(), upper.end()); return lower; } long long sqdist(coord_t p, coord_t q) { return (p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y); } bool is_subseq(vector<tri_t> s1, vector<tri_t> s2) { int i, j; vector<int> fail({-1}); for (i = 1, j = 0; i < (int)s2.size(); ++i, ++j) { if (s2[i] == s2[j]) { fail.push_back(fail[j]); } else { fail.push_back(j); j = fail[j]; for (; j >= 0 && fail[i] != fail[j]; j = fail[j]) ; } } fail.push_back(j); for (i = 0, j = 0; i < (int)s1.size();) { if (s2[j] == s1[i]) { ++i; ++j; if (j == (int)s2.size()) { return true; } } else { j = fail[j]; if (j < 0) { ++i; ++j; } } } return false; } ```
#include <bits/stdc++.h> using ul = std::uint32_t; using li = std::int32_t; using ull = std::uint64_t; using ll = std::int64_t; using vul = std::vector<ul>; using vvul = std::vector<vul>; using vb = std::vector<bool>; using vvb = std::vector<vb>; using vvvb = std::vector<vvb>; using vull = std::vector<ull>; using vvull = std::vector<vull>; using vli = std::vector<li>; using vll = std::vector<ll>; using pulul = std::pair<ul, ul>; using vpulul = std::vector<pulul>; const ul mod = 998244353; const ul base = 1234234; ul n, m; class point { public: ll x = 0; ll y = 0; point() = default; point(ll a, ll b) : x(a), y(b) {} }; point operator-(const point& a, const point& b) { return point(a.x - b.x, a.y - b.y); } point operator+(const point& a, const point& b) { return point(a.x + b.x, a.y + b.y); } ll det(const point& a, const point& b) { return a.x * b.y - a.y * b.x; } bool lesslean(const point& a, const point& b) { return det(a, b) > 0; } std::vector<point> datas(1e5); std::vector<point> top; std::vector<point> but; std::vector<point> hull; std::vector<ul> hash1; std::vector<ul> hash2; vul hashn1, hashn2, hashm1, hashm2; void exgcd(li a, li b, li& x, li& y) { if (b) { exgcd(b, a % b, y, x); y -= x * (a / b); } else { x = 1; y = 0; } } ul inverse(ul a) { li x, y; exgcd(mod, a, x, y); return y < 0 ? li(mod) + y : y; } ul plus(ul a, ul b) { return a + b < mod ? a + b : a + b - mod; } ul minus(ul a, ul b) { return a < b ? a + mod - b : a - b; } ul times(ul a, ul b) { return ull(a) * b % mod; } void gethull() { std::sort(datas.begin(), datas.end(), [](const point& a, const point& b) { return a.x != b.x ? a.x < b.x : a.y > b.y; }); top.resize(0); for (const point& p : datas) { while (top.size() > 1) { const point& a = *(top.end() - 2); const point& b = *(top.end() - 1); if (!lesslean(p - b, b - a)) { top.pop_back(); } else { break; } } top.push_back(p); } std::reverse(datas.begin(), datas.end()); but.resize(0); for (const point& p : datas) { while (but.size() > 1) { const point& a = *(but.end() - 2); const point& b = *(but.end() - 1); if (!lesslean(p - b, b - a)) { but.pop_back(); } else { break; } } but.push_back(p); } hull.resize(0); for (ul i = 1; i != top.size(); ++i) { hull.push_back(top[i]); } for (ul i = 1; i != but.size(); ++i) { hull.push_back(but[i]); } ul inv = inverse(hull.size()); point cen; for (ul i = 0; i != hull.size(); ++i) { cen.x = plus(cen.x, times(hull[i].x, inv)); cen.y = plus(cen.y, times(hull[i].y, inv)); } hash1.resize(hull.size()); hash2.resize(hull.size()); hull.push_back(hull.front()); for (ul i = 0; i + 1 != hull.size(); ++i) { hash1[i] = plus(times(minus(hull[i].x, cen.x), minus(hull[i].x, cen.x)), times(minus(hull[i].y, cen.y), minus(hull[i].y, cen.y))); hash2[i] = plus( times(minus(hull[i].x, hull[i + 1].x), minus(hull[i].x, hull[i + 1].x)), times(minus(hull[i].y, hull[i + 1].y), minus(hull[i].y, hull[i + 1].y))); } } vul fhashn; vul fhashm; vul basepow(1e5 + 1, 1); int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); for (ul i = 1; i != basepow.size(); ++i) { basepow[i] = times(basepow[i - 1], base); } std::cin >> n >> m; datas.resize(n); for (ul i = 0; i != n; ++i) { std::cin >> datas[i].x >> datas[i].y; } gethull(); hashn1 = hash1; hashn2 = hash2; datas.resize(m); for (ul i = 0; i != m; ++i) { std::cin >> datas[i].x >> datas[i].y; } gethull(); hashm1 = hash1; hashm2 = hash2; if (hashn1.size() != hashm1.size()) { std::cout << "NO" << std::endl; return 0; } fhashn.resize(hashn1.size() * 2 + 1); fhashm.resize(hashm1.size() + 1); for (ul i = 0; i != hashn1.size(); ++i) { fhashn[i + 1] = plus(plus(hashn1[i], times(hashn2[i], 2)), times(fhashn[i], base)); fhashm[i + 1] = plus(plus(hashm1[i], times(hashm2[i], 2)), times(fhashm[i], base)); } ul t = hashn1.size(); for (ul i = hashn1.size(); i != 2 * hashn1.size(); ++i) { fhashn[i + 1] = plus(plus(hashn1[i - t], times(hashn2[i - t], 2)), times(fhashn[i], base)); } for (ul i = 0; i != t; ++i) { if (minus(fhashn[i + t], times(fhashn[i], basepow[t])) == minus(fhashm[t], times(fhashm[0], basepow[t]))) { std::cout << "YES" << std::endl; return 0; } } std::cout << "NO" << std::endl; return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using ul = std::uint32_t; using li = std::int32_t; using ull = std::uint64_t; using ll = std::int64_t; using vul = std::vector<ul>; using vvul = std::vector<vul>; using vb = std::vector<bool>; using vvb = std::vector<vb>; using vvvb = std::vector<vvb>; using vull = std::vector<ull>; using vvull = std::vector<vull>; using vli = std::vector<li>; using vll = std::vector<ll>; using pulul = std::pair<ul, ul>; using vpulul = std::vector<pulul>; const ul mod = 998244353; const ul base = 1234234; ul n, m; class point { public: ll x = 0; ll y = 0; point() = default; point(ll a, ll b) : x(a), y(b) {} }; point operator-(const point& a, const point& b) { return point(a.x - b.x, a.y - b.y); } point operator+(const point& a, const point& b) { return point(a.x + b.x, a.y + b.y); } ll det(const point& a, const point& b) { return a.x * b.y - a.y * b.x; } bool lesslean(const point& a, const point& b) { return det(a, b) > 0; } std::vector<point> datas(1e5); std::vector<point> top; std::vector<point> but; std::vector<point> hull; std::vector<ul> hash1; std::vector<ul> hash2; vul hashn1, hashn2, hashm1, hashm2; void exgcd(li a, li b, li& x, li& y) { if (b) { exgcd(b, a % b, y, x); y -= x * (a / b); } else { x = 1; y = 0; } } ul inverse(ul a) { li x, y; exgcd(mod, a, x, y); return y < 0 ? li(mod) + y : y; } ul plus(ul a, ul b) { return a + b < mod ? a + b : a + b - mod; } ul minus(ul a, ul b) { return a < b ? a + mod - b : a - b; } ul times(ul a, ul b) { return ull(a) * b % mod; } void gethull() { std::sort(datas.begin(), datas.end(), [](const point& a, const point& b) { return a.x != b.x ? a.x < b.x : a.y > b.y; }); top.resize(0); for (const point& p : datas) { while (top.size() > 1) { const point& a = *(top.end() - 2); const point& b = *(top.end() - 1); if (!lesslean(p - b, b - a)) { top.pop_back(); } else { break; } } top.push_back(p); } std::reverse(datas.begin(), datas.end()); but.resize(0); for (const point& p : datas) { while (but.size() > 1) { const point& a = *(but.end() - 2); const point& b = *(but.end() - 1); if (!lesslean(p - b, b - a)) { but.pop_back(); } else { break; } } but.push_back(p); } hull.resize(0); for (ul i = 1; i != top.size(); ++i) { hull.push_back(top[i]); } for (ul i = 1; i != but.size(); ++i) { hull.push_back(but[i]); } ul inv = inverse(hull.size()); point cen; for (ul i = 0; i != hull.size(); ++i) { cen.x = plus(cen.x, times(hull[i].x, inv)); cen.y = plus(cen.y, times(hull[i].y, inv)); } hash1.resize(hull.size()); hash2.resize(hull.size()); hull.push_back(hull.front()); for (ul i = 0; i + 1 != hull.size(); ++i) { hash1[i] = plus(times(minus(hull[i].x, cen.x), minus(hull[i].x, cen.x)), times(minus(hull[i].y, cen.y), minus(hull[i].y, cen.y))); hash2[i] = plus( times(minus(hull[i].x, hull[i + 1].x), minus(hull[i].x, hull[i + 1].x)), times(minus(hull[i].y, hull[i + 1].y), minus(hull[i].y, hull[i + 1].y))); } } vul fhashn; vul fhashm; vul basepow(1e5 + 1, 1); int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); for (ul i = 1; i != basepow.size(); ++i) { basepow[i] = times(basepow[i - 1], base); } std::cin >> n >> m; datas.resize(n); for (ul i = 0; i != n; ++i) { std::cin >> datas[i].x >> datas[i].y; } gethull(); hashn1 = hash1; hashn2 = hash2; datas.resize(m); for (ul i = 0; i != m; ++i) { std::cin >> datas[i].x >> datas[i].y; } gethull(); hashm1 = hash1; hashm2 = hash2; if (hashn1.size() != hashm1.size()) { std::cout << "NO" << std::endl; return 0; } fhashn.resize(hashn1.size() * 2 + 1); fhashm.resize(hashm1.size() + 1); for (ul i = 0; i != hashn1.size(); ++i) { fhashn[i + 1] = plus(plus(hashn1[i], times(hashn2[i], 2)), times(fhashn[i], base)); fhashm[i + 1] = plus(plus(hashm1[i], times(hashm2[i], 2)), times(fhashm[i], base)); } ul t = hashn1.size(); for (ul i = hashn1.size(); i != 2 * hashn1.size(); ++i) { fhashn[i + 1] = plus(plus(hashn1[i - t], times(hashn2[i - t], 2)), times(fhashn[i], base)); } for (ul i = 0; i != t; ++i) { if (minus(fhashn[i + t], times(fhashn[i], basepow[t])) == minus(fhashm[t], times(fhashm[0], basepow[t]))) { std::cout << "YES" << std::endl; return 0; } } std::cout << "NO" << std::endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; int n, m; struct tuhla { long long x, y; tuhla() { x = y = 0; } tuhla(long long _x, long long _y) { x = _x; y = _y; } }; bool cmp(tuhla p1, tuhla p2) { if (p1.x == p2.x) { return (p1.y < p2.y); } return (p1.x < p2.x); } vector<tuhla> a, b; vector<tuhla> h1, h2; double text[6 * 100007]; double pattern[6 * 100007]; int fval[6 * 100007]; bool eq(double p1, double p2) { p1 -= p2; if (p1 < 0.001 && p1 > -0.001) { return true; } return false; } double slope(tuhla p1, tuhla p2) { double deltax = (p2.x - p1.x); double deltay = (p2.y - p1.y); if (eq(deltax, 0) == true) { if (deltay < 0) { return -1000000007; } return 1000000007; } return (deltay / deltax); } vector<tuhla> get_hull(vector<tuhla> v) { vector<tuhla> ret; int i; sort(v.begin(), v.end(), cmp); int sz = v.size(); for (i = 0; i < sz; i++) { while (ret.size() >= 2 && slope(ret[ret.size() - 2], v[i]) >= slope(ret[ret.size() - 2], ret[ret.size() - 1])) { ret.pop_back(); } ret.push_back(v[i]); } ret.pop_back(); int rem = ret.size(); for (i = sz - 1; i >= 0; i--) { while (ret.size() >= 2 + rem && slope(v[i], ret[ret.size() - 2]) >= slope(ret[ret.size() - 1], ret[ret.size() - 2])) { ret.pop_back(); } ret.push_back(v[i]); } return ret; } void input() { scanf("%d%d", &n, &m); int i; a.resize(n); b.resize(m); for (i = 0; i < n; i++) { scanf("%I64d%I64d", &a[i].x, &a[i].y); } for (i = 0; i < m; i++) { scanf("%I64d%I64d", &b[i].x, &b[i].y); } } void solve() { h1 = get_hull(a); h2 = get_hull(b); if (h1.size() != h2.size()) { printf("NO\n"); return; } int i; int sz = h1.size(); int cur = 0; h1.push_back(h1[1]); h2.push_back(h2[1]); for (i = 0; i < sz - 1; i++) { text[cur] = (h1[i].x - h1[i + 1].x) * (h1[i].x - h1[i + 1].x) + (h1[i].y - h1[i + 1].y) * (h1[i].y - h1[i + 1].y); pattern[cur] = (h2[i].x - h2[i + 1].x) * (h2[i].x - h2[i + 1].x) + (h2[i].y - h2[i + 1].y) * (h2[i].y - h2[i + 1].y); cur++; double ret1 = atan2(h1[i].y - h1[i + 1].y, h1[i].x - h1[i + 1].x); double ret2 = atan2(h1[i + 2].y - h1[i + 1].y, h1[i + 2].x - h1[i + 1].x); if (ret2 < ret1) { ret2 += 2 * 3.14159265359; } text[cur] = (ret2 - ret1); ret1 = atan2(h2[i].y - h2[i + 1].y, h2[i].x - h2[i + 1].x); ret2 = atan2(h2[i + 2].y - h2[i + 1].y, h2[i + 2].x - h2[i + 1].x); if (ret2 < ret1) { ret2 += 2 * 3.14159265359; } pattern[cur] = (ret2 - ret1); cur++; } for (i = 0; i < cur; i++) { text[cur + i] = text[i]; } fval[0] = 0; fval[1] = 0; int len = 0; for (i = 2; i <= cur; i++) { while (len > 0 && eq(pattern[len], pattern[i - 1]) == false) { len = fval[len]; } if (eq(pattern[len], pattern[i - 1]) == true) { len++; } fval[i] = len; } len = 0; for (i = 1; i <= 2 * cur; i++) { while (len > 0 && eq(pattern[len], text[i - 1]) == false) { len = fval[len]; } if (eq(pattern[len], text[i - 1]) == true) { len++; } if (len == cur) { printf("YES\n"); return; } } printf("NO\n"); } int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); input(); solve(); return 0; }
### Prompt Develop a solution in cpp to the problem described below: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n, m; struct tuhla { long long x, y; tuhla() { x = y = 0; } tuhla(long long _x, long long _y) { x = _x; y = _y; } }; bool cmp(tuhla p1, tuhla p2) { if (p1.x == p2.x) { return (p1.y < p2.y); } return (p1.x < p2.x); } vector<tuhla> a, b; vector<tuhla> h1, h2; double text[6 * 100007]; double pattern[6 * 100007]; int fval[6 * 100007]; bool eq(double p1, double p2) { p1 -= p2; if (p1 < 0.001 && p1 > -0.001) { return true; } return false; } double slope(tuhla p1, tuhla p2) { double deltax = (p2.x - p1.x); double deltay = (p2.y - p1.y); if (eq(deltax, 0) == true) { if (deltay < 0) { return -1000000007; } return 1000000007; } return (deltay / deltax); } vector<tuhla> get_hull(vector<tuhla> v) { vector<tuhla> ret; int i; sort(v.begin(), v.end(), cmp); int sz = v.size(); for (i = 0; i < sz; i++) { while (ret.size() >= 2 && slope(ret[ret.size() - 2], v[i]) >= slope(ret[ret.size() - 2], ret[ret.size() - 1])) { ret.pop_back(); } ret.push_back(v[i]); } ret.pop_back(); int rem = ret.size(); for (i = sz - 1; i >= 0; i--) { while (ret.size() >= 2 + rem && slope(v[i], ret[ret.size() - 2]) >= slope(ret[ret.size() - 1], ret[ret.size() - 2])) { ret.pop_back(); } ret.push_back(v[i]); } return ret; } void input() { scanf("%d%d", &n, &m); int i; a.resize(n); b.resize(m); for (i = 0; i < n; i++) { scanf("%I64d%I64d", &a[i].x, &a[i].y); } for (i = 0; i < m; i++) { scanf("%I64d%I64d", &b[i].x, &b[i].y); } } void solve() { h1 = get_hull(a); h2 = get_hull(b); if (h1.size() != h2.size()) { printf("NO\n"); return; } int i; int sz = h1.size(); int cur = 0; h1.push_back(h1[1]); h2.push_back(h2[1]); for (i = 0; i < sz - 1; i++) { text[cur] = (h1[i].x - h1[i + 1].x) * (h1[i].x - h1[i + 1].x) + (h1[i].y - h1[i + 1].y) * (h1[i].y - h1[i + 1].y); pattern[cur] = (h2[i].x - h2[i + 1].x) * (h2[i].x - h2[i + 1].x) + (h2[i].y - h2[i + 1].y) * (h2[i].y - h2[i + 1].y); cur++; double ret1 = atan2(h1[i].y - h1[i + 1].y, h1[i].x - h1[i + 1].x); double ret2 = atan2(h1[i + 2].y - h1[i + 1].y, h1[i + 2].x - h1[i + 1].x); if (ret2 < ret1) { ret2 += 2 * 3.14159265359; } text[cur] = (ret2 - ret1); ret1 = atan2(h2[i].y - h2[i + 1].y, h2[i].x - h2[i + 1].x); ret2 = atan2(h2[i + 2].y - h2[i + 1].y, h2[i + 2].x - h2[i + 1].x); if (ret2 < ret1) { ret2 += 2 * 3.14159265359; } pattern[cur] = (ret2 - ret1); cur++; } for (i = 0; i < cur; i++) { text[cur + i] = text[i]; } fval[0] = 0; fval[1] = 0; int len = 0; for (i = 2; i <= cur; i++) { while (len > 0 && eq(pattern[len], pattern[i - 1]) == false) { len = fval[len]; } if (eq(pattern[len], pattern[i - 1]) == true) { len++; } fval[i] = len; } len = 0; for (i = 1; i <= 2 * cur; i++) { while (len > 0 && eq(pattern[len], text[i - 1]) == false) { len = fval[len]; } if (eq(pattern[len], text[i - 1]) == true) { len++; } if (len == cur) { printf("YES\n"); return; } } printf("NO\n"); } int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); input(); solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long disll(pair<int, int> a, pair<int, int> b) { return 1LL * (a.first - b.first) * (a.first - b.first) + 1LL * (a.second - b.second) * (a.second - b.second); } long long ccw(pair<int, int> i, pair<int, int> j, pair<int, int> k) { return 1LL * (j.first - i.first) * (k.second - i.second) - 1LL * (k.first - i.first) * (j.second - i.second); } struct ConvexHull { vector<pair<int, int> > p; void add_point(int x, int y) { p.push_back({x, y}); } vector<pair<int, int> > solve() { swap(p[0], *min_element(p.begin(), p.end())); sort(p.begin() + 1, p.end(), [&](pair<int, int> u, pair<int, int> v) { long long t = ccw(p[0], u, v); return t > 0 || !t && u < v; }); vector<pair<int, int> > stk; for (auto it : p) { while (stk.size() > 1 && ccw(stk[stk.size() - 2], stk[stk.size() - 1], it) <= 0) stk.pop_back(); stk.push_back(it); } return stk; } }; struct KMP { vector<long long> p; vector<int> pi; KMP() {} KMP(vector<long long> _p) { p = _p; pi.resize(p.size() + 1); pi[0] = -1; for (int i = 0, j = -1; i < p.size(); pi[++i] = ++j) while (~j && !(p[i] == p[j])) j = pi[j]; } vector<int> findon(vector<long long> t) { vector<int> res; for (int i = 0, j = 0; i < t.size(); i++) { while (~j && !(t[i] == p[j])) j = pi[j]; if (++j == p.size()) res.push_back(i + 1 - j), j = pi[j]; } return res; } }; int n, m; vector<pair<int, int> > vp1, vp2, cv1, cv2; ConvexHull ch1, ch2; vector<long long> seq1, seq2; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { int x, y; scanf("%d%d", &x, &y); vp1.emplace_back(x, y); } for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); vp2.emplace_back(x, y); } sort(vp1.begin(), vp1.end()); sort(vp2.begin(), vp2.end()); int flag1 = 0, flag2 = 0; for (int i = 2; i < n; i++) if (ccw(vp1[i - 2], vp1[i - 1], vp1[i])) { flag1 = 1; break; } for (int i = 2; i < m; i++) if (ccw(vp2[i - 2], vp2[i - 1], vp2[i])) { flag2 = 1; break; } if (flag1 != flag2) { puts("NO"); return 0; } if (flag1 == 0 && flag2 == 0) { if (disll(vp1.front(), vp1.back()) == disll(vp2.front(), vp2.back())) { puts("YES"); } else puts("NO"); return 0; } for (auto &it : vp1) ch1.add_point(it.first, it.second); for (auto &it : vp2) ch2.add_point(it.first, it.second); cv1 = ch1.solve(); cv2 = ch2.solve(); if (cv1.size() != cv2.size()) { puts("NO"); return 0; } for (int i = 0; i < cv1.size(); i++) { pair<int, int> now = cv1[i], nxt1 = cv1[(i + 1) % cv1.size()], nxt2 = cv1[(i + 2) % cv1.size()]; seq1.push_back(disll(now, nxt1)); seq1.push_back(-1); seq1.push_back(disll(now, nxt2)); } for (int i = 0; i < cv2.size(); i++) { pair<int, int> now = cv2[i], nxt1 = cv2[(i + 1) % cv2.size()], nxt2 = cv2[(i + 2) % cv2.size()]; seq2.push_back(disll(now, nxt1)); seq2.push_back(-1); seq2.push_back(disll(now, nxt2)); } int tsz = seq1.size(); for (int i = 0; i < tsz; i++) seq1.push_back(seq1[i]); KMP kmp(seq2); vector<int> res = kmp.findon(seq1); if (res.empty()) puts("NO"); else puts("YES"); return 0; }
### Prompt Develop a solution in Cpp to the problem described below: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long disll(pair<int, int> a, pair<int, int> b) { return 1LL * (a.first - b.first) * (a.first - b.first) + 1LL * (a.second - b.second) * (a.second - b.second); } long long ccw(pair<int, int> i, pair<int, int> j, pair<int, int> k) { return 1LL * (j.first - i.first) * (k.second - i.second) - 1LL * (k.first - i.first) * (j.second - i.second); } struct ConvexHull { vector<pair<int, int> > p; void add_point(int x, int y) { p.push_back({x, y}); } vector<pair<int, int> > solve() { swap(p[0], *min_element(p.begin(), p.end())); sort(p.begin() + 1, p.end(), [&](pair<int, int> u, pair<int, int> v) { long long t = ccw(p[0], u, v); return t > 0 || !t && u < v; }); vector<pair<int, int> > stk; for (auto it : p) { while (stk.size() > 1 && ccw(stk[stk.size() - 2], stk[stk.size() - 1], it) <= 0) stk.pop_back(); stk.push_back(it); } return stk; } }; struct KMP { vector<long long> p; vector<int> pi; KMP() {} KMP(vector<long long> _p) { p = _p; pi.resize(p.size() + 1); pi[0] = -1; for (int i = 0, j = -1; i < p.size(); pi[++i] = ++j) while (~j && !(p[i] == p[j])) j = pi[j]; } vector<int> findon(vector<long long> t) { vector<int> res; for (int i = 0, j = 0; i < t.size(); i++) { while (~j && !(t[i] == p[j])) j = pi[j]; if (++j == p.size()) res.push_back(i + 1 - j), j = pi[j]; } return res; } }; int n, m; vector<pair<int, int> > vp1, vp2, cv1, cv2; ConvexHull ch1, ch2; vector<long long> seq1, seq2; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { int x, y; scanf("%d%d", &x, &y); vp1.emplace_back(x, y); } for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); vp2.emplace_back(x, y); } sort(vp1.begin(), vp1.end()); sort(vp2.begin(), vp2.end()); int flag1 = 0, flag2 = 0; for (int i = 2; i < n; i++) if (ccw(vp1[i - 2], vp1[i - 1], vp1[i])) { flag1 = 1; break; } for (int i = 2; i < m; i++) if (ccw(vp2[i - 2], vp2[i - 1], vp2[i])) { flag2 = 1; break; } if (flag1 != flag2) { puts("NO"); return 0; } if (flag1 == 0 && flag2 == 0) { if (disll(vp1.front(), vp1.back()) == disll(vp2.front(), vp2.back())) { puts("YES"); } else puts("NO"); return 0; } for (auto &it : vp1) ch1.add_point(it.first, it.second); for (auto &it : vp2) ch2.add_point(it.first, it.second); cv1 = ch1.solve(); cv2 = ch2.solve(); if (cv1.size() != cv2.size()) { puts("NO"); return 0; } for (int i = 0; i < cv1.size(); i++) { pair<int, int> now = cv1[i], nxt1 = cv1[(i + 1) % cv1.size()], nxt2 = cv1[(i + 2) % cv1.size()]; seq1.push_back(disll(now, nxt1)); seq1.push_back(-1); seq1.push_back(disll(now, nxt2)); } for (int i = 0; i < cv2.size(); i++) { pair<int, int> now = cv2[i], nxt1 = cv2[(i + 1) % cv2.size()], nxt2 = cv2[(i + 2) % cv2.size()]; seq2.push_back(disll(now, nxt1)); seq2.push_back(-1); seq2.push_back(disll(now, nxt2)); } int tsz = seq1.size(); for (int i = 0; i < tsz; i++) seq1.push_back(seq1[i]); KMP kmp(seq2); vector<int> res = kmp.findon(seq1); if (res.empty()) puts("NO"); else puts("YES"); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; const long long INFL = 1e18 + 123; const double PI = atan2(0, -1); mt19937 tw(960172); long long rnd(long long x, long long y) { static uniform_int_distribution<long long> d; return d(tw) % (y - x + 1) + x; } struct point { int x, y; point(int _x, int _y) : x(_x), y(_y) {} point operator-(point other) { return point(x - other.x, y - other.y); } point operator+(point other) { return point(x + other.x, y + other.y); } bool operator<(point other) { return x < other.x || (x == other.x && y < other.y); } }; long long cross(point p1, point p2) { return (long long)p1.x * p2.y - (long long)p1.y * p2.x; } long long scalar(point p1, point p2) { return (long long)p1.x * p2.x + (long long)p1.y * p2.y; } long long dist2(point p) { return (long long)p.x * p.x + (long long)p.y * p.y; } vector<pair<long long, long long>> calc(int n) { vector<point> points; int mem = 0; for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; points.push_back(point(x, y)); if (points[i] < points[mem]) { mem = i; } } point c = points[mem]; points.erase(points.begin() + mem); for (auto& p : points) { p = p - c; } sort(points.begin(), points.end(), [&](point p1, point p2) { bool fl1 = p1.y > 0 || (p1.y == 0 && p1.x > 0); bool fl2 = p2.y > 0 || (p2.y == 0 && p2.x > 0); if (fl1 != fl2) { return fl1 < fl2; } auto tmp = cross(p1, p2); if (tmp != 0) { return tmp > 0; } return dist2(p1) < dist2(p2); }); vector<point> st; st.push_back(point(0, 0)); points.push_back(point(0, 0)); for (auto p : points) { while (((int)(st).size()) > 1) { long long cr = cross(st.back() - st[((int)(st).size()) - 2], p - st.back()); long long sc = scalar(st.back() - st[((int)(st).size()) - 2], p - st.back()); if (cr < 0 || (cr == 0 && sc > 0)) { st.pop_back(); } else { break; } } st.push_back(p); } st.pop_back(); vector<pair<long long, long long>> ret; for (int i = 0; i < ((int)(st).size()); ++i) { int next = (i + 1) % ((int)(st).size()); int next2 = (i + 2) % ((int)(st).size()); ret.push_back({dist2(st[next] - st[i]), scalar(st[next] - st[i], st[next2] - st[next])}); } return ret; } void solve() { int n, m; cin >> n >> m; auto val1 = calc(n); auto val2 = calc(m); if (((int)(val1).size()) != ((int)(val2).size())) { cout << "NO\n"; return; } auto s = val1; for (int i = 0; i < 2; ++i) { for (auto p : val2) { s.push_back(p); } } vector<int> z(((int)(s).size())); int l = 0, r = 0; for (int i = 1; i < ((int)(s).size()); ++i) { if (i < r) { z[i] = min(r - i, z[i - l]); } while (i + z[i] < ((int)(s).size()) && s[z[i]] == s[i + z[i]]) { ++z[i]; } if (i >= ((int)(val1).size())) { if (z[i] >= ((int)(val1).size())) { cout << "YES\n"; return; } } if (i + z[i] > r) { l = i; r = i + z[i]; } } cout << "NO\n"; } int main() { cerr << fixed << setprecision(15); cout << fixed << setprecision(15); ios::sync_with_stdio(false); int tests = 1; for (int it = 1; it <= tests; ++it) { solve(); } return 0; }
### Prompt Create a solution in CPP for the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; const long long INFL = 1e18 + 123; const double PI = atan2(0, -1); mt19937 tw(960172); long long rnd(long long x, long long y) { static uniform_int_distribution<long long> d; return d(tw) % (y - x + 1) + x; } struct point { int x, y; point(int _x, int _y) : x(_x), y(_y) {} point operator-(point other) { return point(x - other.x, y - other.y); } point operator+(point other) { return point(x + other.x, y + other.y); } bool operator<(point other) { return x < other.x || (x == other.x && y < other.y); } }; long long cross(point p1, point p2) { return (long long)p1.x * p2.y - (long long)p1.y * p2.x; } long long scalar(point p1, point p2) { return (long long)p1.x * p2.x + (long long)p1.y * p2.y; } long long dist2(point p) { return (long long)p.x * p.x + (long long)p.y * p.y; } vector<pair<long long, long long>> calc(int n) { vector<point> points; int mem = 0; for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; points.push_back(point(x, y)); if (points[i] < points[mem]) { mem = i; } } point c = points[mem]; points.erase(points.begin() + mem); for (auto& p : points) { p = p - c; } sort(points.begin(), points.end(), [&](point p1, point p2) { bool fl1 = p1.y > 0 || (p1.y == 0 && p1.x > 0); bool fl2 = p2.y > 0 || (p2.y == 0 && p2.x > 0); if (fl1 != fl2) { return fl1 < fl2; } auto tmp = cross(p1, p2); if (tmp != 0) { return tmp > 0; } return dist2(p1) < dist2(p2); }); vector<point> st; st.push_back(point(0, 0)); points.push_back(point(0, 0)); for (auto p : points) { while (((int)(st).size()) > 1) { long long cr = cross(st.back() - st[((int)(st).size()) - 2], p - st.back()); long long sc = scalar(st.back() - st[((int)(st).size()) - 2], p - st.back()); if (cr < 0 || (cr == 0 && sc > 0)) { st.pop_back(); } else { break; } } st.push_back(p); } st.pop_back(); vector<pair<long long, long long>> ret; for (int i = 0; i < ((int)(st).size()); ++i) { int next = (i + 1) % ((int)(st).size()); int next2 = (i + 2) % ((int)(st).size()); ret.push_back({dist2(st[next] - st[i]), scalar(st[next] - st[i], st[next2] - st[next])}); } return ret; } void solve() { int n, m; cin >> n >> m; auto val1 = calc(n); auto val2 = calc(m); if (((int)(val1).size()) != ((int)(val2).size())) { cout << "NO\n"; return; } auto s = val1; for (int i = 0; i < 2; ++i) { for (auto p : val2) { s.push_back(p); } } vector<int> z(((int)(s).size())); int l = 0, r = 0; for (int i = 1; i < ((int)(s).size()); ++i) { if (i < r) { z[i] = min(r - i, z[i - l]); } while (i + z[i] < ((int)(s).size()) && s[z[i]] == s[i + z[i]]) { ++z[i]; } if (i >= ((int)(val1).size())) { if (z[i] >= ((int)(val1).size())) { cout << "YES\n"; return; } } if (i + z[i] > r) { l = i; r = i + z[i]; } } cout << "NO\n"; } int main() { cerr << fixed << setprecision(15); cout << fixed << setprecision(15); ios::sync_with_stdio(false); int tests = 1; for (int it = 1; it <= tests; ++it) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; long long sgn(long long a) { if (a < 0) return -1; else return 1; } struct pt { long long x; long long y; pt(long long x, long long y) : x(x), y(y) {} pt() {} pt operator+(const pt& other) const { return pt(this->x + other.x, this->y + other.y); } pt operator-(const pt& other) const { return pt(this->x - other.x, this->y - other.y); } long long operator*(const pt& other) const { return (long long)(this->x) * other.x + (long long)(this->y) * other.y; } bool operator==(const pt& other) const { return x == other.x && y == other.y; } long long angle(const pt& other) const { return x * other.x + y * other.y; } }; istream& operator>>(istream& in, pt& p) { in >> p.x >> p.y; return in; } ostream& operator<<(ostream& out, const pt& p) { out << p.x << ' ' << p.y << "; "; return out; } template <typename T> istream& operator>>(istream& in, vector<T>& ar) { for (long long i = 0; i < ar.size(); i++) { in >> ar[i]; } return in; } template <typename T> ostream& operator<<(ostream& out, vector<T>& ar) { for (long long i = 0; i < ar.size(); i++) { out << ar[i] << ' '; } return out; } long long vec_prod(const pt& a, const pt& b) { return (long long)a.x * b.y - (long long)a.y * b.x; } bool operator<(const pt& a, const pt& b) { long long v = vec_prod(a, b); if (v < 0) { return 0; } if (v > 0) { return 1; } if (a * a < b * b) { return 1; } else { return 0; } } pt left_bellow(const vector<pt>& s) { pt a = s[0]; for (auto& it : s) { if (it.x < a.x) { if (it.y < a.y) { a = it; } } } return a; } vector<pt> convex_hool(vector<pt>& s) { pt lb = left_bellow(s); for (auto& it : s) { it = it - lb; } sort(s.begin(), s.end()); vector<pt> hool; s.push_back(*s.begin()); hool.push_back(s[0]); hool.push_back(s[1]); pt prev = *s.begin(); for (long long i = 2; i < s.size(); i++) { pt p3 = s[i]; while (hool.size() > 1) { pt p1 = hool[hool.size() - 2]; pt p2 = hool[hool.size() - 1]; auto pr = vec_prod((p2 - p1), (p3 - p2)); if (pr <= 0) { if (!(p1 == p3)) hool.pop_back(); else break; } else { break; } } hool.push_back(p3); } hool.pop_back(); return hool; } void add(string& s, const long long& mod, const long long& angle) { s += to_string(mod) + "$" + to_string(angle) + "#"; } string to_s(vector<pt>& hool1) { string s1; hool1.push_back(hool1[0]); hool1.push_back(hool1[1]); for (long long i = 0; i < hool1.size() - 2; i++) { add(s1, (hool1[i + 1] - hool1[i]) * (hool1[i + 1] - hool1[i]), (hool1[i + 1] - hool1[i]).angle(hool1[i + 2] - hool1[i + 1])); } return s1; } long long findsecinfirst(const string& where, long long beg) { vector<long long> p(where.size(), 0); p[0] = 0; for (long long i = 1; i < where.size(); i++) { long long j = p[i - 1]; while (j != 0 && where[j] != where[i]) { j = p[j - 1]; } if (where[j] == where[i]) ++j; p[i] = j; } long long mx = 0; for (long long i = beg; i < where.size(); i++) { mx = max(mx, p[i]); } return mx; } signed main() { ios_base::sync_with_stdio(0); string s1, s2; long long n, m; cin >> n >> m; vector<pt> eng1(n), eng2(m); cin >> eng1; cin >> eng2; vector<pt> hool1 = convex_hool(eng1); vector<pt> hool2 = convex_hool(eng2); s1, s2; s1 = to_s(hool1); s2 = to_s(hool2); s1 = s1 + s1; s1 = s2 + "%" + s1; long long max_pref = findsecinfirst(s1, s2.size()); if (max_pref == s2.size()) { cout << "YES"; } else { cout << "NO"; } }
### Prompt Please provide a Cpp coded solution to the problem described below: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long sgn(long long a) { if (a < 0) return -1; else return 1; } struct pt { long long x; long long y; pt(long long x, long long y) : x(x), y(y) {} pt() {} pt operator+(const pt& other) const { return pt(this->x + other.x, this->y + other.y); } pt operator-(const pt& other) const { return pt(this->x - other.x, this->y - other.y); } long long operator*(const pt& other) const { return (long long)(this->x) * other.x + (long long)(this->y) * other.y; } bool operator==(const pt& other) const { return x == other.x && y == other.y; } long long angle(const pt& other) const { return x * other.x + y * other.y; } }; istream& operator>>(istream& in, pt& p) { in >> p.x >> p.y; return in; } ostream& operator<<(ostream& out, const pt& p) { out << p.x << ' ' << p.y << "; "; return out; } template <typename T> istream& operator>>(istream& in, vector<T>& ar) { for (long long i = 0; i < ar.size(); i++) { in >> ar[i]; } return in; } template <typename T> ostream& operator<<(ostream& out, vector<T>& ar) { for (long long i = 0; i < ar.size(); i++) { out << ar[i] << ' '; } return out; } long long vec_prod(const pt& a, const pt& b) { return (long long)a.x * b.y - (long long)a.y * b.x; } bool operator<(const pt& a, const pt& b) { long long v = vec_prod(a, b); if (v < 0) { return 0; } if (v > 0) { return 1; } if (a * a < b * b) { return 1; } else { return 0; } } pt left_bellow(const vector<pt>& s) { pt a = s[0]; for (auto& it : s) { if (it.x < a.x) { if (it.y < a.y) { a = it; } } } return a; } vector<pt> convex_hool(vector<pt>& s) { pt lb = left_bellow(s); for (auto& it : s) { it = it - lb; } sort(s.begin(), s.end()); vector<pt> hool; s.push_back(*s.begin()); hool.push_back(s[0]); hool.push_back(s[1]); pt prev = *s.begin(); for (long long i = 2; i < s.size(); i++) { pt p3 = s[i]; while (hool.size() > 1) { pt p1 = hool[hool.size() - 2]; pt p2 = hool[hool.size() - 1]; auto pr = vec_prod((p2 - p1), (p3 - p2)); if (pr <= 0) { if (!(p1 == p3)) hool.pop_back(); else break; } else { break; } } hool.push_back(p3); } hool.pop_back(); return hool; } void add(string& s, const long long& mod, const long long& angle) { s += to_string(mod) + "$" + to_string(angle) + "#"; } string to_s(vector<pt>& hool1) { string s1; hool1.push_back(hool1[0]); hool1.push_back(hool1[1]); for (long long i = 0; i < hool1.size() - 2; i++) { add(s1, (hool1[i + 1] - hool1[i]) * (hool1[i + 1] - hool1[i]), (hool1[i + 1] - hool1[i]).angle(hool1[i + 2] - hool1[i + 1])); } return s1; } long long findsecinfirst(const string& where, long long beg) { vector<long long> p(where.size(), 0); p[0] = 0; for (long long i = 1; i < where.size(); i++) { long long j = p[i - 1]; while (j != 0 && where[j] != where[i]) { j = p[j - 1]; } if (where[j] == where[i]) ++j; p[i] = j; } long long mx = 0; for (long long i = beg; i < where.size(); i++) { mx = max(mx, p[i]); } return mx; } signed main() { ios_base::sync_with_stdio(0); string s1, s2; long long n, m; cin >> n >> m; vector<pt> eng1(n), eng2(m); cin >> eng1; cin >> eng2; vector<pt> hool1 = convex_hool(eng1); vector<pt> hool2 = convex_hool(eng2); s1, s2; s1 = to_s(hool1); s2 = to_s(hool2); s1 = s1 + s1; s1 = s2 + "%" + s1; long long max_pref = findsecinfirst(s1, s2.size()); if (max_pref == s2.size()) { cout << "YES"; } else { cout << "NO"; } } ```
#include <bits/stdc++.h> using namespace std; long long n, m, p1 = 131, p2 = 1000000007, pr[500005]; pair<long long, long long> coa[500005], cob[500005]; pair<pair<long long, long long>, long long> ugha[500005], ughb[500005]; long long crs(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { return ((a.second - b.second) * (c.first - b.first) - (c.second - b.second) * (a.first - b.first)); } bool eq(pair<long long, long long> a, pair<long long, long long> b) { if (a.first != b.first || a.second != b.second) return false; return true; } long long dis(pair<long long, long long> a, pair<long long, long long> b) { return ((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second)); } long long hsh(long long u, long long v, long long w) { u += 121; v += 2399; w += 29; return (u * p1 * p1 + v * p1 + w) % p2; } int main() { scanf("%lld%lld", &n, &m); for (long long i = 0; i < n; i++) { long long a, b; scanf("%lld%lld", &a, &b); coa[i] = {a, b}; } for (long long i = 0; i < m; i++) { long long a, b; scanf("%lld%lld", &a, &b); cob[i] = {a, b}; } sort(coa, coa + n); sort(cob, cob + m); deque<pair<long long, long long>> upa, dna, upb, dnb; for (long long i = 0; i < n; i++) { if (upa.size() < 2) { if (upa.size() < 1) upa.push_back(coa[i]); else if (!eq(coa[i], upa[upa.size() - 1])) upa.push_back(coa[i]); } else { while (crs(coa[i], upa[upa.size() - 1], upa[upa.size() - 2]) <= 0) { upa.pop_back(); if (upa.size() < 2) break; } if (upa.size() < 2) { if (upa.size() < 1) upa.push_back(coa[i]); else if (!eq(coa[i], upa[upa.size() - 1])) upa.push_back(coa[i]); } else upa.push_back(coa[i]); } } for (long long i = 0; i < n; i++) { if (dna.size() < 2) { if (dna.size() < 1) dna.push_back(coa[i]); else if (!eq(coa[i], dna[dna.size() - 1])) dna.push_back(coa[i]); } else { while (crs(coa[i], dna[dna.size() - 1], dna[dna.size() - 2]) >= 0) { dna.pop_back(); if (dna.size() < 2) break; } if (dna.size() < 2) { if (dna.size() < 1) dna.push_back(coa[i]); else if (!eq(coa[i], dna[dna.size() - 1])) dna.push_back(coa[i]); } else dna.push_back(coa[i]); } } if (upa.size() > 0 && dna.size() > 0) if (eq(upa.front(), dna.front())) upa.pop_front(); if (upa.size() > 0 && dna.size() > 0) if (eq(upa.back(), dna.back())) upa.pop_back(); if (upa.size() > 0 && dna.size() > 0) if (upa.front().first == dna.front().first && upa.front().second < dna.front().second) upa.pop_front(); if (upa.size() > 0 && dna.size() > 0) if (upa.back().first == dna.back().first && upa.back().second < dna.back().second) upa.pop_back(); while (dna.size() > 0) { upa.push_back(dna.back()); dna.pop_back(); } for (long long i = 0; i < m; i++) { if (upb.size() < 2) { if (upb.size() < 1) upb.push_back(cob[i]); else if (!eq(cob[i], upb[upb.size() - 1])) upb.push_back(cob[i]); } else { while (crs(cob[i], upb[upb.size() - 1], upb[upb.size() - 2]) <= 0) { upb.pop_back(); if (upb.size() < 2) break; } if (upb.size() < 2) { if (upb.size() < 1) upb.push_back(cob[i]); else if (!eq(cob[i], upb[upb.size() - 1])) upb.push_back(cob[i]); } else upb.push_back(cob[i]); } } for (long long i = 0; i < m; i++) { if (dnb.size() < 2) { if (dnb.size() < 1) dnb.push_back(cob[i]); else if (!eq(cob[i], dnb[dnb.size() - 1])) dnb.push_back(cob[i]); } else { while (crs(cob[i], dnb[dnb.size() - 1], dnb[dnb.size() - 2]) >= 0) { dnb.pop_back(); if (dnb.size() < 2) break; } if (dnb.size() < 2) { if (dnb.size() < 1) dnb.push_back(cob[i]); else if (!eq(cob[i], dnb[dnb.size() - 1])) dnb.push_back(cob[i]); } else dnb.push_back(cob[i]); } } if (upb.size() > 0 && dnb.size() > 0) if (eq(upb.front(), dnb.front())) upb.pop_front(); if (upb.size() > 0 && dnb.size() > 0) if (eq(upb.back(), dnb.back())) upb.pop_back(); if (upb.size() > 0 && dnb.size() > 0) if (upb.front().first == dnb.front().first && upb.front().second < dnb.front().second) upb.pop_front(); if (upb.size() > 0 && dnb.size() > 0) if (upb.back().first == dnb.back().first && upb.back().second < dnb.back().second) upb.pop_back(); while (dnb.size() > 0) { upb.push_back(dnb.back()); dnb.pop_back(); } if (upa.size() != upb.size()) { printf("NO\n"); return 0; } if (upa.size() <= 1) { printf("YES\n"); return 0; } long long sz = upa.size(); ugha[0] = {{dis(upa[0], upa[sz - 1]), dis(upa[1], upa[0])}, dis(upa[sz - 1], upa[1])}; for (long long i = 1; i < sz - 1; i++) { ugha[i] = {{dis(upa[i], upa[i - 1]), dis(upa[i], upa[i + 1])}, dis(upa[i - 1], upa[i + 1])}; } ugha[sz - 1] = {{dis(upa[sz - 1], upa[sz - 2]), dis(upa[sz - 1], upa[0])}, dis(upa[sz - 2], upa[0])}; ugha[0 + sz] = {{dis(upa[0], upa[sz - 1]), dis(upa[1], upa[0])}, dis(upa[sz - 1], upa[1])}; for (long long i = 1; i < sz - 1; i++) { ugha[i + sz] = {{dis(upa[i], upa[i - 1]), dis(upa[i], upa[i + 1])}, dis(upa[i - 1], upa[i + 1])}; } ugha[sz - 1 + sz] = { {dis(upa[sz - 1], upa[sz - 2]), dis(upa[sz - 1], upa[0])}, dis(upa[sz - 2], upa[0])}; ughb[0] = {{dis(upb[0], upb[sz - 1]), dis(upb[1], upb[0])}, dis(upb[sz - 1], upb[1])}; for (long long i = 1; i < sz - 1; i++) { ughb[i] = {{dis(upb[i], upb[i - 1]), dis(upb[i], upb[i + 1])}, dis(upb[i - 1], upb[i + 1])}; } ughb[sz - 1] = {{dis(upb[sz - 1], upb[sz - 2]), dis(upb[sz - 1], upb[0])}, dis(upb[sz - 2], upb[0])}; long long hmm = 0; for (long long i = 1; i <= sz; i++) { hmm = (hsh(ughb[i - 1].first.first, ughb[i - 1].first.second, ughb[i - 1].second) + p1 * hmm) % p2; } long long pp = 1; for (long long i = 0; i < sz; i++) pp = (pp * p1) % p2; for (long long i = 1; i <= 2 * sz; i++) { pr[i] = (hsh(ugha[i - 1].first.first, ugha[i - 1].first.second, ugha[i - 1].second) + p1 * pr[i - 1]) % p2; } for (long long i = sz + 1; i <= 2 * sz; i++) { long long hk = ((pr[i] - pr[i - sz] * pp) % p2 + p2) % p2; if (hk == hmm) { printf("YES\n"); return 0; } } printf("NO\n"); return 0; }
### Prompt Please formulate a Cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, m, p1 = 131, p2 = 1000000007, pr[500005]; pair<long long, long long> coa[500005], cob[500005]; pair<pair<long long, long long>, long long> ugha[500005], ughb[500005]; long long crs(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { return ((a.second - b.second) * (c.first - b.first) - (c.second - b.second) * (a.first - b.first)); } bool eq(pair<long long, long long> a, pair<long long, long long> b) { if (a.first != b.first || a.second != b.second) return false; return true; } long long dis(pair<long long, long long> a, pair<long long, long long> b) { return ((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second)); } long long hsh(long long u, long long v, long long w) { u += 121; v += 2399; w += 29; return (u * p1 * p1 + v * p1 + w) % p2; } int main() { scanf("%lld%lld", &n, &m); for (long long i = 0; i < n; i++) { long long a, b; scanf("%lld%lld", &a, &b); coa[i] = {a, b}; } for (long long i = 0; i < m; i++) { long long a, b; scanf("%lld%lld", &a, &b); cob[i] = {a, b}; } sort(coa, coa + n); sort(cob, cob + m); deque<pair<long long, long long>> upa, dna, upb, dnb; for (long long i = 0; i < n; i++) { if (upa.size() < 2) { if (upa.size() < 1) upa.push_back(coa[i]); else if (!eq(coa[i], upa[upa.size() - 1])) upa.push_back(coa[i]); } else { while (crs(coa[i], upa[upa.size() - 1], upa[upa.size() - 2]) <= 0) { upa.pop_back(); if (upa.size() < 2) break; } if (upa.size() < 2) { if (upa.size() < 1) upa.push_back(coa[i]); else if (!eq(coa[i], upa[upa.size() - 1])) upa.push_back(coa[i]); } else upa.push_back(coa[i]); } } for (long long i = 0; i < n; i++) { if (dna.size() < 2) { if (dna.size() < 1) dna.push_back(coa[i]); else if (!eq(coa[i], dna[dna.size() - 1])) dna.push_back(coa[i]); } else { while (crs(coa[i], dna[dna.size() - 1], dna[dna.size() - 2]) >= 0) { dna.pop_back(); if (dna.size() < 2) break; } if (dna.size() < 2) { if (dna.size() < 1) dna.push_back(coa[i]); else if (!eq(coa[i], dna[dna.size() - 1])) dna.push_back(coa[i]); } else dna.push_back(coa[i]); } } if (upa.size() > 0 && dna.size() > 0) if (eq(upa.front(), dna.front())) upa.pop_front(); if (upa.size() > 0 && dna.size() > 0) if (eq(upa.back(), dna.back())) upa.pop_back(); if (upa.size() > 0 && dna.size() > 0) if (upa.front().first == dna.front().first && upa.front().second < dna.front().second) upa.pop_front(); if (upa.size() > 0 && dna.size() > 0) if (upa.back().first == dna.back().first && upa.back().second < dna.back().second) upa.pop_back(); while (dna.size() > 0) { upa.push_back(dna.back()); dna.pop_back(); } for (long long i = 0; i < m; i++) { if (upb.size() < 2) { if (upb.size() < 1) upb.push_back(cob[i]); else if (!eq(cob[i], upb[upb.size() - 1])) upb.push_back(cob[i]); } else { while (crs(cob[i], upb[upb.size() - 1], upb[upb.size() - 2]) <= 0) { upb.pop_back(); if (upb.size() < 2) break; } if (upb.size() < 2) { if (upb.size() < 1) upb.push_back(cob[i]); else if (!eq(cob[i], upb[upb.size() - 1])) upb.push_back(cob[i]); } else upb.push_back(cob[i]); } } for (long long i = 0; i < m; i++) { if (dnb.size() < 2) { if (dnb.size() < 1) dnb.push_back(cob[i]); else if (!eq(cob[i], dnb[dnb.size() - 1])) dnb.push_back(cob[i]); } else { while (crs(cob[i], dnb[dnb.size() - 1], dnb[dnb.size() - 2]) >= 0) { dnb.pop_back(); if (dnb.size() < 2) break; } if (dnb.size() < 2) { if (dnb.size() < 1) dnb.push_back(cob[i]); else if (!eq(cob[i], dnb[dnb.size() - 1])) dnb.push_back(cob[i]); } else dnb.push_back(cob[i]); } } if (upb.size() > 0 && dnb.size() > 0) if (eq(upb.front(), dnb.front())) upb.pop_front(); if (upb.size() > 0 && dnb.size() > 0) if (eq(upb.back(), dnb.back())) upb.pop_back(); if (upb.size() > 0 && dnb.size() > 0) if (upb.front().first == dnb.front().first && upb.front().second < dnb.front().second) upb.pop_front(); if (upb.size() > 0 && dnb.size() > 0) if (upb.back().first == dnb.back().first && upb.back().second < dnb.back().second) upb.pop_back(); while (dnb.size() > 0) { upb.push_back(dnb.back()); dnb.pop_back(); } if (upa.size() != upb.size()) { printf("NO\n"); return 0; } if (upa.size() <= 1) { printf("YES\n"); return 0; } long long sz = upa.size(); ugha[0] = {{dis(upa[0], upa[sz - 1]), dis(upa[1], upa[0])}, dis(upa[sz - 1], upa[1])}; for (long long i = 1; i < sz - 1; i++) { ugha[i] = {{dis(upa[i], upa[i - 1]), dis(upa[i], upa[i + 1])}, dis(upa[i - 1], upa[i + 1])}; } ugha[sz - 1] = {{dis(upa[sz - 1], upa[sz - 2]), dis(upa[sz - 1], upa[0])}, dis(upa[sz - 2], upa[0])}; ugha[0 + sz] = {{dis(upa[0], upa[sz - 1]), dis(upa[1], upa[0])}, dis(upa[sz - 1], upa[1])}; for (long long i = 1; i < sz - 1; i++) { ugha[i + sz] = {{dis(upa[i], upa[i - 1]), dis(upa[i], upa[i + 1])}, dis(upa[i - 1], upa[i + 1])}; } ugha[sz - 1 + sz] = { {dis(upa[sz - 1], upa[sz - 2]), dis(upa[sz - 1], upa[0])}, dis(upa[sz - 2], upa[0])}; ughb[0] = {{dis(upb[0], upb[sz - 1]), dis(upb[1], upb[0])}, dis(upb[sz - 1], upb[1])}; for (long long i = 1; i < sz - 1; i++) { ughb[i] = {{dis(upb[i], upb[i - 1]), dis(upb[i], upb[i + 1])}, dis(upb[i - 1], upb[i + 1])}; } ughb[sz - 1] = {{dis(upb[sz - 1], upb[sz - 2]), dis(upb[sz - 1], upb[0])}, dis(upb[sz - 2], upb[0])}; long long hmm = 0; for (long long i = 1; i <= sz; i++) { hmm = (hsh(ughb[i - 1].first.first, ughb[i - 1].first.second, ughb[i - 1].second) + p1 * hmm) % p2; } long long pp = 1; for (long long i = 0; i < sz; i++) pp = (pp * p1) % p2; for (long long i = 1; i <= 2 * sz; i++) { pr[i] = (hsh(ugha[i - 1].first.first, ugha[i - 1].first.second, ugha[i - 1].second) + p1 * pr[i - 1]) % p2; } for (long long i = sz + 1; i <= 2 * sz; i++) { long long hk = ((pr[i] - pr[i - sz] * pp) % p2 + p2) % p2; if (hk == hmm) { printf("YES\n"); return 0; } } printf("NO\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N_MAX = 1e5 + 5; const int INF = 1e9 + 5; struct point { int x, y; point(int _x = 0, int _y = 0) : x(_x), y(_y) {} point &operator+=(const point &other) { x += other.x; y += other.y; return *this; } point &operator-=(const point &other) { x -= other.x; y -= other.y; return *this; } point operator+(const point &other) const { return point(*this) += other; } point operator-(const point &other) const { return point(*this) -= other; } bool operator==(const point &other) const { return x == other.x && y == other.y; } bool operator!=(const point &other) const { return !(*this == other); } }; long long cross(const point &a, const point &b) { return (long long)a.x * b.y - (long long)b.x * a.y; } long long dot(const point &a, const point &b) { return (long long)a.x * b.x + (long long)a.y * b.y; } long long norm(const point &p) { return dot(p, p); } bool left_turn(const point &a, const point &b, const point &c) { return cross(b - a, c - b) > 0; } double dist(const point &a, const point &b) { return sqrt(norm(a - b)); } struct convex_hull { int N, H; point points[N_MAX], hull[N_MAX]; double hull_perim() { double perim = 0; for (int i = 0; i < H; i++) perim += dist(hull[i], hull[i + 1]); return perim; } long long hull_area_doubled() { long long area = 0; for (int i = 0; i < H; i++) area += cross(hull[i], hull[i + 1]); return area; } static bool graham_compare(const point &a, const point &b) { long long cp = cross(a, b); return cp > 0 || (cp == 0 && dot(a, b - a) > 0); } void graham_scan() { point bottom(INF, INF); int bottom_index = -1; for (int i = 0; i < N; i++) if (make_pair(points[i].y, points[i].x) < make_pair(bottom.y, bottom.x)) { bottom = points[i]; bottom_index = i; } int n = 0; for (int i = 0; i < N; i++) if (points[i] != bottom || i == bottom_index) points[n++] = points[i]; N = n; for (int i = 0; i < N; i++) points[i] -= bottom; swap(points[0], points[bottom_index]); sort(points + 1, points + N, graham_compare); for (int i = 0; i < N; i++) points[i] += bottom; H = 0; for (int i = 0; i < N; i++) { while (H >= 2 && !left_turn(hull[H - 2], hull[H - 1], points[i])) H--; hull[H++] = points[i]; } while (H >= 3 && !left_turn(hull[H - 2], hull[H - 1], hull[0])) H--; hull[H] = hull[0]; hull[H + 1] = hull[1]; if (H > 1) { for (int i = 0; i < H; i++) assert(hull[i] != hull[i + 1]); } } vector<long long> get_sequence() { vector<long long> seq; for (int i = 0; i < H; i++) { seq.push_back(norm(hull[i + 1] - hull[i])); seq.push_back(-norm(hull[i + 2] - hull[i])); } return seq; } }; convex_hull engines[2]; template <typename T> bool kmp_cyclic(vector<T> sequence, const vector<T> &pattern) { int n = sequence.size(), m = pattern.size(); if (n != m) return false; for (int i = 0; i < n; i++) sequence.push_back(sequence[i]); n *= 2; vector<int> fail(m + 1, 0); int p = 0; for (int i = 1; i < m; i++) { while (p > 0 && pattern[p] != pattern[i]) p = fail[p]; if (pattern[p] == pattern[i]) p++; fail[i + 1] = p; } p = 0; for (int i = 0; i < n; i++) { while (p > 0 && pattern[p] != sequence[i]) p = fail[p]; if (pattern[p] == sequence[i]) p++; if (p == m) { p = fail[p]; return true; } } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> engines[0].N >> engines[1].N; for (int e = 0; e < 2; e++) for (int i = 0; i < engines[e].N; i++) cin >> engines[e].points[i].x >> engines[e].points[i].y; engines[0].graham_scan(); engines[1].graham_scan(); bool isomorphic = kmp_cyclic(engines[0].get_sequence(), engines[1].get_sequence()); cout << (isomorphic ? "YES" : "NO") << '\n'; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N_MAX = 1e5 + 5; const int INF = 1e9 + 5; struct point { int x, y; point(int _x = 0, int _y = 0) : x(_x), y(_y) {} point &operator+=(const point &other) { x += other.x; y += other.y; return *this; } point &operator-=(const point &other) { x -= other.x; y -= other.y; return *this; } point operator+(const point &other) const { return point(*this) += other; } point operator-(const point &other) const { return point(*this) -= other; } bool operator==(const point &other) const { return x == other.x && y == other.y; } bool operator!=(const point &other) const { return !(*this == other); } }; long long cross(const point &a, const point &b) { return (long long)a.x * b.y - (long long)b.x * a.y; } long long dot(const point &a, const point &b) { return (long long)a.x * b.x + (long long)a.y * b.y; } long long norm(const point &p) { return dot(p, p); } bool left_turn(const point &a, const point &b, const point &c) { return cross(b - a, c - b) > 0; } double dist(const point &a, const point &b) { return sqrt(norm(a - b)); } struct convex_hull { int N, H; point points[N_MAX], hull[N_MAX]; double hull_perim() { double perim = 0; for (int i = 0; i < H; i++) perim += dist(hull[i], hull[i + 1]); return perim; } long long hull_area_doubled() { long long area = 0; for (int i = 0; i < H; i++) area += cross(hull[i], hull[i + 1]); return area; } static bool graham_compare(const point &a, const point &b) { long long cp = cross(a, b); return cp > 0 || (cp == 0 && dot(a, b - a) > 0); } void graham_scan() { point bottom(INF, INF); int bottom_index = -1; for (int i = 0; i < N; i++) if (make_pair(points[i].y, points[i].x) < make_pair(bottom.y, bottom.x)) { bottom = points[i]; bottom_index = i; } int n = 0; for (int i = 0; i < N; i++) if (points[i] != bottom || i == bottom_index) points[n++] = points[i]; N = n; for (int i = 0; i < N; i++) points[i] -= bottom; swap(points[0], points[bottom_index]); sort(points + 1, points + N, graham_compare); for (int i = 0; i < N; i++) points[i] += bottom; H = 0; for (int i = 0; i < N; i++) { while (H >= 2 && !left_turn(hull[H - 2], hull[H - 1], points[i])) H--; hull[H++] = points[i]; } while (H >= 3 && !left_turn(hull[H - 2], hull[H - 1], hull[0])) H--; hull[H] = hull[0]; hull[H + 1] = hull[1]; if (H > 1) { for (int i = 0; i < H; i++) assert(hull[i] != hull[i + 1]); } } vector<long long> get_sequence() { vector<long long> seq; for (int i = 0; i < H; i++) { seq.push_back(norm(hull[i + 1] - hull[i])); seq.push_back(-norm(hull[i + 2] - hull[i])); } return seq; } }; convex_hull engines[2]; template <typename T> bool kmp_cyclic(vector<T> sequence, const vector<T> &pattern) { int n = sequence.size(), m = pattern.size(); if (n != m) return false; for (int i = 0; i < n; i++) sequence.push_back(sequence[i]); n *= 2; vector<int> fail(m + 1, 0); int p = 0; for (int i = 1; i < m; i++) { while (p > 0 && pattern[p] != pattern[i]) p = fail[p]; if (pattern[p] == pattern[i]) p++; fail[i + 1] = p; } p = 0; for (int i = 0; i < n; i++) { while (p > 0 && pattern[p] != sequence[i]) p = fail[p]; if (pattern[p] == sequence[i]) p++; if (p == m) { p = fail[p]; return true; } } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> engines[0].N >> engines[1].N; for (int e = 0; e < 2; e++) for (int i = 0; i < engines[e].N; i++) cin >> engines[e].points[i].x >> engines[e].points[i].y; engines[0].graham_scan(); engines[1].graham_scan(); bool isomorphic = kmp_cyclic(engines[0].get_sequence(), engines[1].get_sequence()); cout << (isomorphic ? "YES" : "NO") << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 123, inf = 1e9, mod = 1e9 + 7; const double eps = 1e-9; struct pt { int x, y; }; bool cmp(pt a, pt b) { return a.x < b.x || a.x == b.x && a.y < b.y; } bool cw(pt a, pt b, pt c) { return 1ll * a.x * (b.y - c.y) + 1ll * b.x * (c.y - a.y) + 1ll * c.x * (a.y - b.y) < 0; } bool ccw(pt a, pt b, pt c) { return 1ll * a.x * (b.y - c.y) + 1ll * b.x * (c.y - a.y) + 1ll * c.x * (a.y - b.y) > 0; } void convex_hull(vector<pt>& a) { if (a.size() == 1) return; sort(a.begin(), a.end(), &cmp); pt p1 = a[0], p2 = a.back(); vector<pt> up, down; up.push_back(p1); down.push_back(p1); for (size_t i = 1; i < a.size(); ++i) { if (i == a.size() - 1 || cw(p1, a[i], p2)) { while (up.size() >= 2 && !cw(up[up.size() - 2], up[up.size() - 1], a[i])) up.pop_back(); up.push_back(a[i]); } if (i == a.size() - 1 || ccw(p1, a[i], p2)) { while (down.size() >= 2 && !ccw(down[down.size() - 2], down[down.size() - 1], a[i])) down.pop_back(); down.push_back(a[i]); } } a.clear(); for (size_t i = 0; i < up.size(); ++i) a.push_back(up[i]); for (size_t i = down.size() - 2; i > 0; --i) a.push_back(down[i]); } long long dis(pt a, pt b) { return 1ll * (a.x - b.x) * (a.x - b.x) + 1ll * (a.y - b.y) * (a.y - b.y); } pair<double, double> get(pt q, pt w, pt e) { long long a, b, c; a = dis(q, w); b = dis(q, e); c = dis(w, e); return {1.0 * (a + b - c) / (2 * sqrtl(a) * sqrtl(b)), sqrtl(a)}; } vector<int> z_function(vector<pair<double, double> > second) { int n = (int)second.size(); vector<int> z(n); for (int i = 1, l = 0, r = 0; i < n; ++i) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while (i + z[i] < n && fabs(second[z[i]].first - second[i + z[i]].first) < eps && fabs(second[z[i]].second - second[i + z[i]].second) < eps) ++z[i]; if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; } return z; } int main() { int n, m; scanf("%d%d", &n, &m); vector<pt> a, b; for (int i = 0; i < n; i++) { int x, y; scanf("%d%d", &x, &y); a.push_back({x, y}); } convex_hull(a); for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); b.push_back({x, y}); } convex_hull(b); if (a.size() != b.size()) { puts("NO"); return 0; } vector<pair<double, double> > second, t; for (int i = 0; i < a.size(); i++) second.push_back( get(a[i], a[(i - 1 + a.size()) % a.size()], a[(i + 1) % a.size()])); for (int i = 0; i < b.size(); i++) t.push_back( get(b[i], b[(i - 1 + b.size()) % b.size()], b[(i + 1) % b.size()])); t.push_back({-1e9, -1e9}); for (int i = 0; i < a.size(); i++) t.push_back(second[i]); for (int i = 0; i < a.size(); i++) t.push_back(second[i]); vector<int> z = z_function(t); int good = 0; for (int i = 0; i < z.size(); i++) if (z[i] >= b.size()) good++; if (good <= 0) puts("NO"); else puts("YES"); }
### Prompt Please create a solution in Cpp to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 123, inf = 1e9, mod = 1e9 + 7; const double eps = 1e-9; struct pt { int x, y; }; bool cmp(pt a, pt b) { return a.x < b.x || a.x == b.x && a.y < b.y; } bool cw(pt a, pt b, pt c) { return 1ll * a.x * (b.y - c.y) + 1ll * b.x * (c.y - a.y) + 1ll * c.x * (a.y - b.y) < 0; } bool ccw(pt a, pt b, pt c) { return 1ll * a.x * (b.y - c.y) + 1ll * b.x * (c.y - a.y) + 1ll * c.x * (a.y - b.y) > 0; } void convex_hull(vector<pt>& a) { if (a.size() == 1) return; sort(a.begin(), a.end(), &cmp); pt p1 = a[0], p2 = a.back(); vector<pt> up, down; up.push_back(p1); down.push_back(p1); for (size_t i = 1; i < a.size(); ++i) { if (i == a.size() - 1 || cw(p1, a[i], p2)) { while (up.size() >= 2 && !cw(up[up.size() - 2], up[up.size() - 1], a[i])) up.pop_back(); up.push_back(a[i]); } if (i == a.size() - 1 || ccw(p1, a[i], p2)) { while (down.size() >= 2 && !ccw(down[down.size() - 2], down[down.size() - 1], a[i])) down.pop_back(); down.push_back(a[i]); } } a.clear(); for (size_t i = 0; i < up.size(); ++i) a.push_back(up[i]); for (size_t i = down.size() - 2; i > 0; --i) a.push_back(down[i]); } long long dis(pt a, pt b) { return 1ll * (a.x - b.x) * (a.x - b.x) + 1ll * (a.y - b.y) * (a.y - b.y); } pair<double, double> get(pt q, pt w, pt e) { long long a, b, c; a = dis(q, w); b = dis(q, e); c = dis(w, e); return {1.0 * (a + b - c) / (2 * sqrtl(a) * sqrtl(b)), sqrtl(a)}; } vector<int> z_function(vector<pair<double, double> > second) { int n = (int)second.size(); vector<int> z(n); for (int i = 1, l = 0, r = 0; i < n; ++i) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while (i + z[i] < n && fabs(second[z[i]].first - second[i + z[i]].first) < eps && fabs(second[z[i]].second - second[i + z[i]].second) < eps) ++z[i]; if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; } return z; } int main() { int n, m; scanf("%d%d", &n, &m); vector<pt> a, b; for (int i = 0; i < n; i++) { int x, y; scanf("%d%d", &x, &y); a.push_back({x, y}); } convex_hull(a); for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); b.push_back({x, y}); } convex_hull(b); if (a.size() != b.size()) { puts("NO"); return 0; } vector<pair<double, double> > second, t; for (int i = 0; i < a.size(); i++) second.push_back( get(a[i], a[(i - 1 + a.size()) % a.size()], a[(i + 1) % a.size()])); for (int i = 0; i < b.size(); i++) t.push_back( get(b[i], b[(i - 1 + b.size()) % b.size()], b[(i + 1) % b.size()])); t.push_back({-1e9, -1e9}); for (int i = 0; i < a.size(); i++) t.push_back(second[i]); for (int i = 0; i < a.size(); i++) t.push_back(second[i]); vector<int> z = z_function(t); int good = 0; for (int i = 0; i < z.size(); i++) if (z[i] >= b.size()) good++; if (good <= 0) puts("NO"); else puts("YES"); } ```
#include <bits/stdc++.h> using namespace std; using namespace placeholders; template <class T> void mini(T& l, T r) { l = min(l, r); } template <class T> void maxi(T& l, T r) { l = max(l, r); } template <class TH> void _dbg(const char* sdbg, TH h) { cerr << sdbg << "=" << h << "\n"; } template <class TH, class... TA> void _dbg(const char* sdbg, TH h, TA... a) { while (*sdbg != ',') { cerr << *sdbg++; } cerr << "=" << h << ","; _dbg(sdbg + 1, a...); } template <class T> ostream& operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template <class L, class R> ostream& operator<<(ostream& os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } struct VEC { long long x, y; }; inline long long cmp(long long a, long long b) { return a - b; } inline long long cmp(const VEC& a, const VEC& b) { long long at = cmp(a.x, b.x); return !at ? cmp(a.y, b.y) : at; } inline VEC operator-(const VEC& a, const VEC& b) { return (VEC){a.x - b.x, a.y - b.y}; } inline long long operator*(const VEC& a, const VEC& b) { return a.x * b.y - a.y * b.x; } inline long long operator%(const VEC& a, const VEC& b) { return a.x * b.x + a.y * b.y; } inline bool operator<(const VEC& a, const VEC& b) { return cmp(a, b) < 0; } inline bool operator==(const VEC& a, const VEC& b) { return !cmp(a, b); } long long cmp_side(const VEC& a, const VEC& b) { return cmp(a.x * b.y, +a.y * b.x); } vector<VEC> convex_hull(vector<VEC> u) { sort(u.begin(), u.end()); u.erase(unique(u.begin(), u.end()), u.end()); if (u.size() < 3) return u; vector<VEC> c; for (size_t i = 0, o = 1, m = 1; ~i; i += o) { while (c.size() > m) { VEC a = c.back() - c[c.size() - 2]; VEC b = c.back() - u[i]; if (cmp_side(a, b) < 0) break; c.pop_back(); } c.push_back(u[i]); if (i + 1 == u.size()) m = c.size(), o = -1; } c.pop_back(); return c; } const int N = 2E5 + 10; pair<long long, pair<long long, long long> > c[N], d[N]; inline long long sqr(long long x) { return x * x; } inline long long sqr(const VEC& a) { return sqr(a.x) + sqr(a.y); } int kmp(int ls, pair<long long, pair<long long, long long> > s[], long long lp, pair<long long, pair<long long, long long> > p[]) { static int fail[N] = {-1}; for (int i, j = 1; j < lp; ++j) { for (i = fail[j - 1]; i >= 0 && p[i + 1] != p[j]; i = fail[i]) ; fail[j] = p[i + 1] != p[j] ? i + 1 : -1; } for (int i = 0, j = 0; i < ls && j < lp; ++i) if (s[i] == p[j]) { ++j; if (j == lp) return true; } else if (j) j = fail[j - 1] + 1, --i; return false; } bool check(vector<VEC>& a, vector<VEC>& b) { int m = a.size(); a.push_back(a[0]); a.push_back(a[1]); b.push_back(b[0]); b.push_back(b[1]); for (int i = 1; i + 1 < a.size(); ++i) { c[i - 1] = make_pair(sqr(a[i] - a[i - 1]), make_pair((a[i] - a[i - 1]) * (a[i + 1] - a[i]), (a[i] - a[i - 1]) % (a[i + 1] - a[i]))); } for (int i = 1; i + 1 < b.size(); ++i) { d[i - 1] = make_pair(sqr(b[i] - b[i - 1]), make_pair((b[i] - b[i - 1]) * (b[i + 1] - b[i]), (b[i] - b[i - 1]) % (b[i + 1] - b[i]))); } copy(c, c + m, c + m); int ret = kmp(m * 2, c, m, d); a.pop_back(); a.pop_back(); b.pop_back(); b.pop_back(); return ret; } int main() { int n, m; scanf("%d%d", &n, &m); vector<VEC> a(n), b(m); for (int i = 0; i < n; ++i) scanf("%lld%lld", &a[i].x, &a[i].y); a = convex_hull(a); for (int i = 0; i < m; ++i) scanf("%lld%lld", &b[i].x, &b[i].y); b = convex_hull(b); if (a.size() != b.size()) { puts("NO"); return 0; } bool ans = check(a, b); reverse(b.begin(), b.end()); ans |= check(a, b); puts(ans ? "YES" : "NO"); return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using namespace placeholders; template <class T> void mini(T& l, T r) { l = min(l, r); } template <class T> void maxi(T& l, T r) { l = max(l, r); } template <class TH> void _dbg(const char* sdbg, TH h) { cerr << sdbg << "=" << h << "\n"; } template <class TH, class... TA> void _dbg(const char* sdbg, TH h, TA... a) { while (*sdbg != ',') { cerr << *sdbg++; } cerr << "=" << h << ","; _dbg(sdbg + 1, a...); } template <class T> ostream& operator<<(ostream& os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template <class L, class R> ostream& operator<<(ostream& os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } struct VEC { long long x, y; }; inline long long cmp(long long a, long long b) { return a - b; } inline long long cmp(const VEC& a, const VEC& b) { long long at = cmp(a.x, b.x); return !at ? cmp(a.y, b.y) : at; } inline VEC operator-(const VEC& a, const VEC& b) { return (VEC){a.x - b.x, a.y - b.y}; } inline long long operator*(const VEC& a, const VEC& b) { return a.x * b.y - a.y * b.x; } inline long long operator%(const VEC& a, const VEC& b) { return a.x * b.x + a.y * b.y; } inline bool operator<(const VEC& a, const VEC& b) { return cmp(a, b) < 0; } inline bool operator==(const VEC& a, const VEC& b) { return !cmp(a, b); } long long cmp_side(const VEC& a, const VEC& b) { return cmp(a.x * b.y, +a.y * b.x); } vector<VEC> convex_hull(vector<VEC> u) { sort(u.begin(), u.end()); u.erase(unique(u.begin(), u.end()), u.end()); if (u.size() < 3) return u; vector<VEC> c; for (size_t i = 0, o = 1, m = 1; ~i; i += o) { while (c.size() > m) { VEC a = c.back() - c[c.size() - 2]; VEC b = c.back() - u[i]; if (cmp_side(a, b) < 0) break; c.pop_back(); } c.push_back(u[i]); if (i + 1 == u.size()) m = c.size(), o = -1; } c.pop_back(); return c; } const int N = 2E5 + 10; pair<long long, pair<long long, long long> > c[N], d[N]; inline long long sqr(long long x) { return x * x; } inline long long sqr(const VEC& a) { return sqr(a.x) + sqr(a.y); } int kmp(int ls, pair<long long, pair<long long, long long> > s[], long long lp, pair<long long, pair<long long, long long> > p[]) { static int fail[N] = {-1}; for (int i, j = 1; j < lp; ++j) { for (i = fail[j - 1]; i >= 0 && p[i + 1] != p[j]; i = fail[i]) ; fail[j] = p[i + 1] != p[j] ? i + 1 : -1; } for (int i = 0, j = 0; i < ls && j < lp; ++i) if (s[i] == p[j]) { ++j; if (j == lp) return true; } else if (j) j = fail[j - 1] + 1, --i; return false; } bool check(vector<VEC>& a, vector<VEC>& b) { int m = a.size(); a.push_back(a[0]); a.push_back(a[1]); b.push_back(b[0]); b.push_back(b[1]); for (int i = 1; i + 1 < a.size(); ++i) { c[i - 1] = make_pair(sqr(a[i] - a[i - 1]), make_pair((a[i] - a[i - 1]) * (a[i + 1] - a[i]), (a[i] - a[i - 1]) % (a[i + 1] - a[i]))); } for (int i = 1; i + 1 < b.size(); ++i) { d[i - 1] = make_pair(sqr(b[i] - b[i - 1]), make_pair((b[i] - b[i - 1]) * (b[i + 1] - b[i]), (b[i] - b[i - 1]) % (b[i + 1] - b[i]))); } copy(c, c + m, c + m); int ret = kmp(m * 2, c, m, d); a.pop_back(); a.pop_back(); b.pop_back(); b.pop_back(); return ret; } int main() { int n, m; scanf("%d%d", &n, &m); vector<VEC> a(n), b(m); for (int i = 0; i < n; ++i) scanf("%lld%lld", &a[i].x, &a[i].y); a = convex_hull(a); for (int i = 0; i < m; ++i) scanf("%lld%lld", &b[i].x, &b[i].y); b = convex_hull(b); if (a.size() != b.size()) { puts("NO"); return 0; } bool ans = check(a, b); reverse(b.begin(), b.end()); ans |= check(a, b); puts(ans ? "YES" : "NO"); return 0; } ```
#include <bits/stdc++.h> #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; void JIZZ(string output = "") { cout << output; exit(0); } const long double PI = 3.14159265358979323846264338327950288; const long double eps = 1e-13; long long mods[] = {1000000000 + 7, 1000000000 + 9, 1000000000 + 87, 1000000000 + 123, 1000000000 + 861, 1000000000 + 4329, 1000000000 + 25261}; long long prms[] = {7, 13, 47017}; long long mod1, mod2; pair<long long, long long> dif(pair<long long, long long> a, pair<long long, long long> b) { return pair<long long, long long>(a.first - b.first, a.second - b.second); } long long cr(pair<long long, long long> a, pair<long long, long long> b) { return a.first * b.second - a.second * b.first; } pair<vector<pair<long long, long long>>, vector<pair<long long, long long>>> doit(long long n) { vector<pair<long long, long long>> pt(n); for (auto &p : pt) cin >> p.first >> p.second; sort(pt.begin(), pt.end()); long long tz = 0; vector<long long> tbm; auto usle = [&](pair<long long, long long> &a, pair<long long, long long> &b, pair<long long, long long> &c) -> bool { return cr(dif(b, a), dif(c, a)) >= 0; }; for (long long i = 0; i < n; ++i) { while (tz > 1 && usle(pt[tbm[tz - 1]], pt[i], pt[tbm[tz - 2]])) tbm.pop_back(), --tz; tbm.push_back(i); ++tz; } long long ttz = tz; for (long long i = n - 2; i >= 0; --i) { while (tz > ttz && usle(pt[tbm[tz - 1]], pt[i], pt[tbm[tz - 2]])) tbm.pop_back(), --tz; tbm.push_back(i); ++tz; } --tz; ; ; vector<pair<long long, long long>> tbp(tz); for (long long i = 0; i < tz; ++i) tbp[i] = pt[tbm[i]]; ; ; for (pair<long long, long long> &p : tbp) p.first *= tz, p.second *= tz; long long xtt = 0, ytt = 0; for (pair<long long, long long> &p : tbp) xtt += p.first, ytt += p.second; xtt /= tz; ytt /= tz; ; ; vector<pair<long long, long long>> rt1(tz), rt2(tz); for (long long i = 0; i < tz; ++i) { long long dx = abs(tbp[i].first - xtt); long long dy = abs(tbp[i].second - ytt); auto doit2 = [&](long long p) -> long long { long long tdx = dx % p, tdy = dy % p; long long ddd = tdx * tdx + tdy * tdy; return ddd % p; }; rt1[i].first = doit2(mod1); rt2[i].first = doit2(mod2); } for (long long i = 0; i < tz; ++i) { pair<long long, long long> p1 = tbp[i], p2 = tbp[i == tz - 1 ? 0 : i + 1]; pair<long long, long long> d = dif(p2, p1); auto doit2 = [&](long long p) -> long long { long long tdx = abs(d.first) % p, tdy = abs(d.second) % p; long long ddd = tdx * tdx + tdy * tdy; return ddd % p; }; rt1[i].second = doit2(mod1); rt2[i].second = doit2(mod2); }; ; return make_pair(rt1, rt2); } bool ok(vector<pair<long long, long long>> &vap, vector<pair<long long, long long>> &vbp, const long long mod) { if (vap.size() != vbp.size()) return 0; long long len = vap.size(); ; ; long long p = prms[rand() % 3]; vector<long long> va(1 + len), vb(1 + len); for (long long i = 0; i < len; ++i) { va[i + 1] = (vap[i].first * 7122 + (vap[i].second ^ 7122)) % mod; vb[i + 1] = (vbp[i].first * 7122 + (vbp[i].second ^ 7122)) % mod; } vector<long long> ha(1 + len * 2), hb(1 + len), pp(1 + len * 2); pp[0] = 1; for (long long i = 1; i <= len * 2; ++i) pp[i] = pp[i - 1] * p % mod; for (long long i = 1; i <= len * 2; ++i) ha[i] = (ha[i - 1] + va[i > len ? i - len : i] * pp[i]) % mod; for (long long i = 1; i <= len; ++i) hb[i] = (hb[i - 1] + vb[i] * pp[i]) % mod; ; ; for (auto o = 0u; o < vb.size(); ++o) { if ((ha[o + len] - ha[o] + mod) % mod == (hb[len] * pp[o] % mod)) return 1; } return 0; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); srand(clock()); long long n, m; cin >> n >> m; mod1 = mods[rand() % 7], mod2 = mods[rand() % 7]; auto rn = doit(n); auto rm = doit(m); if (ok(rn.first, rm.first, mod1) && ok(rn.second, rm.second, mod2)) cout << "YES" << endl; else cout << "NO" << endl; }
### Prompt Please create a solution in CPP to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; void JIZZ(string output = "") { cout << output; exit(0); } const long double PI = 3.14159265358979323846264338327950288; const long double eps = 1e-13; long long mods[] = {1000000000 + 7, 1000000000 + 9, 1000000000 + 87, 1000000000 + 123, 1000000000 + 861, 1000000000 + 4329, 1000000000 + 25261}; long long prms[] = {7, 13, 47017}; long long mod1, mod2; pair<long long, long long> dif(pair<long long, long long> a, pair<long long, long long> b) { return pair<long long, long long>(a.first - b.first, a.second - b.second); } long long cr(pair<long long, long long> a, pair<long long, long long> b) { return a.first * b.second - a.second * b.first; } pair<vector<pair<long long, long long>>, vector<pair<long long, long long>>> doit(long long n) { vector<pair<long long, long long>> pt(n); for (auto &p : pt) cin >> p.first >> p.second; sort(pt.begin(), pt.end()); long long tz = 0; vector<long long> tbm; auto usle = [&](pair<long long, long long> &a, pair<long long, long long> &b, pair<long long, long long> &c) -> bool { return cr(dif(b, a), dif(c, a)) >= 0; }; for (long long i = 0; i < n; ++i) { while (tz > 1 && usle(pt[tbm[tz - 1]], pt[i], pt[tbm[tz - 2]])) tbm.pop_back(), --tz; tbm.push_back(i); ++tz; } long long ttz = tz; for (long long i = n - 2; i >= 0; --i) { while (tz > ttz && usle(pt[tbm[tz - 1]], pt[i], pt[tbm[tz - 2]])) tbm.pop_back(), --tz; tbm.push_back(i); ++tz; } --tz; ; ; vector<pair<long long, long long>> tbp(tz); for (long long i = 0; i < tz; ++i) tbp[i] = pt[tbm[i]]; ; ; for (pair<long long, long long> &p : tbp) p.first *= tz, p.second *= tz; long long xtt = 0, ytt = 0; for (pair<long long, long long> &p : tbp) xtt += p.first, ytt += p.second; xtt /= tz; ytt /= tz; ; ; vector<pair<long long, long long>> rt1(tz), rt2(tz); for (long long i = 0; i < tz; ++i) { long long dx = abs(tbp[i].first - xtt); long long dy = abs(tbp[i].second - ytt); auto doit2 = [&](long long p) -> long long { long long tdx = dx % p, tdy = dy % p; long long ddd = tdx * tdx + tdy * tdy; return ddd % p; }; rt1[i].first = doit2(mod1); rt2[i].first = doit2(mod2); } for (long long i = 0; i < tz; ++i) { pair<long long, long long> p1 = tbp[i], p2 = tbp[i == tz - 1 ? 0 : i + 1]; pair<long long, long long> d = dif(p2, p1); auto doit2 = [&](long long p) -> long long { long long tdx = abs(d.first) % p, tdy = abs(d.second) % p; long long ddd = tdx * tdx + tdy * tdy; return ddd % p; }; rt1[i].second = doit2(mod1); rt2[i].second = doit2(mod2); }; ; return make_pair(rt1, rt2); } bool ok(vector<pair<long long, long long>> &vap, vector<pair<long long, long long>> &vbp, const long long mod) { if (vap.size() != vbp.size()) return 0; long long len = vap.size(); ; ; long long p = prms[rand() % 3]; vector<long long> va(1 + len), vb(1 + len); for (long long i = 0; i < len; ++i) { va[i + 1] = (vap[i].first * 7122 + (vap[i].second ^ 7122)) % mod; vb[i + 1] = (vbp[i].first * 7122 + (vbp[i].second ^ 7122)) % mod; } vector<long long> ha(1 + len * 2), hb(1 + len), pp(1 + len * 2); pp[0] = 1; for (long long i = 1; i <= len * 2; ++i) pp[i] = pp[i - 1] * p % mod; for (long long i = 1; i <= len * 2; ++i) ha[i] = (ha[i - 1] + va[i > len ? i - len : i] * pp[i]) % mod; for (long long i = 1; i <= len; ++i) hb[i] = (hb[i - 1] + vb[i] * pp[i]) % mod; ; ; for (auto o = 0u; o < vb.size(); ++o) { if ((ha[o + len] - ha[o] + mod) % mod == (hb[len] * pp[o] % mod)) return 1; } return 0; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); srand(clock()); long long n, m; cin >> n >> m; mod1 = mods[rand() % 7], mod2 = mods[rand() % 7]; auto rn = doit(n); auto rm = doit(m); if (ok(rn.first, rm.first, mod1) && ok(rn.second, rm.second, mod2)) cout << "YES" << endl; else cout << "NO" << endl; } ```
#include <bits/stdc++.h> using namespace std; const double pi = 3.1415926535897; const double eps = 1e-8; inline int read() { int x = 0, sgn = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1; for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); return sgn * x; } int dcmp(double x) { if (fabs(x) < eps) return 0; return (x < 0) ? -1 : 1; } struct vec { double x, y; vec(double xx = 0, double yy = 0) { x = xx, y = yy; } vec operator+(vec b) { return vec(x + b.x, y + b.y); } vec operator-(vec b) { return vec(x - b.x, y - b.y); } vec operator*(double p) { return vec(x * p, y * p); } vec operator/(double p) { return vec(x / p, y / p); } vec rot(double e) { return vec(x * cos(e) - y * sin(e), x * sin(e) + y * cos(e)); } }; double dot(vec a, vec b) { return a.x * b.x + a.y * b.y; } double crs(vec a, vec b) { return a.x * b.y - b.x * a.y; } double len(vec a) { return dot(a, a); } double angle(vec a, vec b) { double x = dot(a, b); if (dcmp(x) == 0) return 0; return (x / len(a) / len(b)); } using namespace std; bool eq(vec a, vec b) { return (dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0); } bool cmp(vec a, vec b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } inline int ConvexHull(vec *p, int n, vec *ch) { sort(p, p + n, cmp); int k, i, m = 0; for (i = 0; i < n; i++) { while (m > 1 && dcmp(crs(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) <= 0) m--; ch[m++] = p[i]; } k = m; for (i = n - 2; i >= 0; i--) { while (m > k && dcmp(crs(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) <= 0) m--; ch[m++] = p[i]; } if (n > 1) m--; return m; } int n, m, n1, n2, j, z[200010]; double x, y, a1[200010 << 2], a2[200010 << 2]; vec p[200010], q[200010], c1[200010], c2[200010]; inline void getp(double *a2, int m) { z[0] = j = 0; for (int i = 1; i < m; i++) { while (j > 0 && dcmp(a2[i] - a2[j]) != 0) j = z[j - 1]; if (dcmp(a2[i] - a2[j]) == 0) ++j; z[i] = j; } } inline int kmp(double *a1, double *a2, int n, int m) { getp(a2, m); j = 0; for (int i = 0; i < n; i++) { while (j > 0 && dcmp(a2[j] - a1[i]) != 0) j = z[j - 1]; if (dcmp(a2[j] - a1[i]) == 0) j++; if (j == m) return 1; } return 0; } int main() { n = read(), m = read(); for (int i = 0; i < n; i++) { x = read(), y = read(); p[i] = vec(x, y); } n1 = ConvexHull(p, n, c1); for (int i = 0; i < m; i++) { x = read(), y = read(); q[i] = vec(x, y); } n2 = ConvexHull(q, m, c2); if (n1 != n2) printf("NO"); else { n = n1; for (int i = 0; i < n; i++) a1[i * 2] = angle(c1[(i - 1 + n) % n] - c1[i], c1[(i + 1) % n] - c1[i]), a1[i * 2 + 1] = len(c1[(i + 1) % n] - c1[i]); for (int i = 0; i < n; i++) a2[i * 2] = angle(c2[(i - 1 + n) % n] - c2[i], c2[(i + 1) % n] - c2[i]), a2[i * 2 + 1] = len(c2[(i + 1) % n] - c2[i]); for (int i = 2 * n; i < 4 * n; i++) a1[i] = a1[i - 2 * n]; int flag = kmp(a1, a2, 4 * n, 2 * n); printf("%s\n", flag ? "YES" : "NO"); } return 0; }
### Prompt In CPP, your task is to solve the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double pi = 3.1415926535897; const double eps = 1e-8; inline int read() { int x = 0, sgn = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1; for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); return sgn * x; } int dcmp(double x) { if (fabs(x) < eps) return 0; return (x < 0) ? -1 : 1; } struct vec { double x, y; vec(double xx = 0, double yy = 0) { x = xx, y = yy; } vec operator+(vec b) { return vec(x + b.x, y + b.y); } vec operator-(vec b) { return vec(x - b.x, y - b.y); } vec operator*(double p) { return vec(x * p, y * p); } vec operator/(double p) { return vec(x / p, y / p); } vec rot(double e) { return vec(x * cos(e) - y * sin(e), x * sin(e) + y * cos(e)); } }; double dot(vec a, vec b) { return a.x * b.x + a.y * b.y; } double crs(vec a, vec b) { return a.x * b.y - b.x * a.y; } double len(vec a) { return dot(a, a); } double angle(vec a, vec b) { double x = dot(a, b); if (dcmp(x) == 0) return 0; return (x / len(a) / len(b)); } using namespace std; bool eq(vec a, vec b) { return (dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0); } bool cmp(vec a, vec b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } inline int ConvexHull(vec *p, int n, vec *ch) { sort(p, p + n, cmp); int k, i, m = 0; for (i = 0; i < n; i++) { while (m > 1 && dcmp(crs(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) <= 0) m--; ch[m++] = p[i]; } k = m; for (i = n - 2; i >= 0; i--) { while (m > k && dcmp(crs(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2])) <= 0) m--; ch[m++] = p[i]; } if (n > 1) m--; return m; } int n, m, n1, n2, j, z[200010]; double x, y, a1[200010 << 2], a2[200010 << 2]; vec p[200010], q[200010], c1[200010], c2[200010]; inline void getp(double *a2, int m) { z[0] = j = 0; for (int i = 1; i < m; i++) { while (j > 0 && dcmp(a2[i] - a2[j]) != 0) j = z[j - 1]; if (dcmp(a2[i] - a2[j]) == 0) ++j; z[i] = j; } } inline int kmp(double *a1, double *a2, int n, int m) { getp(a2, m); j = 0; for (int i = 0; i < n; i++) { while (j > 0 && dcmp(a2[j] - a1[i]) != 0) j = z[j - 1]; if (dcmp(a2[j] - a1[i]) == 0) j++; if (j == m) return 1; } return 0; } int main() { n = read(), m = read(); for (int i = 0; i < n; i++) { x = read(), y = read(); p[i] = vec(x, y); } n1 = ConvexHull(p, n, c1); for (int i = 0; i < m; i++) { x = read(), y = read(); q[i] = vec(x, y); } n2 = ConvexHull(q, m, c2); if (n1 != n2) printf("NO"); else { n = n1; for (int i = 0; i < n; i++) a1[i * 2] = angle(c1[(i - 1 + n) % n] - c1[i], c1[(i + 1) % n] - c1[i]), a1[i * 2 + 1] = len(c1[(i + 1) % n] - c1[i]); for (int i = 0; i < n; i++) a2[i * 2] = angle(c2[(i - 1 + n) % n] - c2[i], c2[(i + 1) % n] - c2[i]), a2[i * 2 + 1] = len(c2[(i + 1) % n] - c2[i]); for (int i = 2 * n; i < 4 * n; i++) a1[i] = a1[i - 2 * n]; int flag = kmp(a1, a2, 4 * n, 2 * n); printf("%s\n", flag ? "YES" : "NO"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long double EPS = 1e-6; struct Point { long long x, y; Point(long long int x, long long int y) : x(x), y(y) {} Point() {} Point operator-(Point p) { return {x - p.x, y - p.y}; } Point operator+(Point p) { return {x + p.x, y + p.y}; } }; long long cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } long long dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } long long len2(Point p) { return p.x * p.x + p.y * p.y; } Point mn; bool cmp(Point &a, Point &b) { long long c = cross(a - mn, b - mn); if (c > 0) return true; if (c < 0) return false; return len2(a - mn) < len2(b - mn); } vector<Point> hull(vector<Point> &a) { auto n = (int)a.size(); mn = a[0]; for (int i = 1; i < n; i++) { if (a[i].y < mn.y || (a[i].y == mn.y && a[i].x < mn.x)) { mn = a[i]; } } sort(a.begin(), a.end(), cmp); vector<Point> st; for (auto p : a) { while (st.size() >= 2 && cross(st[st.size() - 1] - st[st.size() - 2], p - st[st.size() - 1]) <= 0) { st.pop_back(); } st.push_back(p); } return st; } struct Side { long double len, angle; Side(long double len, long double angle) : len(len), angle(angle) {} Side(Point v1, Point v2) { len = hypot(v1.x, v1.y); angle = atan2(cross(v1, v2), dot(v1, v2)); } }; bool eq(Side &a, Side &b) { return abs(a.len - b.len) < EPS && abs(a.angle - b.angle) < EPS; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<Point> a(n), b(m); for (int i = 0; i < n; i++) { cin >> a[i].x >> a[i].y; } a = hull(a); n = (int)a.size(); for (int i = 0; i < m; i++) { cin >> b[i].x >> b[i].y; } b = hull(b); m = (int)b.size(); vector<Side> s; for (int i = 0; i < m; i++) { s.emplace_back(b[(i + 1) % m] - b[i], b[(i + 2) % m] - b[(i + 1) % m]); } s.emplace_back(-1, 0); for (int i = 0; i < n; i++) { s.emplace_back(a[(i + 1) % n] - a[i], a[(i + 2) % n] - a[(i + 1) % n]); } for (int i = 0; i < n; i++) { s.emplace_back(a[(i + 1) % n] - a[i], a[(i + 2) % n] - a[(i + 1) % n]); } vector<int> p(s.size()); int k = 0; for (int i = 1; i < p.size(); i++) { while (k > 0 && !eq(s[i], s[k])) { k = p[k - 1]; } if (eq(s[i], s[k])) { k++; } p[i] = k; if (p[i] == m) { cout << "YES" << endl; return 0; } } cout << "NO" << endl; return 0; }
### Prompt Construct a Cpp code solution to the problem outlined: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long double EPS = 1e-6; struct Point { long long x, y; Point(long long int x, long long int y) : x(x), y(y) {} Point() {} Point operator-(Point p) { return {x - p.x, y - p.y}; } Point operator+(Point p) { return {x + p.x, y + p.y}; } }; long long cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } long long dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } long long len2(Point p) { return p.x * p.x + p.y * p.y; } Point mn; bool cmp(Point &a, Point &b) { long long c = cross(a - mn, b - mn); if (c > 0) return true; if (c < 0) return false; return len2(a - mn) < len2(b - mn); } vector<Point> hull(vector<Point> &a) { auto n = (int)a.size(); mn = a[0]; for (int i = 1; i < n; i++) { if (a[i].y < mn.y || (a[i].y == mn.y && a[i].x < mn.x)) { mn = a[i]; } } sort(a.begin(), a.end(), cmp); vector<Point> st; for (auto p : a) { while (st.size() >= 2 && cross(st[st.size() - 1] - st[st.size() - 2], p - st[st.size() - 1]) <= 0) { st.pop_back(); } st.push_back(p); } return st; } struct Side { long double len, angle; Side(long double len, long double angle) : len(len), angle(angle) {} Side(Point v1, Point v2) { len = hypot(v1.x, v1.y); angle = atan2(cross(v1, v2), dot(v1, v2)); } }; bool eq(Side &a, Side &b) { return abs(a.len - b.len) < EPS && abs(a.angle - b.angle) < EPS; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<Point> a(n), b(m); for (int i = 0; i < n; i++) { cin >> a[i].x >> a[i].y; } a = hull(a); n = (int)a.size(); for (int i = 0; i < m; i++) { cin >> b[i].x >> b[i].y; } b = hull(b); m = (int)b.size(); vector<Side> s; for (int i = 0; i < m; i++) { s.emplace_back(b[(i + 1) % m] - b[i], b[(i + 2) % m] - b[(i + 1) % m]); } s.emplace_back(-1, 0); for (int i = 0; i < n; i++) { s.emplace_back(a[(i + 1) % n] - a[i], a[(i + 2) % n] - a[(i + 1) % n]); } for (int i = 0; i < n; i++) { s.emplace_back(a[(i + 1) % n] - a[i], a[(i + 2) % n] - a[(i + 1) % n]); } vector<int> p(s.size()); int k = 0; for (int i = 1; i < p.size(); i++) { while (k > 0 && !eq(s[i], s[k])) { k = p[k - 1]; } if (eq(s[i], s[k])) { k++; } p[i] = k; if (p[i] == m) { cout << "YES" << endl; return 0; } } cout << "NO" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const double eps = 1e-6; const double pi = acos(-1.0); double radToDeg(double theta) { return theta * 180.0 / pi; } pair<double, double> toVec(pair<double, double>& a, pair<double, double>& b) { return pair<double, double>(b.first - a.first, b.second - a.second); } double dot(pair<double, double>& a, pair<double, double>& b) { return (a.first * b.first + a.second * b.second); } double norm_sq(pair<double, double>& v) { return v.first * v.first + v.second * v.second; } double angle(pair<double, double>& a, pair<double, double>& o, pair<double, double>& b) { pair<double, double> oa = toVec(o, a), ob = toVec(o, b); double ans = acos(dot(oa, ob) / sqrt(norm_sq(oa) * norm_sq(ob))); return radToDeg(ans); } double cross(pair<double, double>& a, pair<double, double>& b) { return a.first * b.second - a.second * b.first; } double cross(pair<double, double>& O, pair<double, double>& A, pair<double, double>& B) { return (A.first - O.first) * (B.second - O.second) - (A.second - O.second) * (B.first - O.first); } vector<pair<double, double> > convexHull(vector<pair<double, double> >& P) { int n = P.size(), k = 0; vector<pair<double, double> > H(2 * n); sort(P.begin(), P.end()); for (int i = 0; i < n; i++) { while (k >= 2 && cross(H[k - 2], H[k - 1], P[i]) <= 0) k--; H[k++] = P[i]; } for (int i = n - 2, t = k + 1; i >= 0; i--) { while (k >= t && cross(H[k - 2], H[k - 1], P[i]) <= 0) k--; H[k++] = P[i]; } H.resize(k); return H; } double dist(pair<double, double>& p1, pair<double, double>& p2) { return hypot(p1.first - p2.first, p1.second - p2.second); } bool equal(const vector<double>& a, const vector<double>& b) { return fabs(a[0] - b[0]) < eps && fabs(a[1] - b[1]) < eps && fabs(a[2] - b[2]) < eps; } int kmp(const vector<vector<double> >& T, const vector<vector<double> >& P) { if (P.empty()) return 0; vector<int> pi(P.size(), 0); for (int i = 1, k = 0; i < P.size(); ++i) { while (k && !equal(P[k], P[i])) k = pi[k - 1]; if (equal(P[k], P[i])) ++k; pi[i] = k; } for (int i = 0, k = 0; i < T.size(); ++i) { while (k && !equal(P[k], T[i])) k = pi[k - 1]; if (equal(P[k], T[i])) ++k; if (k == P.size()) return i - k + 1; } return -1; } int main() { int n, m; scanf("%d%d", &n, &m); vector<pair<double, double> > a(n, pair<double, double>(0, 0)), b(m, pair<double, double>(0, 0)); for (int i = 0; i < n; i++) scanf("%lf%lf", &a[i].first, &a[i].second); for (int i = 0; i < m; i++) scanf("%lf%lf", &b[i].first, &b[i].second); vector<pair<double, double> > aconv = convexHull(a), bconv = convexHull(b); aconv.pop_back(); bconv.pop_back(); int aconv_size = aconv.size(); for (int i = 0; i < aconv_size; i++) aconv.push_back(aconv[i]); vector<vector<double> > T, P; for (int i = 0; i < aconv.size(); i++) { int prev = i - 1, cur = i, next = i + 1; if (i == 0) prev = aconv.size() - 1; if (i == aconv.size() - 1) next = 0; vector<double> tmp; tmp.push_back(dist(aconv[prev], aconv[cur])); tmp.push_back(angle(aconv[prev], aconv[cur], aconv[next])); tmp.push_back(dist(aconv[cur], aconv[next])); T.push_back(tmp); } for (int i = 0; i < bconv.size(); i++) { int prev = i - 1, cur = i, next = i + 1; if (i == 0) prev = bconv.size() - 1; if (i == bconv.size() - 1) next = 0; vector<double> tmp; tmp.push_back(dist(bconv[prev], bconv[cur])); tmp.push_back(angle(bconv[prev], bconv[cur], bconv[next])); tmp.push_back(dist(bconv[cur], bconv[next])); P.push_back(tmp); } int index = kmp(T, P); if (index != -1) { printf("YES\n"); } else { printf("NO\n"); } return 0; }
### Prompt Develop a solution in cpp to the problem described below: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int inf = 1e9; const double eps = 1e-6; const double pi = acos(-1.0); double radToDeg(double theta) { return theta * 180.0 / pi; } pair<double, double> toVec(pair<double, double>& a, pair<double, double>& b) { return pair<double, double>(b.first - a.first, b.second - a.second); } double dot(pair<double, double>& a, pair<double, double>& b) { return (a.first * b.first + a.second * b.second); } double norm_sq(pair<double, double>& v) { return v.first * v.first + v.second * v.second; } double angle(pair<double, double>& a, pair<double, double>& o, pair<double, double>& b) { pair<double, double> oa = toVec(o, a), ob = toVec(o, b); double ans = acos(dot(oa, ob) / sqrt(norm_sq(oa) * norm_sq(ob))); return radToDeg(ans); } double cross(pair<double, double>& a, pair<double, double>& b) { return a.first * b.second - a.second * b.first; } double cross(pair<double, double>& O, pair<double, double>& A, pair<double, double>& B) { return (A.first - O.first) * (B.second - O.second) - (A.second - O.second) * (B.first - O.first); } vector<pair<double, double> > convexHull(vector<pair<double, double> >& P) { int n = P.size(), k = 0; vector<pair<double, double> > H(2 * n); sort(P.begin(), P.end()); for (int i = 0; i < n; i++) { while (k >= 2 && cross(H[k - 2], H[k - 1], P[i]) <= 0) k--; H[k++] = P[i]; } for (int i = n - 2, t = k + 1; i >= 0; i--) { while (k >= t && cross(H[k - 2], H[k - 1], P[i]) <= 0) k--; H[k++] = P[i]; } H.resize(k); return H; } double dist(pair<double, double>& p1, pair<double, double>& p2) { return hypot(p1.first - p2.first, p1.second - p2.second); } bool equal(const vector<double>& a, const vector<double>& b) { return fabs(a[0] - b[0]) < eps && fabs(a[1] - b[1]) < eps && fabs(a[2] - b[2]) < eps; } int kmp(const vector<vector<double> >& T, const vector<vector<double> >& P) { if (P.empty()) return 0; vector<int> pi(P.size(), 0); for (int i = 1, k = 0; i < P.size(); ++i) { while (k && !equal(P[k], P[i])) k = pi[k - 1]; if (equal(P[k], P[i])) ++k; pi[i] = k; } for (int i = 0, k = 0; i < T.size(); ++i) { while (k && !equal(P[k], T[i])) k = pi[k - 1]; if (equal(P[k], T[i])) ++k; if (k == P.size()) return i - k + 1; } return -1; } int main() { int n, m; scanf("%d%d", &n, &m); vector<pair<double, double> > a(n, pair<double, double>(0, 0)), b(m, pair<double, double>(0, 0)); for (int i = 0; i < n; i++) scanf("%lf%lf", &a[i].first, &a[i].second); for (int i = 0; i < m; i++) scanf("%lf%lf", &b[i].first, &b[i].second); vector<pair<double, double> > aconv = convexHull(a), bconv = convexHull(b); aconv.pop_back(); bconv.pop_back(); int aconv_size = aconv.size(); for (int i = 0; i < aconv_size; i++) aconv.push_back(aconv[i]); vector<vector<double> > T, P; for (int i = 0; i < aconv.size(); i++) { int prev = i - 1, cur = i, next = i + 1; if (i == 0) prev = aconv.size() - 1; if (i == aconv.size() - 1) next = 0; vector<double> tmp; tmp.push_back(dist(aconv[prev], aconv[cur])); tmp.push_back(angle(aconv[prev], aconv[cur], aconv[next])); tmp.push_back(dist(aconv[cur], aconv[next])); T.push_back(tmp); } for (int i = 0; i < bconv.size(); i++) { int prev = i - 1, cur = i, next = i + 1; if (i == 0) prev = bconv.size() - 1; if (i == bconv.size() - 1) next = 0; vector<double> tmp; tmp.push_back(dist(bconv[prev], bconv[cur])); tmp.push_back(angle(bconv[prev], bconv[cur], bconv[next])); tmp.push_back(dist(bconv[cur], bconv[next])); P.push_back(tmp); } int index = kmp(T, P); if (index != -1) { printf("YES\n"); } else { printf("NO\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int INF = 0x3F3F3F3F; const int MOD = 1e9 + 7; const int MAX = 1e5 + 5; int N, M; pair<int, int> pts1[MAX], pts2[MAX]; stack<pair<int, int> > h1, h2; vector<double> hull1, hull2; int lps[2 * MAX]; inline long long int orientation(pair<int, int> a, pair<int, int> b, pair<int, int> c) { return (long long int)(b.second - a.second) * (c.first - b.first) - (long long int)(c.second - b.second) * (b.first - a.first); } inline double dist(pair<int, int> p1, pair<int, int> p2) { long long int x = p1.first - p2.first, y = p1.second - p2.second; return sqrt(x * x + y * y); } inline double angle(pair<int, int> p1, pair<int, int> p2, pair<int, int> p3) { long long int x1 = p1.first - p2.first, y1 = p1.second - p2.second; long long int x2 = p3.first - p2.first, y2 = p3.second - p2.second; long long int dot = x1 * x2 + y1 * y2, det = x1 * y2 - y1 * x2; double d1 = dist(p1, p2), d2 = dist(p2, p3); return atan2(det, dot) * (d1 / d2); } inline void popStack(stack<pair<int, int> > &st, vector<pair<int, int> > &hull) { while (st.size() > 1) { hull.push_back({st.top().first, st.top().second}); st.pop(); } } void process(pair<int, int> p, stack<pair<int, int> > &st) { bool cw = false; while (st.size() > 1 && !cw) { pair<int, int> b = st.top(); st.pop(); pair<int, int> a = st.top(); if (orientation(a, b, p) > 0) { cw = true; st.push(b); } } st.push(p); } void findHull(int N, pair<int, int> arr[], vector<double> &hull) { vector<pair<int, int> > v; while (!h1.empty()) { h1.pop(); } while (!h2.empty()) { h2.pop(); } sort(arr, arr + N); for (int i = 0; i < N; i++) { process(arr[i], h1); } for (int i = N - 1; i >= 0; i--) { process(arr[i], h2); } popStack(h1, v); popStack(h2, v); for (int i = 0; i < v.size(); i++) { pair<int, int> p1 = v[i], p2 = v[(i + 1) % v.size()], p3 = v[(i + 2) % v.size()]; hull.push_back(dist(p1, p2)); hull.push_back(angle(p1, p2, p3)); } } bool kmp(vector<double> &T, vector<double> &S) { int N = (int)S.size(), M = (int)T.size(); int len = 0; lps[0] = 0; int i = 1, j = 0; while (i < M) { if (abs(T[i] - T[len]) < 1e-9) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } i = 0, j = 0; while (i < N) { if (abs(T[j] - S[i]) < 1e-9) { j++, i++; } if (j == M) { return 1; } else if (i < N && abs(T[j] - S[i]) > 1e-9) { if (j != 0) { j = lps[j - 1]; } else { i++; } } } return 0; } int main() { scanf("%d%d", &N, &M); for (int i = 0; i < N; i++) { scanf("%d%d", &pts1[i].first, &pts1[i].second); } for (int i = 0; i < M; i++) { scanf("%d%d", &pts2[i].first, &pts2[i].second); } findHull(N, pts1, hull1); findHull(M, pts2, hull2); if (hull1.size() != hull2.size()) { printf("NO\n"); } else { int n = (int)hull1.size(); for (int i = 0; i < n; i++) { hull1.push_back(hull1[i]); } printf(kmp(hull2, hull1) ? "YES\n" : "NO\n"); } return 0; }
### Prompt Create a solution in CPP for the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 0x3F3F3F3F; const int MOD = 1e9 + 7; const int MAX = 1e5 + 5; int N, M; pair<int, int> pts1[MAX], pts2[MAX]; stack<pair<int, int> > h1, h2; vector<double> hull1, hull2; int lps[2 * MAX]; inline long long int orientation(pair<int, int> a, pair<int, int> b, pair<int, int> c) { return (long long int)(b.second - a.second) * (c.first - b.first) - (long long int)(c.second - b.second) * (b.first - a.first); } inline double dist(pair<int, int> p1, pair<int, int> p2) { long long int x = p1.first - p2.first, y = p1.second - p2.second; return sqrt(x * x + y * y); } inline double angle(pair<int, int> p1, pair<int, int> p2, pair<int, int> p3) { long long int x1 = p1.first - p2.first, y1 = p1.second - p2.second; long long int x2 = p3.first - p2.first, y2 = p3.second - p2.second; long long int dot = x1 * x2 + y1 * y2, det = x1 * y2 - y1 * x2; double d1 = dist(p1, p2), d2 = dist(p2, p3); return atan2(det, dot) * (d1 / d2); } inline void popStack(stack<pair<int, int> > &st, vector<pair<int, int> > &hull) { while (st.size() > 1) { hull.push_back({st.top().first, st.top().second}); st.pop(); } } void process(pair<int, int> p, stack<pair<int, int> > &st) { bool cw = false; while (st.size() > 1 && !cw) { pair<int, int> b = st.top(); st.pop(); pair<int, int> a = st.top(); if (orientation(a, b, p) > 0) { cw = true; st.push(b); } } st.push(p); } void findHull(int N, pair<int, int> arr[], vector<double> &hull) { vector<pair<int, int> > v; while (!h1.empty()) { h1.pop(); } while (!h2.empty()) { h2.pop(); } sort(arr, arr + N); for (int i = 0; i < N; i++) { process(arr[i], h1); } for (int i = N - 1; i >= 0; i--) { process(arr[i], h2); } popStack(h1, v); popStack(h2, v); for (int i = 0; i < v.size(); i++) { pair<int, int> p1 = v[i], p2 = v[(i + 1) % v.size()], p3 = v[(i + 2) % v.size()]; hull.push_back(dist(p1, p2)); hull.push_back(angle(p1, p2, p3)); } } bool kmp(vector<double> &T, vector<double> &S) { int N = (int)S.size(), M = (int)T.size(); int len = 0; lps[0] = 0; int i = 1, j = 0; while (i < M) { if (abs(T[i] - T[len]) < 1e-9) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } i = 0, j = 0; while (i < N) { if (abs(T[j] - S[i]) < 1e-9) { j++, i++; } if (j == M) { return 1; } else if (i < N && abs(T[j] - S[i]) > 1e-9) { if (j != 0) { j = lps[j - 1]; } else { i++; } } } return 0; } int main() { scanf("%d%d", &N, &M); for (int i = 0; i < N; i++) { scanf("%d%d", &pts1[i].first, &pts1[i].second); } for (int i = 0; i < M; i++) { scanf("%d%d", &pts2[i].first, &pts2[i].second); } findHull(N, pts1, hull1); findHull(M, pts2, hull2); if (hull1.size() != hull2.size()) { printf("NO\n"); } else { int n = (int)hull1.size(); for (int i = 0; i < n; i++) { hull1.push_back(hull1[i]); } printf(kmp(hull2, hull1) ? "YES\n" : "NO\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long N = 100007; bool cmp(pair<long long, long long> x, pair<long long, long long> y) { if (x.first < y.first) return 1; if (x.first > y.first) return 0; return x.second < y.second; } long long Area(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { return a.first * b.second + b.first * c.second + c.first * a.second - a.first * c.second - c.first * b.second - b.first * a.second; } pair<long long, long long> a[N], b[N]; vector<pair<long long, long long> > va; vector<pair<long long, long long> > vb; bool cmp2(pair<long long, long long> x, pair<long long, long long> y) { if (Area(a[0], x, y) == 0) return x < y; return (Area(a[0], x, y) > 0); } bool cmp3(pair<long long, long long> x, pair<long long, long long> y) { if (Area(b[0], x, y) == 0) return x < y; return (Area(b[0], x, y) > 0); } clock_t t; long long dist(pair<long long, long long> p1, pair<long long, long long> p2) { return (p1.first - p2.first) * (p1.first - p2.first) + (p1.second - p2.second) * (p1.second - p2.second); } int main() { long long n, m, i; pair<long long, long long> p; ios::sync_with_stdio(false); cin.tie(NULL); t = clock(); cin >> n >> m; long long con = 1000; for (i = 0; i < n; i++) cin >> a[i].first >> a[i].second; for (i = 0; i < m; i++) cin >> b[i].first >> b[i].second; sort(a, a + n, cmp); sort(b, b + m, cmp); sort(a + 1, a + n, cmp2); sort(b + 1, b + m, cmp3); va.push_back(a[0]); va.push_back(a[1]); for (i = 2; i < n; i++) { p = va.back(); va.pop_back(); if (!va.empty() && Area(va.back(), p, a[i]) <= 0) i--; else { va.push_back(p); va.push_back(a[i]); } } vb.push_back(b[0]); vb.push_back(b[1]); for (i = 2; i < m; i++) { p = vb.back(); vb.pop_back(); if (!vb.empty() && Area(vb.back(), p, b[i]) <= 0) i--; else { vb.push_back(p); vb.push_back(b[i]); } } if (va.size() != vb.size()) { cout << "NO\n"; return 0; } int x, y, x2, y2, j; for (i = 0; i < vb.size(); i++) { for (j = 0; j < con; j++) { x = rand() % va.size(); y = rand() % va.size(); x2 = (x + i) % vb.size(); y2 = (y + i) % vb.size(); if (dist(va[x], va[y]) != dist(vb[x2], vb[y2])) break; } if (j == con) { cout << "YES\n"; return 0; } } cout << "NO\n"; return 0; }
### Prompt Please formulate a Cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 100007; bool cmp(pair<long long, long long> x, pair<long long, long long> y) { if (x.first < y.first) return 1; if (x.first > y.first) return 0; return x.second < y.second; } long long Area(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { return a.first * b.second + b.first * c.second + c.first * a.second - a.first * c.second - c.first * b.second - b.first * a.second; } pair<long long, long long> a[N], b[N]; vector<pair<long long, long long> > va; vector<pair<long long, long long> > vb; bool cmp2(pair<long long, long long> x, pair<long long, long long> y) { if (Area(a[0], x, y) == 0) return x < y; return (Area(a[0], x, y) > 0); } bool cmp3(pair<long long, long long> x, pair<long long, long long> y) { if (Area(b[0], x, y) == 0) return x < y; return (Area(b[0], x, y) > 0); } clock_t t; long long dist(pair<long long, long long> p1, pair<long long, long long> p2) { return (p1.first - p2.first) * (p1.first - p2.first) + (p1.second - p2.second) * (p1.second - p2.second); } int main() { long long n, m, i; pair<long long, long long> p; ios::sync_with_stdio(false); cin.tie(NULL); t = clock(); cin >> n >> m; long long con = 1000; for (i = 0; i < n; i++) cin >> a[i].first >> a[i].second; for (i = 0; i < m; i++) cin >> b[i].first >> b[i].second; sort(a, a + n, cmp); sort(b, b + m, cmp); sort(a + 1, a + n, cmp2); sort(b + 1, b + m, cmp3); va.push_back(a[0]); va.push_back(a[1]); for (i = 2; i < n; i++) { p = va.back(); va.pop_back(); if (!va.empty() && Area(va.back(), p, a[i]) <= 0) i--; else { va.push_back(p); va.push_back(a[i]); } } vb.push_back(b[0]); vb.push_back(b[1]); for (i = 2; i < m; i++) { p = vb.back(); vb.pop_back(); if (!vb.empty() && Area(vb.back(), p, b[i]) <= 0) i--; else { vb.push_back(p); vb.push_back(b[i]); } } if (va.size() != vb.size()) { cout << "NO\n"; return 0; } int x, y, x2, y2, j; for (i = 0; i < vb.size(); i++) { for (j = 0; j < con; j++) { x = rand() % va.size(); y = rand() % va.size(); x2 = (x + i) % vb.size(); y2 = (y + i) % vb.size(); if (dist(va[x], va[y]) != dist(vb[x2], vb[y2])) break; } if (j == con) { cout << "YES\n"; return 0; } } cout << "NO\n"; return 0; } ```
#include <bits/stdc++.h> int sgn(const long long &x) { return x < 0 ? -1 : x > 0; } int cmp(const long long &x, const long long &y) { return sgn(x - y); } long long sqr(const long long &x) { return x * x; } struct point { long long x, y; explicit point(const long long &x = 0, const long long &y = 0) : x(x), y(y) {} int dim() const { return sgn(y) == 0 ? sgn(x) > 0 : sgn(y) > 0; } point rot90() const { return point(-y, x); } point _rot90() const { return point(y, -x); } }; bool operator<(const point &a, const point &b) { return a.x == b.x ? a.y < b.y : a.x < b.x; } point operator-(const point &a) { return point(-a.x, -a.y); } point operator+(const point &a, const point &b) { return point(a.x + b.x, a.y + b.y); } point operator-(const point &a, const point &b) { return point(a.x - b.x, a.y - b.y); } point operator*(const point &a, const long long &b) { return point(a.x * b, a.y * b); } point operator/(const point &a, const long long &b) { return point(a.x / b, a.y / b); } long long dot(const point &a, const point &b) { return a.x * b.x + a.y * b.y; } long long det(const point &a, const point &b) { return a.x * b.y - a.y * b.x; } long long dis2(const point &a, const point &b = point()) { return sqr(a.x - b.x) + sqr(a.y - b.y); } bool turn_left(const point &a, const point &b, const point &c) { return sgn(det(b - a, c - a)) >= 0; } std::vector<point> convex_hull(std::vector<point> a) { int cnt = 0; std::sort(a.begin(), a.end()); static std::vector<point> ret; ret.resize(a.size() << 1); for (int i = 0; i < (int)a.size(); ++i) { while (cnt > 1 && turn_left(ret[cnt - 2], a[i], ret[cnt - 1])) --cnt; ret[cnt++] = a[i]; } int fixed = cnt; for (int i = (int)a.size() - 1; i >= 0; --i) { while (cnt > fixed && turn_left(ret[cnt - 2], a[i], ret[cnt - 1])) --cnt; ret[cnt++] = a[i]; } return std::vector<point>(ret.begin(), ret.begin() + cnt - 1); } struct node { long long a, b, c; node(const long long &a = 0, const long long &b = 0, const long long &c = 0) : a(a), b(b), c(c) {} }; bool operator==(const node &a, const node &b) { return a.a == b.a && a.b == b.b && a.c == b.c; } bool operator<(const node &a, const node &b) { if (a.a != b.a) return a.a < b.a; if (a.b != b.b) return a.b < b.b; return a.c < b.c; } bool operator>(const node &a, const node &b) { if (a.a != b.a) return a.a > b.a; if (a.b != b.b) return a.b > b.b; return a.c > b.c; } int min_rep(const std::vector<node> &str) { int n = str.size(); if (n <= 1) return 0; int p = 0, q = 1; while (p < n && q < n) { if (str[p] < str[q]) { q++; } else if (str[p] > str[q]) { p = q; q++; } else { int i, l = p, r = q; for (i = 0; i < n; i++) { int ll = l % n, rr = r % n; if (str[ll] < str[rr]) { q = r; break; } else if (str[ll] > str[rr]) { p = q; q = rr; break; } l++; r++; } if (i == n) break; } } return p; } int N, M; std::vector<point> p, q; std::vector<node> a, b; int main() { std::ios::sync_with_stdio(0); std::cin >> N >> M; p.resize(N); q.resize(M); for (int i = 0; i < N; ++i) std::cin >> p[i].x >> p[i].y; for (int i = 0; i < M; ++i) std::cin >> q[i].x >> q[i].y; std::vector<point> cvp = convex_hull(p), cvq = convex_hull(q); for (int i = 0; i < cvp.size(); ++i) { int j = (i + 1) % cvp.size(), k = (i - 1 + cvp.size()) % cvp.size(); a.emplace_back(dis2(cvp[i], cvp[j]), dot(cvp[k] - cvp[i], cvp[j] - cvp[i]), det(cvp[k] - cvp[i], cvp[j] - cvp[i])); } for (int i = 0; i < cvq.size(); ++i) { int j = (i + 1) % cvq.size(), k = (i - 1 + cvq.size()) % cvq.size(); b.emplace_back(dis2(cvq[i], cvq[j]), dot(cvq[k] - cvq[i], cvq[j] - cvq[i]), det(cvq[k] - cvq[i], cvq[j] - cvq[i])); } int n = min_rep(a), m = min_rep(b); if (a.size() != b.size()) return std::cout << "NO\n", 0; for (int i = 0; i < a.size(); ++i) { if (!(a[(n + i) % a.size()] == b[(m + i) % b.size()])) return std::cout << "NO\n", 0; } std::cout << "YES\n"; }
### Prompt Please formulate a cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> int sgn(const long long &x) { return x < 0 ? -1 : x > 0; } int cmp(const long long &x, const long long &y) { return sgn(x - y); } long long sqr(const long long &x) { return x * x; } struct point { long long x, y; explicit point(const long long &x = 0, const long long &y = 0) : x(x), y(y) {} int dim() const { return sgn(y) == 0 ? sgn(x) > 0 : sgn(y) > 0; } point rot90() const { return point(-y, x); } point _rot90() const { return point(y, -x); } }; bool operator<(const point &a, const point &b) { return a.x == b.x ? a.y < b.y : a.x < b.x; } point operator-(const point &a) { return point(-a.x, -a.y); } point operator+(const point &a, const point &b) { return point(a.x + b.x, a.y + b.y); } point operator-(const point &a, const point &b) { return point(a.x - b.x, a.y - b.y); } point operator*(const point &a, const long long &b) { return point(a.x * b, a.y * b); } point operator/(const point &a, const long long &b) { return point(a.x / b, a.y / b); } long long dot(const point &a, const point &b) { return a.x * b.x + a.y * b.y; } long long det(const point &a, const point &b) { return a.x * b.y - a.y * b.x; } long long dis2(const point &a, const point &b = point()) { return sqr(a.x - b.x) + sqr(a.y - b.y); } bool turn_left(const point &a, const point &b, const point &c) { return sgn(det(b - a, c - a)) >= 0; } std::vector<point> convex_hull(std::vector<point> a) { int cnt = 0; std::sort(a.begin(), a.end()); static std::vector<point> ret; ret.resize(a.size() << 1); for (int i = 0; i < (int)a.size(); ++i) { while (cnt > 1 && turn_left(ret[cnt - 2], a[i], ret[cnt - 1])) --cnt; ret[cnt++] = a[i]; } int fixed = cnt; for (int i = (int)a.size() - 1; i >= 0; --i) { while (cnt > fixed && turn_left(ret[cnt - 2], a[i], ret[cnt - 1])) --cnt; ret[cnt++] = a[i]; } return std::vector<point>(ret.begin(), ret.begin() + cnt - 1); } struct node { long long a, b, c; node(const long long &a = 0, const long long &b = 0, const long long &c = 0) : a(a), b(b), c(c) {} }; bool operator==(const node &a, const node &b) { return a.a == b.a && a.b == b.b && a.c == b.c; } bool operator<(const node &a, const node &b) { if (a.a != b.a) return a.a < b.a; if (a.b != b.b) return a.b < b.b; return a.c < b.c; } bool operator>(const node &a, const node &b) { if (a.a != b.a) return a.a > b.a; if (a.b != b.b) return a.b > b.b; return a.c > b.c; } int min_rep(const std::vector<node> &str) { int n = str.size(); if (n <= 1) return 0; int p = 0, q = 1; while (p < n && q < n) { if (str[p] < str[q]) { q++; } else if (str[p] > str[q]) { p = q; q++; } else { int i, l = p, r = q; for (i = 0; i < n; i++) { int ll = l % n, rr = r % n; if (str[ll] < str[rr]) { q = r; break; } else if (str[ll] > str[rr]) { p = q; q = rr; break; } l++; r++; } if (i == n) break; } } return p; } int N, M; std::vector<point> p, q; std::vector<node> a, b; int main() { std::ios::sync_with_stdio(0); std::cin >> N >> M; p.resize(N); q.resize(M); for (int i = 0; i < N; ++i) std::cin >> p[i].x >> p[i].y; for (int i = 0; i < M; ++i) std::cin >> q[i].x >> q[i].y; std::vector<point> cvp = convex_hull(p), cvq = convex_hull(q); for (int i = 0; i < cvp.size(); ++i) { int j = (i + 1) % cvp.size(), k = (i - 1 + cvp.size()) % cvp.size(); a.emplace_back(dis2(cvp[i], cvp[j]), dot(cvp[k] - cvp[i], cvp[j] - cvp[i]), det(cvp[k] - cvp[i], cvp[j] - cvp[i])); } for (int i = 0; i < cvq.size(); ++i) { int j = (i + 1) % cvq.size(), k = (i - 1 + cvq.size()) % cvq.size(); b.emplace_back(dis2(cvq[i], cvq[j]), dot(cvq[k] - cvq[i], cvq[j] - cvq[i]), det(cvq[k] - cvq[i], cvq[j] - cvq[i])); } int n = min_rep(a), m = min_rep(b); if (a.size() != b.size()) return std::cout << "NO\n", 0; for (int i = 0; i < a.size(); ++i) { if (!(a[(n + i) % a.size()] == b[(m + i) % b.size()])) return std::cout << "NO\n", 0; } std::cout << "YES\n"; } ```
#include <bits/stdc++.h> using namespace std; template <typename T> using maxHeap = priority_queue<T, vector<T>, less<T>>; template <typename T> using minHeap = priority_queue<T, vector<T>, greater<T>>; template <typename Iter> ostream& _out(ostream& s, Iter b, Iter e) { s << "["; for (auto it = b; it != e; it++) s << (it == b ? "" : " ") << *it; s << "]"; return s; } template <typename A, typename B> ostream& operator<<(ostream& s, const pair<A, B>& p) { return s << "(" << p.first << "," << p.second << ")"; } template <typename T> ostream& operator<<(ostream& s, const vector<T>& c) { return _out(s, begin(c), end(c)); } bool debug = 0; template <typename T> void DEBUG(const T& x) { if (debug) cerr << x; } template <typename T, typename... Args> void DEBUG(const T& head, const Args&... tail) { if (debug) { cerr << head; DEBUG(tail...); } } template <typename T> struct Point { static constexpr long double EPS = 1e-8; T x, y; Point(T _ = 0, T __ = 0) : x(_), y(__) {} template <typename T2> Point(const Point<T2>& a) : x(a.x), y(a.y) {} inline long double theta() const { return atan2((long double)y, (long double)x); } inline long double dis() const { return hypot((long double)x, (long double)y); } inline long double dis(const Point& o) const { return hypot((long double)(x - o.x), (long double)(y - o.y)); } Point operator-(const Point& o) const { return Point(x - o.x, y - o.y); } Point operator-=(const Point& o) { x -= o.x, y -= o.y; return *this; } Point operator+(const Point& o) const { return Point(x + o.x, y + o.y); } Point operator+=(const Point& o) { x += o.x, y += o.y; return *this; } Point operator*(const T& k) const { return Point(x * k, y * k); } Point operator*=(const T& k) { x *= k, y *= k; return *this; } Point operator/(const T& k) const { return Point(x / k, y / k); } Point operator/=(const T& k) { x /= k, y /= k; return *this; } Point operator-() const { return Point(-x, -y); } Point rot90() const { return Point(-y, x); } bool equal(const Point& o, true_type) const { return fabs(x - o.x) < EPS and fabs(y - o.y) < EPS; } bool equal(const Point& o, false_type) const { return tie(x, y) == tie(o.x, o.y); } bool operator==(const Point& o) const { return equal(o, is_floating_point<T>()); } bool operator!=(const Point& o) const { return !(*this == o); } bool operator<(const Point& o) const { return theta() < o.theta(); } friend inline T cross(const Point& a, const Point& b) { return a.x * b.y - b.x * a.y; } friend inline T dot(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y; } friend ostream& operator<<(ostream& ss, const Point& o) { ss << "(" << o.x << ", " << o.y << ")"; return ss; } }; template <typename T> class ConvexHull_2D { private: vector<Point<T>> dots; struct myhash { uint64_t operator()(const Point<T>& a) const { uint64_t xx = 0, yy = 0; memcpy(&xx, &a.x, sizeof(a.x)); memcpy(&yy, &a.y, sizeof(a.y)); uint64_t ret = xx * 17 + yy * 31; ret = (ret ^ (ret >> 16)) * 0x9E3779B1; ret = (ret ^ (ret >> 13)) * 0xC2B2AE35; ret = ret ^ xx; return (ret ^ (ret << 3)) * yy; } }; unordered_set<Point<T>, myhash> in_hull; public: inline void init() { in_hull.clear(); dots.clear(); } void insert(const Point<T>& x) { dots.push_back(x); } void solve() { sort(begin(dots), end(dots), [](const Point<T>& a, const Point<T>& b) { return tie(a.x, a.y) < tie(b.x, b.y); }); vector<Point<T>> stk((int)((dots).size()) << 1); int top = 0; for (auto p : dots) { while (top >= 2 and cross(p - stk[top - 2], stk[top - 1] - stk[top - 2]) <= 0) top--; stk[top++] = p; } for (int i = (int)((dots).size()) - 2, t = top + 1; i >= 0; i--) { while (top >= t and cross(dots[i] - stk[top - 2], stk[top - 1] - stk[top - 2]) <= 0) top--; stk[top++] = dots[i]; } stk.resize(top - 1); swap(stk, dots); for (auto i : stk) in_hull.insert(i); } vector<Point<T>> get() { return dots; } inline bool in_it(const Point<T>& x) { return in_hull.find(x) != in_hull.end(); } }; using PTL = Point<int64_t>; ConvexHull_2D<int64_t> cv; template <typename T> bool check(const vector<T>&, const vector<T>&); vector<int64_t> get_it(const vector<PTL>& a) { vector<int64_t> ret; ret.push_back(dot(a.back() - a[0], a.back() - a[0])); ret.push_back(dot(a.back() - a[0], a[1] - a[0])); for (int i = 1; i < (int)((a).size()) - 1; i++) { ret.push_back(dot(a[i - 1] - a[i], a[i - 1] - a[i])); ret.push_back(dot(a[i - 1] - a[i], a[i + 1] - a[i])); } ret.push_back(dot(a[(int)((a).size()) - 2] - a[(int)((a).size()) - 1], a[(int)((a).size()) - 2] - a[(int)((a).size()) - 1])); ret.push_back(dot(a[(int)((a).size()) - 2] - a[(int)((a).size()) - 1], a[0] - a[(int)((a).size()) - 1])); return ret; } int main(int argc, char* argv[]) { if (argc > 1 and string(argv[1]) == "-D") debug = 1; if (!debug) { ios_base::sync_with_stdio(0); cin.tie(0); } auto kill = []() { cout << "NO" << '\n'; exit(0); }; int n, m; cin >> n >> m; cv.init(); for (int i = 0; i < n; i++) { int64_t x, y; cin >> x >> y; cv.insert(PTL(x, y)); } cv.solve(); vector<PTL> one = cv.get(); cv.init(); for (int i = 0; i < m; i++) { int64_t x, y; cin >> x >> y; cv.insert(PTL(x, y)); } cv.solve(); vector<PTL> two = cv.get(); if (debug) cerr << __PRETTY_FUNCTION__ << ":" << 217 << " - " << ("one") << "=" << (one) << '\n'; if (debug) cerr << __PRETTY_FUNCTION__ << ":" << 218 << " - " << ("two") << "=" << (two) << '\n'; if ((int)((one).size()) != (int)((two).size())) kill(); if (!check(get_it(one), get_it(two))) kill(); cout << "YES" << '\n'; return 0; } template <typename T> bool check(const vector<T>& a, const vector<T>& b) { for (int i = 0; i < (int)((a).size()); i++) { bool flag = true; for (int j = 0; j < (int)((b).size()); j++) { flag &= (a[(i + j) % (int)((a).size())] == b[j]); if (!flag) break; } if (flag) return true; } return false; }
### Prompt Please formulate a CPP solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> using maxHeap = priority_queue<T, vector<T>, less<T>>; template <typename T> using minHeap = priority_queue<T, vector<T>, greater<T>>; template <typename Iter> ostream& _out(ostream& s, Iter b, Iter e) { s << "["; for (auto it = b; it != e; it++) s << (it == b ? "" : " ") << *it; s << "]"; return s; } template <typename A, typename B> ostream& operator<<(ostream& s, const pair<A, B>& p) { return s << "(" << p.first << "," << p.second << ")"; } template <typename T> ostream& operator<<(ostream& s, const vector<T>& c) { return _out(s, begin(c), end(c)); } bool debug = 0; template <typename T> void DEBUG(const T& x) { if (debug) cerr << x; } template <typename T, typename... Args> void DEBUG(const T& head, const Args&... tail) { if (debug) { cerr << head; DEBUG(tail...); } } template <typename T> struct Point { static constexpr long double EPS = 1e-8; T x, y; Point(T _ = 0, T __ = 0) : x(_), y(__) {} template <typename T2> Point(const Point<T2>& a) : x(a.x), y(a.y) {} inline long double theta() const { return atan2((long double)y, (long double)x); } inline long double dis() const { return hypot((long double)x, (long double)y); } inline long double dis(const Point& o) const { return hypot((long double)(x - o.x), (long double)(y - o.y)); } Point operator-(const Point& o) const { return Point(x - o.x, y - o.y); } Point operator-=(const Point& o) { x -= o.x, y -= o.y; return *this; } Point operator+(const Point& o) const { return Point(x + o.x, y + o.y); } Point operator+=(const Point& o) { x += o.x, y += o.y; return *this; } Point operator*(const T& k) const { return Point(x * k, y * k); } Point operator*=(const T& k) { x *= k, y *= k; return *this; } Point operator/(const T& k) const { return Point(x / k, y / k); } Point operator/=(const T& k) { x /= k, y /= k; return *this; } Point operator-() const { return Point(-x, -y); } Point rot90() const { return Point(-y, x); } bool equal(const Point& o, true_type) const { return fabs(x - o.x) < EPS and fabs(y - o.y) < EPS; } bool equal(const Point& o, false_type) const { return tie(x, y) == tie(o.x, o.y); } bool operator==(const Point& o) const { return equal(o, is_floating_point<T>()); } bool operator!=(const Point& o) const { return !(*this == o); } bool operator<(const Point& o) const { return theta() < o.theta(); } friend inline T cross(const Point& a, const Point& b) { return a.x * b.y - b.x * a.y; } friend inline T dot(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y; } friend ostream& operator<<(ostream& ss, const Point& o) { ss << "(" << o.x << ", " << o.y << ")"; return ss; } }; template <typename T> class ConvexHull_2D { private: vector<Point<T>> dots; struct myhash { uint64_t operator()(const Point<T>& a) const { uint64_t xx = 0, yy = 0; memcpy(&xx, &a.x, sizeof(a.x)); memcpy(&yy, &a.y, sizeof(a.y)); uint64_t ret = xx * 17 + yy * 31; ret = (ret ^ (ret >> 16)) * 0x9E3779B1; ret = (ret ^ (ret >> 13)) * 0xC2B2AE35; ret = ret ^ xx; return (ret ^ (ret << 3)) * yy; } }; unordered_set<Point<T>, myhash> in_hull; public: inline void init() { in_hull.clear(); dots.clear(); } void insert(const Point<T>& x) { dots.push_back(x); } void solve() { sort(begin(dots), end(dots), [](const Point<T>& a, const Point<T>& b) { return tie(a.x, a.y) < tie(b.x, b.y); }); vector<Point<T>> stk((int)((dots).size()) << 1); int top = 0; for (auto p : dots) { while (top >= 2 and cross(p - stk[top - 2], stk[top - 1] - stk[top - 2]) <= 0) top--; stk[top++] = p; } for (int i = (int)((dots).size()) - 2, t = top + 1; i >= 0; i--) { while (top >= t and cross(dots[i] - stk[top - 2], stk[top - 1] - stk[top - 2]) <= 0) top--; stk[top++] = dots[i]; } stk.resize(top - 1); swap(stk, dots); for (auto i : stk) in_hull.insert(i); } vector<Point<T>> get() { return dots; } inline bool in_it(const Point<T>& x) { return in_hull.find(x) != in_hull.end(); } }; using PTL = Point<int64_t>; ConvexHull_2D<int64_t> cv; template <typename T> bool check(const vector<T>&, const vector<T>&); vector<int64_t> get_it(const vector<PTL>& a) { vector<int64_t> ret; ret.push_back(dot(a.back() - a[0], a.back() - a[0])); ret.push_back(dot(a.back() - a[0], a[1] - a[0])); for (int i = 1; i < (int)((a).size()) - 1; i++) { ret.push_back(dot(a[i - 1] - a[i], a[i - 1] - a[i])); ret.push_back(dot(a[i - 1] - a[i], a[i + 1] - a[i])); } ret.push_back(dot(a[(int)((a).size()) - 2] - a[(int)((a).size()) - 1], a[(int)((a).size()) - 2] - a[(int)((a).size()) - 1])); ret.push_back(dot(a[(int)((a).size()) - 2] - a[(int)((a).size()) - 1], a[0] - a[(int)((a).size()) - 1])); return ret; } int main(int argc, char* argv[]) { if (argc > 1 and string(argv[1]) == "-D") debug = 1; if (!debug) { ios_base::sync_with_stdio(0); cin.tie(0); } auto kill = []() { cout << "NO" << '\n'; exit(0); }; int n, m; cin >> n >> m; cv.init(); for (int i = 0; i < n; i++) { int64_t x, y; cin >> x >> y; cv.insert(PTL(x, y)); } cv.solve(); vector<PTL> one = cv.get(); cv.init(); for (int i = 0; i < m; i++) { int64_t x, y; cin >> x >> y; cv.insert(PTL(x, y)); } cv.solve(); vector<PTL> two = cv.get(); if (debug) cerr << __PRETTY_FUNCTION__ << ":" << 217 << " - " << ("one") << "=" << (one) << '\n'; if (debug) cerr << __PRETTY_FUNCTION__ << ":" << 218 << " - " << ("two") << "=" << (two) << '\n'; if ((int)((one).size()) != (int)((two).size())) kill(); if (!check(get_it(one), get_it(two))) kill(); cout << "YES" << '\n'; return 0; } template <typename T> bool check(const vector<T>& a, const vector<T>& b) { for (int i = 0; i < (int)((a).size()); i++) { bool flag = true; for (int j = 0; j < (int)((b).size()); j++) { flag &= (a[(i + j) % (int)((a).size())] == b[j]); if (!flag) break; } if (flag) return true; } return false; } ```
#include <bits/stdc++.h> using namespace std; using lld = int64_t; struct PT { lld x, y; PT() : x(0), y(0) {} PT(lld a, lld b) : x(a), y(b) {} PT operator-(const PT& a) const { return PT(x - a.x, y - a.y); } }; lld dot(const PT& a, const PT& b) { return a.x * b.x + a.y * b.y; } lld cross(const PT& a, const PT& b) { return a.x * b.y - a.y * b.x; } class ConvexHull_2D { private: vector<PT> dots; public: inline void init() { dots.clear(); } void insert(const PT& x) { dots.push_back(x); } void solve() { sort(begin(dots), end(dots), [](const PT& a, const PT& b) { return tie(a.x, a.y) < tie(b.x, b.y); }); vector<PT> stk((static_cast<int>((dots).size())) << 1); int top = 0; for (auto p : dots) { while (top >= 2 and cross(p - stk[top - 2], stk[top - 1] - stk[top - 2]) <= 0) top--; stk[top++] = p; } for (int i = (static_cast<int>((dots).size())) - 2, t = top + 1; i >= 0; i--) { while (top >= t and cross(dots[i] - stk[top - 2], stk[top - 1] - stk[top - 2]) <= 0) top--; stk[top++] = dots[i]; } stk.resize(top - 1); swap(stk, dots); } vector<PT> get() { return dots; } }; ConvexHull_2D cv; inline bool check(const vector<lld>&, const vector<lld>&); vector<lld> get_it(const vector<PT>& a) { vector<lld> ret; ret.push_back(dot(a.back() - a[0], a.back() - a[0])); ret.push_back(dot(a.back() - a[0], a[1] - a[0])); for (int i = 1; i < (static_cast<int>((a).size())) - 1; i++) { ret.push_back(dot(a[i - 1] - a[i], a[i - 1] - a[i])); ret.push_back(dot(a[i - 1] - a[i], a[i + 1] - a[i])); } ret.push_back(dot(a[(static_cast<int>((a).size())) - 2] - a[(static_cast<int>((a).size())) - 1], a[(static_cast<int>((a).size())) - 2] - a[(static_cast<int>((a).size())) - 1])); ret.push_back(dot(a[(static_cast<int>((a).size())) - 2] - a[(static_cast<int>((a).size())) - 1], a[0] - a[(static_cast<int>((a).size())) - 1])); return ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; cv.init(); for (int i = 0; i < n; i++) { lld x, y; cin >> x >> y; cv.insert(PT(x, y)); } cv.solve(); vector<PT> one = cv.get(); cv.init(); for (int i = 0; i < m; i++) { lld x, y; cin >> x >> y; cv.insert(PT(x, y)); } cv.solve(); vector<PT> two = cv.get(); if ((static_cast<int>((one).size())) != (static_cast<int>((two).size())) or !check(get_it(one), get_it(two))) cout << "NO" << '\n'; else cout << "YES" << '\n'; return 0; } inline bool check(const vector<lld>& a, const vector<lld>& b) { for (int i = 0; i < (static_cast<int>((a).size())); i++) { bool flag = true; for (int j = 0; j < (static_cast<int>((b).size())); j++) { flag &= (a[(i + j) % (static_cast<int>((a).size()))] == b[j]); if (!flag) break; } if (flag) return true; } return false; }
### Prompt Create a solution in cpp for the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using lld = int64_t; struct PT { lld x, y; PT() : x(0), y(0) {} PT(lld a, lld b) : x(a), y(b) {} PT operator-(const PT& a) const { return PT(x - a.x, y - a.y); } }; lld dot(const PT& a, const PT& b) { return a.x * b.x + a.y * b.y; } lld cross(const PT& a, const PT& b) { return a.x * b.y - a.y * b.x; } class ConvexHull_2D { private: vector<PT> dots; public: inline void init() { dots.clear(); } void insert(const PT& x) { dots.push_back(x); } void solve() { sort(begin(dots), end(dots), [](const PT& a, const PT& b) { return tie(a.x, a.y) < tie(b.x, b.y); }); vector<PT> stk((static_cast<int>((dots).size())) << 1); int top = 0; for (auto p : dots) { while (top >= 2 and cross(p - stk[top - 2], stk[top - 1] - stk[top - 2]) <= 0) top--; stk[top++] = p; } for (int i = (static_cast<int>((dots).size())) - 2, t = top + 1; i >= 0; i--) { while (top >= t and cross(dots[i] - stk[top - 2], stk[top - 1] - stk[top - 2]) <= 0) top--; stk[top++] = dots[i]; } stk.resize(top - 1); swap(stk, dots); } vector<PT> get() { return dots; } }; ConvexHull_2D cv; inline bool check(const vector<lld>&, const vector<lld>&); vector<lld> get_it(const vector<PT>& a) { vector<lld> ret; ret.push_back(dot(a.back() - a[0], a.back() - a[0])); ret.push_back(dot(a.back() - a[0], a[1] - a[0])); for (int i = 1; i < (static_cast<int>((a).size())) - 1; i++) { ret.push_back(dot(a[i - 1] - a[i], a[i - 1] - a[i])); ret.push_back(dot(a[i - 1] - a[i], a[i + 1] - a[i])); } ret.push_back(dot(a[(static_cast<int>((a).size())) - 2] - a[(static_cast<int>((a).size())) - 1], a[(static_cast<int>((a).size())) - 2] - a[(static_cast<int>((a).size())) - 1])); ret.push_back(dot(a[(static_cast<int>((a).size())) - 2] - a[(static_cast<int>((a).size())) - 1], a[0] - a[(static_cast<int>((a).size())) - 1])); return ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; cv.init(); for (int i = 0; i < n; i++) { lld x, y; cin >> x >> y; cv.insert(PT(x, y)); } cv.solve(); vector<PT> one = cv.get(); cv.init(); for (int i = 0; i < m; i++) { lld x, y; cin >> x >> y; cv.insert(PT(x, y)); } cv.solve(); vector<PT> two = cv.get(); if ((static_cast<int>((one).size())) != (static_cast<int>((two).size())) or !check(get_it(one), get_it(two))) cout << "NO" << '\n'; else cout << "YES" << '\n'; return 0; } inline bool check(const vector<lld>& a, const vector<lld>& b) { for (int i = 0; i < (static_cast<int>((a).size())); i++) { bool flag = true; for (int j = 0; j < (static_cast<int>((b).size())); j++) { flag &= (a[(i + j) % (static_cast<int>((a).size()))] == b[j]); if (!flag) break; } if (flag) return true; } return false; } ```
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") using namespace std; clock_t timeStart, timeFinish; void timeBegin() { timeStart = clock(); } void timeEnd() { timeFinish = clock(); } void timeDuration() { timeEnd(); double time_taken = double(timeFinish - timeStart) / double(CLOCKS_PER_SEC); cout << "Time taken by program is : " << fixed << time_taken << setprecision(5); cout << " sec " << endl; } class InputReader { public: InputReader() { input_file = stdin; cursor = 0; fread(buffer, SIZE, 1, input_file); } InputReader(const char *file_name) { input_file = fopen(file_name, "r"); cursor = 0; fread(buffer, SIZE, 1, input_file); } inline InputReader &operator>>(int &n) { while ((buffer[cursor] < '0' || buffer[cursor] > '9') && buffer[cursor] != '-') { advance(); } int sign = 1; if (buffer[cursor] == '-') { sign = -1; advance(); } n = 0; while ('0' <= buffer[cursor] && buffer[cursor] <= '9') { n = n * 10 + buffer[cursor] - '0'; advance(); } n *= sign; return *this; } private: FILE *input_file; static const int SIZE = 1 << 17; int cursor; char buffer[SIZE]; inline void advance() { ++cursor; if (cursor == SIZE) { cursor = 0; fread(buffer, SIZE, 1, input_file); } } }; const int MAXN = 100010; struct Point { int x, y; bool const operator<(const Point &other) const { return (x < other.x || (x == other.x && y < other.y)); } }; long long dist(Point a, Point b) { return 1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y); } long long det(Point a, Point b, Point c) { return 1LL * (a.x - b.x) * (c.x - b.x) + 1LL * (a.y - b.y) * (c.y - b.y); } inline long long area(Point a, Point b, Point c) { return 1LL * (b.x - a.x) * (c.y - a.y) - 1LL * (b.y - a.y) * (c.x - a.x); } vector<Point> convexHull(vector<Point> p) { int n = p.size(), m = 0; if (n < 3) return p; vector<Point> hull(n + n); sort(p.begin(), p.end()); for (int i = 0; i < n; ++i) { while (m > 1 and area(hull[m - 2], hull[m - 1], p[i]) <= 0) --m; hull[m++] = p[i]; } for (int i = n - 2, j = m + 1; i >= 0; --i) { while (m >= j and area(hull[m - 2], hull[m - 1], p[i]) <= 0) --m; hull[m++] = p[i]; } hull.resize(m - 1); return hull; } pair<long long, long long> sa[1 + 2 * MAXN], sb[1 + 2 * MAXN]; int pi[1 + 2 * MAXN]; int main() { timeBegin(); ios_base::sync_with_stdio(false); cin.tie(0); srand(time(0)); int n, m; cin >> n >> m; vector<Point> a(n), b(m); for (int i = 0; i < n; i++) { cin >> a[i].x >> a[i].y; } for (int i = 0; i < m; i++) { cin >> b[i].x >> b[i].y; } a = convexHull(a); b = convexHull(b); if (a.size() != b.size()) { cout << "NO\n"; } else { n = a.size(); long long aSum = 0, bSum = 0; for (int i = 1; i <= n; i++) { sa[i] = {dist(a[(i - 1) % n], a[i % n]), det(a[(i - 1) % n], a[i % n], a[(i + 1) % n])}; aSum += sa[i].first; sb[i] = sb[i + n] = {dist(b[(i - 1) % n], b[i % n]), det(b[(i - 1) % n], b[i % n], b[(i + 1) % n])}; bSum += sb[i].first; } if (aSum != bSum) { cout << "NO\n"; } else { for (int j = 2, i = 0; j <= n; j++) { while (i > 0 && sa[i + 1] != sa[j]) { i = pi[i]; } if (sa[i + 1] == sa[j]) { i++; } pi[j] = i; } for (int j = 1, i = 0; j <= 2 * n; j++) { while (i > 0 && sa[i + 1] != sb[j]) { i = pi[i]; } if (sa[i + 1] == sb[j]) { i++; } if (i == n) { cout << "YES\n"; return 0; } } cout << "NO\n"; } } return 0; }
### Prompt Create a solution in Cpp for the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") using namespace std; clock_t timeStart, timeFinish; void timeBegin() { timeStart = clock(); } void timeEnd() { timeFinish = clock(); } void timeDuration() { timeEnd(); double time_taken = double(timeFinish - timeStart) / double(CLOCKS_PER_SEC); cout << "Time taken by program is : " << fixed << time_taken << setprecision(5); cout << " sec " << endl; } class InputReader { public: InputReader() { input_file = stdin; cursor = 0; fread(buffer, SIZE, 1, input_file); } InputReader(const char *file_name) { input_file = fopen(file_name, "r"); cursor = 0; fread(buffer, SIZE, 1, input_file); } inline InputReader &operator>>(int &n) { while ((buffer[cursor] < '0' || buffer[cursor] > '9') && buffer[cursor] != '-') { advance(); } int sign = 1; if (buffer[cursor] == '-') { sign = -1; advance(); } n = 0; while ('0' <= buffer[cursor] && buffer[cursor] <= '9') { n = n * 10 + buffer[cursor] - '0'; advance(); } n *= sign; return *this; } private: FILE *input_file; static const int SIZE = 1 << 17; int cursor; char buffer[SIZE]; inline void advance() { ++cursor; if (cursor == SIZE) { cursor = 0; fread(buffer, SIZE, 1, input_file); } } }; const int MAXN = 100010; struct Point { int x, y; bool const operator<(const Point &other) const { return (x < other.x || (x == other.x && y < other.y)); } }; long long dist(Point a, Point b) { return 1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y); } long long det(Point a, Point b, Point c) { return 1LL * (a.x - b.x) * (c.x - b.x) + 1LL * (a.y - b.y) * (c.y - b.y); } inline long long area(Point a, Point b, Point c) { return 1LL * (b.x - a.x) * (c.y - a.y) - 1LL * (b.y - a.y) * (c.x - a.x); } vector<Point> convexHull(vector<Point> p) { int n = p.size(), m = 0; if (n < 3) return p; vector<Point> hull(n + n); sort(p.begin(), p.end()); for (int i = 0; i < n; ++i) { while (m > 1 and area(hull[m - 2], hull[m - 1], p[i]) <= 0) --m; hull[m++] = p[i]; } for (int i = n - 2, j = m + 1; i >= 0; --i) { while (m >= j and area(hull[m - 2], hull[m - 1], p[i]) <= 0) --m; hull[m++] = p[i]; } hull.resize(m - 1); return hull; } pair<long long, long long> sa[1 + 2 * MAXN], sb[1 + 2 * MAXN]; int pi[1 + 2 * MAXN]; int main() { timeBegin(); ios_base::sync_with_stdio(false); cin.tie(0); srand(time(0)); int n, m; cin >> n >> m; vector<Point> a(n), b(m); for (int i = 0; i < n; i++) { cin >> a[i].x >> a[i].y; } for (int i = 0; i < m; i++) { cin >> b[i].x >> b[i].y; } a = convexHull(a); b = convexHull(b); if (a.size() != b.size()) { cout << "NO\n"; } else { n = a.size(); long long aSum = 0, bSum = 0; for (int i = 1; i <= n; i++) { sa[i] = {dist(a[(i - 1) % n], a[i % n]), det(a[(i - 1) % n], a[i % n], a[(i + 1) % n])}; aSum += sa[i].first; sb[i] = sb[i + n] = {dist(b[(i - 1) % n], b[i % n]), det(b[(i - 1) % n], b[i % n], b[(i + 1) % n])}; bSum += sb[i].first; } if (aSum != bSum) { cout << "NO\n"; } else { for (int j = 2, i = 0; j <= n; j++) { while (i > 0 && sa[i + 1] != sa[j]) { i = pi[i]; } if (sa[i + 1] == sa[j]) { i++; } pi[j] = i; } for (int j = 1, i = 0; j <= 2 * n; j++) { while (i > 0 && sa[i + 1] != sb[j]) { i = pi[i]; } if (sa[i + 1] == sb[j]) { i++; } if (i == n) { cout << "YES\n"; return 0; } } cout << "NO\n"; } } return 0; } ```
#include <bits/stdc++.h> using namespace std; struct Point { long long x, y; Point() {} Point(long long x, long long y) : x(x), y(y) {} Point operator-(const Point &o) const { return {x - o.x, y - o.y}; } bool operator<(const Point &o) const { return tie(x, y) < tie(o.x, o.y); } bool operator==(const Point &o) const { return tie(x, y) == tie(o.x, o.y); } }; int N, M; struct str { long long a, b, ccw; bool operator==(const str &o) const { return tie(a, b, ccw) == tie(o.a, o.b, o.ccw); } bool operator!=(const str &o) const { return tie(a, b, ccw) != tie(o.a, o.b, o.ccw); } }; long long x, y; vector<Point> inp[2], H[2]; long long cross(Point o, Point a, Point b) { a = a - o, b = b - o; long long ret = a.x * b.y - a.y * b.x; return ret; } void convexhull(vector<Point> &inp, vector<Point> &H) { int n = inp.size(), k = 0; H.resize(2 * n); for (int i = 0; i < n; i++) { while (k >= 2 && cross(H[k - 2], H[k - 1], inp[i]) <= 0) k--; H[k++] = inp[i]; } for (int i = n - 1, t = k + 1; i >= 0; i--) { while (k >= t && cross(H[k - 2], H[k - 1], inp[i]) <= 0) k--; H[k++] = inp[i]; } H.erase(H.begin() + k - 1, H.end()); } int fail[100005]; vector<str> ptr, match; void f() { int i, j; int n = ptr.size(); fail[0] = -1; for (i = 1; i < n; i++) { j = fail[i - 1] + 1; while (ptr[i] != ptr[j] && j > 0) j = fail[j - 1] + 1; if (ptr[i] == ptr[j]) fail[i] = j; else fail[i] = -1; } } int comp() { int i, j; for (i = j = 0; i < M; i++, j++) { if (j == N) return i - j; while (match[i] != ptr[j] && j > 0) j = fail[j - 1] + 1; if (match[i] != ptr[j]) j = -1; } return -1; } long long dist(Point a, Point b) { b = b - a; return b.x * b.x + b.y * b.y; } int main() { scanf("%d %d", &N, &M); for (int i = 0; i < N; i++) { scanf("%lld %lld", &x, &y); inp[0].emplace_back(x, y); } for (int i = 0; i < M; i++) { scanf("%lld %lld", &x, &y); inp[1].emplace_back(x, y); } sort(inp[0].begin(), inp[0].end()); sort(inp[1].begin(), inp[1].end()); convexhull(inp[0], H[0]); convexhull(inp[1], H[1]); if (H[0].size() != H[1].size()) return !printf("NO"); H[0].push_back(H[0][0]); H[0].push_back(H[0][1]); H[1].push_back(H[1][0]); H[1].push_back(H[1][1]); for (int i = 0; i < H[0].size() - 2; i++) { ptr.push_back({dist(H[0][i], H[0][i + 1]), dist(H[0][i], H[0][i + 2]), cross(H[0][i], H[0][i + 1], H[0][i + 2])}); } for (int i = 0; i < H[1].size() - 2; i++) { match.push_back({dist(H[1][i], H[1][i + 1]), dist(H[1][i], H[1][i + 2]), cross(H[1][i], H[1][i + 1], H[1][i + 2])}); } M = match.size(); for (int i = 0; i < M; i++) match.push_back(match[i]); M *= 2; N = ptr.size(); f(); printf("%s", comp() != -1 ? "YES" : "NO"); }
### Prompt Develop a solution in cpp to the problem described below: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct Point { long long x, y; Point() {} Point(long long x, long long y) : x(x), y(y) {} Point operator-(const Point &o) const { return {x - o.x, y - o.y}; } bool operator<(const Point &o) const { return tie(x, y) < tie(o.x, o.y); } bool operator==(const Point &o) const { return tie(x, y) == tie(o.x, o.y); } }; int N, M; struct str { long long a, b, ccw; bool operator==(const str &o) const { return tie(a, b, ccw) == tie(o.a, o.b, o.ccw); } bool operator!=(const str &o) const { return tie(a, b, ccw) != tie(o.a, o.b, o.ccw); } }; long long x, y; vector<Point> inp[2], H[2]; long long cross(Point o, Point a, Point b) { a = a - o, b = b - o; long long ret = a.x * b.y - a.y * b.x; return ret; } void convexhull(vector<Point> &inp, vector<Point> &H) { int n = inp.size(), k = 0; H.resize(2 * n); for (int i = 0; i < n; i++) { while (k >= 2 && cross(H[k - 2], H[k - 1], inp[i]) <= 0) k--; H[k++] = inp[i]; } for (int i = n - 1, t = k + 1; i >= 0; i--) { while (k >= t && cross(H[k - 2], H[k - 1], inp[i]) <= 0) k--; H[k++] = inp[i]; } H.erase(H.begin() + k - 1, H.end()); } int fail[100005]; vector<str> ptr, match; void f() { int i, j; int n = ptr.size(); fail[0] = -1; for (i = 1; i < n; i++) { j = fail[i - 1] + 1; while (ptr[i] != ptr[j] && j > 0) j = fail[j - 1] + 1; if (ptr[i] == ptr[j]) fail[i] = j; else fail[i] = -1; } } int comp() { int i, j; for (i = j = 0; i < M; i++, j++) { if (j == N) return i - j; while (match[i] != ptr[j] && j > 0) j = fail[j - 1] + 1; if (match[i] != ptr[j]) j = -1; } return -1; } long long dist(Point a, Point b) { b = b - a; return b.x * b.x + b.y * b.y; } int main() { scanf("%d %d", &N, &M); for (int i = 0; i < N; i++) { scanf("%lld %lld", &x, &y); inp[0].emplace_back(x, y); } for (int i = 0; i < M; i++) { scanf("%lld %lld", &x, &y); inp[1].emplace_back(x, y); } sort(inp[0].begin(), inp[0].end()); sort(inp[1].begin(), inp[1].end()); convexhull(inp[0], H[0]); convexhull(inp[1], H[1]); if (H[0].size() != H[1].size()) return !printf("NO"); H[0].push_back(H[0][0]); H[0].push_back(H[0][1]); H[1].push_back(H[1][0]); H[1].push_back(H[1][1]); for (int i = 0; i < H[0].size() - 2; i++) { ptr.push_back({dist(H[0][i], H[0][i + 1]), dist(H[0][i], H[0][i + 2]), cross(H[0][i], H[0][i + 1], H[0][i + 2])}); } for (int i = 0; i < H[1].size() - 2; i++) { match.push_back({dist(H[1][i], H[1][i + 1]), dist(H[1][i], H[1][i + 2]), cross(H[1][i], H[1][i + 1], H[1][i + 2])}); } M = match.size(); for (int i = 0; i < M; i++) match.push_back(match[i]); M *= 2; N = ptr.size(); f(); printf("%s", comp() != -1 ? "YES" : "NO"); } ```
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; struct data { long long len, dot, cross; data() {} data(long long len, long long dot, long long cross) : len(len), dot(dot), cross(cross) {} bool operator==(const data &d) const { return (len == d.len && dot == d.dot && cross == d.cross); } bool operator!=(const data &d) const { return (len != d.len || dot != d.dot || cross != d.cross); } }; struct point { long long x, y; point() {} point(long long x, long long y) : x(x), y(y) {} void operator=(const point &p) { x = p.x, y = p.y; } point operator+(const point &p) const { point pt(x + p.x, y + p.y); return pt; } point operator-(const point &p) const { point pt(x - p.x, y - p.y); return pt; } long long crossProduct(const point &p) const { return x * p.y - y * p.x; } long long dotProduct(const point &p) const { return x * p.x + y * p.y; } long long dist() { return x * x + y * y; } }; bool comp(point &p1, point &p2) { if (p1.x != p2.x) return p1.x < p2.x; return p1.y < p2.y; } bool cw(point &a, point &b, point &c) { long long area = a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y); return area < 0; } bool ccw(point &a, point &b, point &c) { long long area = a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y); return area > 0; } vector<point> convex_hull(vector<point> &v) { if (v.size() == 1) return v; sort(v.begin(), v.end(), comp); point p1 = v[0], p2 = v.back(); vector<point> up, down; up.push_back(p1); down.push_back(p1); for (long long i = 1; i < v.size(); i++) { if (i == v.size() - 1 || cw(p1, v[i], p2)) { while (up.size() >= 2 && !cw(up[up.size() - 2], up[up.size() - 1], v[i])) up.pop_back(); up.push_back(v[i]); } if (i == v.size() - 1 || ccw(p1, v[i], p2)) { while (down.size() >= 2 && !ccw(down[down.size() - 2], down[down.size() - 1], v[i])) down.pop_back(); down.push_back(v[i]); } } for (long long i = down.size() - 2; i > 0; i--) up.push_back(down[i]); return up; } vector<long long> prefix_function(vector<data> &v) { long long n = (long long)v.size(); vector<long long> pi(n); for (long long i = 1; i < n; i++) { long long j = pi[i - 1]; while (j > 0 && v[i] != v[j]) j = pi[j - 1]; if (v[i] == v[j]) j++; pi[i] = j; } return pi; } void find_occurences(vector<data> &text, vector<data> &pattern) { vector<data> v = pattern; v.push_back(data(-1, -1, -1)); for (auto &it : text) v.push_back(it); long long sz1 = text.size(), sz2 = pattern.size(); vector<long long> lps = prefix_function(v); for (long long i = sz2 + 1; i <= sz1 + sz2; i++) { if (lps[i] == sz2) { cout << "YES"; exit(0); } } } long long n, m; vector<point> poly1, poly2, hull1, hull2; vector<data> v1, v2; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n >> m; for (long long i = 1; i <= n; i++) { long long x, y; cin >> x >> y; point p(x, y); poly1.push_back(p); } for (long long i = 1; i <= m; i++) { long long x, y; cin >> x >> y; point p(x, y); poly2.push_back(p); } hull1 = convex_hull(poly1); hull2 = convex_hull(poly2); if (hull1.size() != hull2.size()) { cout << "NO"; return 0; } long long sz = hull1.size(); for (long long i = 0; i < sz; i++) { data cur; point a = hull1[i], b = hull1[(i + 1) % sz], c = hull1[(i + 2) % sz]; cur.len = (b - a).dist(); cur.dot = (b - a).dotProduct(c - b); cur.cross = (b - a).crossProduct(c - b); v1.push_back(cur); } for (long long i = 0; i < sz; i++) v1.push_back(v1[i]); for (long long i = 0; i < sz; i++) { data cur; point a = hull2[i], b = hull2[(i + 1) % sz], c = hull2[(i + 2) % sz]; cur.len = (b - a).dist(); cur.dot = (b - a).dotProduct(c - b); cur.cross = (b - a).crossProduct(c - b); v2.push_back(cur); } find_occurences(v1, v2); cout << "NO"; return 0; }
### Prompt Construct a CPP code solution to the problem outlined: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; struct data { long long len, dot, cross; data() {} data(long long len, long long dot, long long cross) : len(len), dot(dot), cross(cross) {} bool operator==(const data &d) const { return (len == d.len && dot == d.dot && cross == d.cross); } bool operator!=(const data &d) const { return (len != d.len || dot != d.dot || cross != d.cross); } }; struct point { long long x, y; point() {} point(long long x, long long y) : x(x), y(y) {} void operator=(const point &p) { x = p.x, y = p.y; } point operator+(const point &p) const { point pt(x + p.x, y + p.y); return pt; } point operator-(const point &p) const { point pt(x - p.x, y - p.y); return pt; } long long crossProduct(const point &p) const { return x * p.y - y * p.x; } long long dotProduct(const point &p) const { return x * p.x + y * p.y; } long long dist() { return x * x + y * y; } }; bool comp(point &p1, point &p2) { if (p1.x != p2.x) return p1.x < p2.x; return p1.y < p2.y; } bool cw(point &a, point &b, point &c) { long long area = a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y); return area < 0; } bool ccw(point &a, point &b, point &c) { long long area = a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y); return area > 0; } vector<point> convex_hull(vector<point> &v) { if (v.size() == 1) return v; sort(v.begin(), v.end(), comp); point p1 = v[0], p2 = v.back(); vector<point> up, down; up.push_back(p1); down.push_back(p1); for (long long i = 1; i < v.size(); i++) { if (i == v.size() - 1 || cw(p1, v[i], p2)) { while (up.size() >= 2 && !cw(up[up.size() - 2], up[up.size() - 1], v[i])) up.pop_back(); up.push_back(v[i]); } if (i == v.size() - 1 || ccw(p1, v[i], p2)) { while (down.size() >= 2 && !ccw(down[down.size() - 2], down[down.size() - 1], v[i])) down.pop_back(); down.push_back(v[i]); } } for (long long i = down.size() - 2; i > 0; i--) up.push_back(down[i]); return up; } vector<long long> prefix_function(vector<data> &v) { long long n = (long long)v.size(); vector<long long> pi(n); for (long long i = 1; i < n; i++) { long long j = pi[i - 1]; while (j > 0 && v[i] != v[j]) j = pi[j - 1]; if (v[i] == v[j]) j++; pi[i] = j; } return pi; } void find_occurences(vector<data> &text, vector<data> &pattern) { vector<data> v = pattern; v.push_back(data(-1, -1, -1)); for (auto &it : text) v.push_back(it); long long sz1 = text.size(), sz2 = pattern.size(); vector<long long> lps = prefix_function(v); for (long long i = sz2 + 1; i <= sz1 + sz2; i++) { if (lps[i] == sz2) { cout << "YES"; exit(0); } } } long long n, m; vector<point> poly1, poly2, hull1, hull2; vector<data> v1, v2; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n >> m; for (long long i = 1; i <= n; i++) { long long x, y; cin >> x >> y; point p(x, y); poly1.push_back(p); } for (long long i = 1; i <= m; i++) { long long x, y; cin >> x >> y; point p(x, y); poly2.push_back(p); } hull1 = convex_hull(poly1); hull2 = convex_hull(poly2); if (hull1.size() != hull2.size()) { cout << "NO"; return 0; } long long sz = hull1.size(); for (long long i = 0; i < sz; i++) { data cur; point a = hull1[i], b = hull1[(i + 1) % sz], c = hull1[(i + 2) % sz]; cur.len = (b - a).dist(); cur.dot = (b - a).dotProduct(c - b); cur.cross = (b - a).crossProduct(c - b); v1.push_back(cur); } for (long long i = 0; i < sz; i++) v1.push_back(v1[i]); for (long long i = 0; i < sz; i++) { data cur; point a = hull2[i], b = hull2[(i + 1) % sz], c = hull2[(i + 2) % sz]; cur.len = (b - a).dist(); cur.dot = (b - a).dotProduct(c - b); cur.cross = (b - a).crossProduct(c - b); v2.push_back(cur); } find_occurences(v1, v2); cout << "NO"; return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long B = 5051; const long long M = 1e9 + 9; long long n, m; vector<pair<long long, long long> > p, q; pair<long long, long long> operator-(pair<long long, long long> a, pair<long long, long long> b) { return make_pair(a.first - b.first, a.second - b.second); } void no() { cout << "NO" << endl; exit(0); } void yes() { cout << "YES" << endl; exit(0); } long long cross(pair<long long, long long> A, pair<long long, long long> B) { return A.first * B.second - A.second * B.first; } long long dot(pair<long long, long long> A, pair<long long, long long> B) { return A.first * B.first + A.second * B.second; } long long ccw(pair<long long, long long> o, pair<long long, long long> a, pair<long long, long long> b) { pair<long long, long long> oa = a - o; pair<long long, long long> ob = b - o; return cross(oa, ob); } long long hsh(vector<long long> v) { long long ret = 0; for (long long first : v) ret = (ret * B + first) % M; return (ret + M) % M; } vector<pair<long long, long long> > convex_hull( vector<pair<long long, long long> > pts) { sort(pts.begin(), pts.end()); vector<pair<long long, long long> > hull; for (long long c = 2; c--;) { long long start = hull.size(); for (pair<long long, long long> P : pts) { while (hull.size() >= start + 2 and ccw(hull[hull.size() - 2], hull[hull.size() - 1], P) <= 0) hull.pop_back(); hull.push_back(P); } hull.pop_back(); reverse(pts.begin(), pts.end()); } return hull; } long long out(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { long long first = ccw(a, b, c) + 13; long long second = dot(b - a, b - a) + 17; return ((first % M * B + second % M) % M + M) % M; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (long long i = 0; i < n; i++) { long long first, second; cin >> first >> second; p.push_back({first, second}); } for (long long i = 0; i < m; i++) { long long xa, ya; cin >> xa >> ya; q.push_back({xa, ya}); } p = convex_hull(p); q = convex_hull(q); if (p.size() != q.size()) no(); if (p.size() == 1) yes(); n = p.size(); vector<long long> pp, qq; for (long long i = 0; i < n; i++) { long long j = (i + 1) % n; long long k = (i + 2) % n; pp.push_back(out(p[i], p[j], p[k])); qq.push_back(out(q[i], q[j], q[k])); } long long hp = hsh(pp); long long hq = hsh(qq); long long bb = 1; for (long long i = 0; i < n - 1; i++) bb = bb * B % M; for (long long i = 0; i < n; i++) { hq = (((hq - qq[i] * bb) % M * B % M + qq[i]) % M + M) % M; if (hq == hp) yes(); } no(); return 0; }
### Prompt Develop a solution in CPP to the problem described below: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long B = 5051; const long long M = 1e9 + 9; long long n, m; vector<pair<long long, long long> > p, q; pair<long long, long long> operator-(pair<long long, long long> a, pair<long long, long long> b) { return make_pair(a.first - b.first, a.second - b.second); } void no() { cout << "NO" << endl; exit(0); } void yes() { cout << "YES" << endl; exit(0); } long long cross(pair<long long, long long> A, pair<long long, long long> B) { return A.first * B.second - A.second * B.first; } long long dot(pair<long long, long long> A, pair<long long, long long> B) { return A.first * B.first + A.second * B.second; } long long ccw(pair<long long, long long> o, pair<long long, long long> a, pair<long long, long long> b) { pair<long long, long long> oa = a - o; pair<long long, long long> ob = b - o; return cross(oa, ob); } long long hsh(vector<long long> v) { long long ret = 0; for (long long first : v) ret = (ret * B + first) % M; return (ret + M) % M; } vector<pair<long long, long long> > convex_hull( vector<pair<long long, long long> > pts) { sort(pts.begin(), pts.end()); vector<pair<long long, long long> > hull; for (long long c = 2; c--;) { long long start = hull.size(); for (pair<long long, long long> P : pts) { while (hull.size() >= start + 2 and ccw(hull[hull.size() - 2], hull[hull.size() - 1], P) <= 0) hull.pop_back(); hull.push_back(P); } hull.pop_back(); reverse(pts.begin(), pts.end()); } return hull; } long long out(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { long long first = ccw(a, b, c) + 13; long long second = dot(b - a, b - a) + 17; return ((first % M * B + second % M) % M + M) % M; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (long long i = 0; i < n; i++) { long long first, second; cin >> first >> second; p.push_back({first, second}); } for (long long i = 0; i < m; i++) { long long xa, ya; cin >> xa >> ya; q.push_back({xa, ya}); } p = convex_hull(p); q = convex_hull(q); if (p.size() != q.size()) no(); if (p.size() == 1) yes(); n = p.size(); vector<long long> pp, qq; for (long long i = 0; i < n; i++) { long long j = (i + 1) % n; long long k = (i + 2) % n; pp.push_back(out(p[i], p[j], p[k])); qq.push_back(out(q[i], q[j], q[k])); } long long hp = hsh(pp); long long hq = hsh(qq); long long bb = 1; for (long long i = 0; i < n - 1; i++) bb = bb * B % M; for (long long i = 0; i < n; i++) { hq = (((hq - qq[i] * bb) % M * B % M + qq[i]) % M + M) % M; if (hq == hp) yes(); } no(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 10; const int MOD = 1e9 + 7; struct Point { long long x, y; void show() { cout << "(" << x << "," << y << ")" << endl; } } a[MAX], b[MAX], A[MAX], B[MAX]; Point operator+(Point A, Point B) { return (Point){A.x + B.x, A.y + B.y}; } Point operator-(Point A, Point B) { return (Point){A.x - B.x, A.y - B.y}; } Point operator*(Point A, long long B) { return (Point){A.x * B, A.y * B}; } Point operator/(Point A, long long B) { return (Point){A.x / B, A.y / B}; } long long operator*(Point A, Point B) { return A.x * B.x + A.y * B.y; } long long operator^(Point A, Point B) { return A.x * B.y - A.y * B.x; } long long cross(Point A, Point B) { return A.x * B.y - A.y * B.x; } double dis(Point A, Point B) { return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y)); } long long dis2(Point A, Point B) { return (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y); } int cmp(const Point& x, const Point& y) { if (x.x == y.x) return x.y <= y.y; return x.x < y.x; } int Tu(Point* p, int n, Point* ch) { sort(p, p + n, cmp); int m = 0; for (int i = 0; i < n; i++) { while (m > 1 && cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; ch[m++] = p[i]; } int k = m; for (int i = n - 2; i >= 0; i--) { while (m > k && cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; ch[m++] = p[i]; } if (n > 1) m--; return m; } struct lenka { long long a, b, c; int operator==(const lenka& d) const { return d.a == a && d.b == b && d.c == c; } int operator!=(const lenka& d) const { return d.a != a || d.b != b || d.c != c; } }; vector<lenka> PA, PB; int f[MAX]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) scanf("%lld%lld", &a[i].x, &a[i].y); for (int i = 0; i < m; i++) scanf("%lld%lld", &b[i].x, &b[i].y); n = Tu(a, n, A); m = Tu(b, m, B); if (n != m) puts("NO"); else { for (int i = 0; i < n; i++) PA.push_back((lenka){dis2(A[(i - 1 + n) % n], A[i]), dis2(A[i], A[(i + 1) % n]), dis2(A[(i - 1 + n) % n], A[(i + 1) % n])}); for (int i = 0; i < m; i++) PB.push_back((lenka){dis2(B[(i - 1 + m) % m], B[i]), dis2(B[i], B[(i + 1) % m]), dis2(B[(i - 1 + m) % m], B[(i + 1) % m])}); for (int i = 0; i < n; i++) PA.push_back(PA[i]); f[0] = f[1] = 0; for (int i = 1; i < m; i++) { int j = f[i]; while (j && PB[i] != PB[j]) j = f[j]; f[i + 1] = (PB[i] == PB[j] ? j + 1 : 0); } for (int i = 0, j = 0; i < PA.size(); i++) { while (j && PA[i] != PB[j]) j = f[j]; if (PA[i] == PB[j]) j++; if (j == m) { puts("YES"); return 0; } } puts("NO"); } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 10; const int MOD = 1e9 + 7; struct Point { long long x, y; void show() { cout << "(" << x << "," << y << ")" << endl; } } a[MAX], b[MAX], A[MAX], B[MAX]; Point operator+(Point A, Point B) { return (Point){A.x + B.x, A.y + B.y}; } Point operator-(Point A, Point B) { return (Point){A.x - B.x, A.y - B.y}; } Point operator*(Point A, long long B) { return (Point){A.x * B, A.y * B}; } Point operator/(Point A, long long B) { return (Point){A.x / B, A.y / B}; } long long operator*(Point A, Point B) { return A.x * B.x + A.y * B.y; } long long operator^(Point A, Point B) { return A.x * B.y - A.y * B.x; } long long cross(Point A, Point B) { return A.x * B.y - A.y * B.x; } double dis(Point A, Point B) { return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y)); } long long dis2(Point A, Point B) { return (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y); } int cmp(const Point& x, const Point& y) { if (x.x == y.x) return x.y <= y.y; return x.x < y.x; } int Tu(Point* p, int n, Point* ch) { sort(p, p + n, cmp); int m = 0; for (int i = 0; i < n; i++) { while (m > 1 && cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; ch[m++] = p[i]; } int k = m; for (int i = n - 2; i >= 0; i--) { while (m > k && cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; ch[m++] = p[i]; } if (n > 1) m--; return m; } struct lenka { long long a, b, c; int operator==(const lenka& d) const { return d.a == a && d.b == b && d.c == c; } int operator!=(const lenka& d) const { return d.a != a || d.b != b || d.c != c; } }; vector<lenka> PA, PB; int f[MAX]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) scanf("%lld%lld", &a[i].x, &a[i].y); for (int i = 0; i < m; i++) scanf("%lld%lld", &b[i].x, &b[i].y); n = Tu(a, n, A); m = Tu(b, m, B); if (n != m) puts("NO"); else { for (int i = 0; i < n; i++) PA.push_back((lenka){dis2(A[(i - 1 + n) % n], A[i]), dis2(A[i], A[(i + 1) % n]), dis2(A[(i - 1 + n) % n], A[(i + 1) % n])}); for (int i = 0; i < m; i++) PB.push_back((lenka){dis2(B[(i - 1 + m) % m], B[i]), dis2(B[i], B[(i + 1) % m]), dis2(B[(i - 1 + m) % m], B[(i + 1) % m])}); for (int i = 0; i < n; i++) PA.push_back(PA[i]); f[0] = f[1] = 0; for (int i = 1; i < m; i++) { int j = f[i]; while (j && PB[i] != PB[j]) j = f[j]; f[i + 1] = (PB[i] == PB[j] ? j + 1 : 0); } for (int i = 0, j = 0; i < PA.size(); i++) { while (j && PA[i] != PB[j]) j = f[j]; if (PA[i] == PB[j]) j++; if (j == m) { puts("YES"); return 0; } } puts("NO"); } return 0; } ```
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; const long double PI = 3.14159265358979323846; const long double eps = 1e-5; const int INF = numeric_limits<int>::max(); const long long LINF = numeric_limits<long long>::max(); const long long mod = 1000 * 1000 * 1000 + 7; bool clockwise(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { return (b.first - a.first) * (c.second - a.second) - (b.second - a.second) * (c.first - a.first) < 0; } bool counterClockwise(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { return (b.first - a.first) * (c.second - a.second) - (b.second - a.second) * (c.first - a.first) > 0; } vector<pair<int, int> > convexHull(vector<pair<int, int> > a) { if (a.size() < 3) return a; sort(a.begin(), a.end()); pair<int, int> p1 = a[0], p2 = a.back(); vector<pair<int, int> > up, down; up.push_back(p1); down.push_back(p1); for (int i = 1; i < a.size(); ++i) { if ((i == a.size() - 1) || clockwise(p1, a[i], p2)) { while ((up.size() >= 2) && (!clockwise(up[up.size() - 2], up[up.size() - 1], a[i]))) up.pop_back(); up.push_back(a[i]); } if ((i == a.size() - 1) || (counterClockwise(p1, a[i], p2))) { while ((down.size() >= 2) && (!counterClockwise(down[down.size() - 2], down[down.size() - 1], a[i]))) down.pop_back(); down.push_back(a[i]); } } a.clear(); for (int i = 0; i < up.size(); ++i) a.push_back(up[i]); for (int i = down.size() - 2; i > 0; --i) a.push_back(down[i]); return a; } vector<int> z_function(const string& s) { int n = s.size(); vector<int> z(n); for (int i = 1, l = 0, r = 0; i < n; ++i) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while ((i + z[i] < n) && (s[z[i]] == s[i + z[i]])) ++z[i]; if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; } return z; } long long norm2(const pair<int, int>& p) { return (long long)(p.first) * (p.first) + (long long)(p.second) * p.second; } long long gcd(long long x, long long y) { if (y == 0) return x; return gcd(y, x % y); } string get(vector<pair<int, int> >& a) { int n = a.size(); vector<pair<int, int> > v(n); a.push_back(a[0]); for (int i = 0; i < n; ++i) { v[i].first = a[i + 1].first - a[i].first; v[i].second = a[i + 1].second - a[i].second; } string s = ""; v.push_back(v[0]); for (int i = 0; i < n; ++i) { s += to_string((norm2(v[i]))) + '!'; long long y = (long long)v[i].first * v[i + 1].first + (long long)(v[i].second) * v[i + 1].second; s += to_string(y) + '$'; } return s; } void solve() { int n, m; cin >> n >> m; vector<pair<int, int> > a(n), b(m); for (int i = 0; i < n; ++i) { cin >> a[i].first >> a[i].second; } for (int i = 0; i < m; ++i) { cin >> b[i].first >> b[i].second; } a = convexHull(a), b = convexHull(b); if (a.size() != b.size()) { cout << "NO\n"; return; } string s1 = get(a), s2 = get(b); int sz = s1.size(); if (s2.size() != sz) { cout << "NO\n"; return; } string s = s1 + "#" + s2 + s2; vector<int> res = z_function(s); for (int i = 1; i < res.size(); ++i) { if (res[i] >= sz) { cout << "YES\n"; return; } } cout << "NO\n"; } int main() { ios_base::sync_with_stdio(0); solve(); return 0; }
### Prompt Please create a solution in cpp to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; const long double PI = 3.14159265358979323846; const long double eps = 1e-5; const int INF = numeric_limits<int>::max(); const long long LINF = numeric_limits<long long>::max(); const long long mod = 1000 * 1000 * 1000 + 7; bool clockwise(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { return (b.first - a.first) * (c.second - a.second) - (b.second - a.second) * (c.first - a.first) < 0; } bool counterClockwise(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { return (b.first - a.first) * (c.second - a.second) - (b.second - a.second) * (c.first - a.first) > 0; } vector<pair<int, int> > convexHull(vector<pair<int, int> > a) { if (a.size() < 3) return a; sort(a.begin(), a.end()); pair<int, int> p1 = a[0], p2 = a.back(); vector<pair<int, int> > up, down; up.push_back(p1); down.push_back(p1); for (int i = 1; i < a.size(); ++i) { if ((i == a.size() - 1) || clockwise(p1, a[i], p2)) { while ((up.size() >= 2) && (!clockwise(up[up.size() - 2], up[up.size() - 1], a[i]))) up.pop_back(); up.push_back(a[i]); } if ((i == a.size() - 1) || (counterClockwise(p1, a[i], p2))) { while ((down.size() >= 2) && (!counterClockwise(down[down.size() - 2], down[down.size() - 1], a[i]))) down.pop_back(); down.push_back(a[i]); } } a.clear(); for (int i = 0; i < up.size(); ++i) a.push_back(up[i]); for (int i = down.size() - 2; i > 0; --i) a.push_back(down[i]); return a; } vector<int> z_function(const string& s) { int n = s.size(); vector<int> z(n); for (int i = 1, l = 0, r = 0; i < n; ++i) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while ((i + z[i] < n) && (s[z[i]] == s[i + z[i]])) ++z[i]; if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; } return z; } long long norm2(const pair<int, int>& p) { return (long long)(p.first) * (p.first) + (long long)(p.second) * p.second; } long long gcd(long long x, long long y) { if (y == 0) return x; return gcd(y, x % y); } string get(vector<pair<int, int> >& a) { int n = a.size(); vector<pair<int, int> > v(n); a.push_back(a[0]); for (int i = 0; i < n; ++i) { v[i].first = a[i + 1].first - a[i].first; v[i].second = a[i + 1].second - a[i].second; } string s = ""; v.push_back(v[0]); for (int i = 0; i < n; ++i) { s += to_string((norm2(v[i]))) + '!'; long long y = (long long)v[i].first * v[i + 1].first + (long long)(v[i].second) * v[i + 1].second; s += to_string(y) + '$'; } return s; } void solve() { int n, m; cin >> n >> m; vector<pair<int, int> > a(n), b(m); for (int i = 0; i < n; ++i) { cin >> a[i].first >> a[i].second; } for (int i = 0; i < m; ++i) { cin >> b[i].first >> b[i].second; } a = convexHull(a), b = convexHull(b); if (a.size() != b.size()) { cout << "NO\n"; return; } string s1 = get(a), s2 = get(b); int sz = s1.size(); if (s2.size() != sz) { cout << "NO\n"; return; } string s = s1 + "#" + s2 + s2; vector<int> res = z_function(s); for (int i = 1; i < res.size(); ++i) { if (res[i] >= sz) { cout << "YES\n"; return; } } cout << "NO\n"; } int main() { ios_base::sync_with_stdio(0); solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-10; bool dcmp(double x, double y = 0) { return fabs(x - y) <= EPS; } double add(double a, double b) { if (abs(a + b) < EPS * (abs(a) + abs(b))) return 0; return a + b; } struct P { double x, y; P() {} P(double x, double y) : x(x), y(y) {} P operator+(P p) { return P(add(x, p.x), add(y, p.y)); } P operator-(P p) { return P(add(x, -p.x), add(y, -p.y)); } P operator*(double d) { return P(x * d, y * d); } double dot(P p) { return add(x * p.x, y * p.y); } double det(P p) { return add(x * p.y, -y * p.x); } }; double dis(P a, P b) { double x = a.x - b.x, y = a.y - b.y; return x * x + y * y; } vector<P> convex_hull(vector<P> &ps) { int n = ps.size(); sort(ps.begin(), ps.end(), [](const P &a, const P &b) { return a.x != b.x ? a.x < b.x : a.y < b.y; }); int k = 0; vector<P> qs(n << 1); for (int i = 0; i < n; i++) { while (k > 1 && (qs[k - 1] - qs[k - 2]).det(ps[i] - qs[k - 1]) <= 0) { k--; } qs[k++] = ps[i]; } for (int i = n - 2, t = k; i >= 0; i--) { while (k > t && (qs[k - 1] - qs[k - 2]).det(ps[i] - qs[k - 1]) <= 0) { k--; } qs[k++] = ps[i]; } qs.resize(k - 1); return qs; } int main() { int n, m; scanf("%d %d", &n, &m); vector<P> a(n), b(m); for (int i = 0; i < n; i++) { scanf("%lf %lf", &a[i].x, &a[i].y); } for (int i = 0; i < m; i++) { scanf("%lf %lf", &b[i].x, &b[i].y); } vector<P> ac = convex_hull(a), bc = convex_hull(b); int an = ac.size(), bn = bc.size(); if (an != bn) { printf("NO\n"); } else { struct V { long long x, y, z; bool operator<(const V &v) const { if (x != v.x) return x < v.x; if (y != v.y) return y < v.y; return z < v.z; } }; vector<V> av(an), bv(bn); for (int i = 0; i < an; i++) { long long x = dis(ac[i], ac[(i + 1) % an]); long long y = dis(ac[(i + 1) % an], ac[(i + 2) % an]); long long z = dis(ac[i], ac[(i + 2) % an]); av[i] = {x, y, z}; } sort(av.begin(), av.end()); for (int i = 0; i < bn; i++) { long long x = dis(bc[i], bc[(i + 1) % bn]); long long y = dis(bc[(i + 1) % bn], bc[(i + 2) % bn]); long long z = dis(bc[i], bc[(i + 2) % bn]); bv[i] = {x, y, z}; } sort(bv.begin(), bv.end()); bool ans = true; for (int i = 0; i < an; i++) { if (av[i].x != bv[i].x || av[i].y != bv[i].y || av[i].z != bv[i].z) { ans = false; break; } } printf("%s\n", ans ? "YES" : "NO"); } return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double EPS = 1e-10; bool dcmp(double x, double y = 0) { return fabs(x - y) <= EPS; } double add(double a, double b) { if (abs(a + b) < EPS * (abs(a) + abs(b))) return 0; return a + b; } struct P { double x, y; P() {} P(double x, double y) : x(x), y(y) {} P operator+(P p) { return P(add(x, p.x), add(y, p.y)); } P operator-(P p) { return P(add(x, -p.x), add(y, -p.y)); } P operator*(double d) { return P(x * d, y * d); } double dot(P p) { return add(x * p.x, y * p.y); } double det(P p) { return add(x * p.y, -y * p.x); } }; double dis(P a, P b) { double x = a.x - b.x, y = a.y - b.y; return x * x + y * y; } vector<P> convex_hull(vector<P> &ps) { int n = ps.size(); sort(ps.begin(), ps.end(), [](const P &a, const P &b) { return a.x != b.x ? a.x < b.x : a.y < b.y; }); int k = 0; vector<P> qs(n << 1); for (int i = 0; i < n; i++) { while (k > 1 && (qs[k - 1] - qs[k - 2]).det(ps[i] - qs[k - 1]) <= 0) { k--; } qs[k++] = ps[i]; } for (int i = n - 2, t = k; i >= 0; i--) { while (k > t && (qs[k - 1] - qs[k - 2]).det(ps[i] - qs[k - 1]) <= 0) { k--; } qs[k++] = ps[i]; } qs.resize(k - 1); return qs; } int main() { int n, m; scanf("%d %d", &n, &m); vector<P> a(n), b(m); for (int i = 0; i < n; i++) { scanf("%lf %lf", &a[i].x, &a[i].y); } for (int i = 0; i < m; i++) { scanf("%lf %lf", &b[i].x, &b[i].y); } vector<P> ac = convex_hull(a), bc = convex_hull(b); int an = ac.size(), bn = bc.size(); if (an != bn) { printf("NO\n"); } else { struct V { long long x, y, z; bool operator<(const V &v) const { if (x != v.x) return x < v.x; if (y != v.y) return y < v.y; return z < v.z; } }; vector<V> av(an), bv(bn); for (int i = 0; i < an; i++) { long long x = dis(ac[i], ac[(i + 1) % an]); long long y = dis(ac[(i + 1) % an], ac[(i + 2) % an]); long long z = dis(ac[i], ac[(i + 2) % an]); av[i] = {x, y, z}; } sort(av.begin(), av.end()); for (int i = 0; i < bn; i++) { long long x = dis(bc[i], bc[(i + 1) % bn]); long long y = dis(bc[(i + 1) % bn], bc[(i + 2) % bn]); long long z = dis(bc[i], bc[(i + 2) % bn]); bv[i] = {x, y, z}; } sort(bv.begin(), bv.end()); bool ans = true; for (int i = 0; i < an; i++) { if (av[i].x != bv[i].x || av[i].y != bv[i].y || av[i].z != bv[i].z) { ans = false; break; } } printf("%s\n", ans ? "YES" : "NO"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; void doRoutine() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } const long long INF = 2000000000000000000; pair<long long, long long> foot; pair<long long, long long> vec(pair<long long, long long> a, pair<long long, long long> b) { return {b.first - a.first, b.second - a.second}; } long long cp(pair<long long, long long> a, pair<long long, long long> b) { return a.first * b.second - a.second * b.first; } long long sc(pair<long long, long long> a, pair<long long, long long> b) { return a.first * b.first + a.second * b.second; } long long len2(pair<long long, long long> v) { return v.first * v.first + v.second * v.second; } bool cmp_foot(pair<long long, long long> a, pair<long long, long long> b) { pair<long long, long long> v1 = vec(foot, a), v2 = vec(foot, b); long long r = cp(v1, v2); if (r != 0) { return r > 0; } else { return len2(v1) < len2(v2); } } bool bad(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { pair<long long, long long> v1 = vec(a, b), v2 = vec(a, c); return cp(v2, v1) >= 0; } vector<pair<long long, long long> > hull( vector<pair<long long, long long> > &a) { long long n = a.size(); sort(a.begin(), a.end()); foot = a[0]; sort(a.begin() + 1, a.end(), cmp_foot); vector<pair<long long, long long> > res; for (long long i = 0; i < n; ++i) { while (res.size() > 1 && bad(res[res.size() - 2], res.back(), a[i])) { res.pop_back(); } res.push_back(a[i]); } return res; } vector<long long> get_seq(vector<pair<long long, long long> > &a) { vector<long long> res; long long n = a.size(); for (long long i = 0; i < n; ++i) { auto v1 = vec(a[i], a[(i + 1) % n]); auto v2 = vec(a[i], a[(i - 1 + n) % n]); res.push_back(sc(v1, v2)); res.push_back(len2(vec(a[i], a[(i + 1) % n]))); } return res; } vector<long long> pfunc(vector<long long> &a) { long long n = a.size(); vector<long long> res(n + 1); res[0] = -1; res[1] = 0; for (long long i = 1; i < n; ++i) { long long x = res[i]; while (x >= 0 && a[x] != a[i]) { x = res[x]; } res[i + 1] = x + 1; } return res; } bool find(vector<long long> &a, vector<long long> &b) { vector<long long> s = b; s.push_back(INF); for (auto el : a) { s.push_back(el); } auto p = pfunc(s); for (auto el : p) { if (el == b.size()) { return true; } } return false; } signed main() { doRoutine(); long long n, m; cin >> n >> m; vector<pair<long long, long long> > a(n), b(m); for (long long i = 0; i < n; ++i) { cin >> a[i].first >> a[i].second; } for (long long i = 0; i < m; ++i) { cin >> b[i].first >> b[i].second; } auto ha = hull(a); auto hb = hull(b); if (ha.size() != hb.size()) { cout << "NO" << endl; return 0; } auto s = get_seq(ha), t = get_seq(hb); long long tmp = t.size(); for (long long i = 0; i < tmp; ++i) { t.push_back(t[i]); } if (find(t, s)) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
### Prompt Please provide a CPP coded solution to the problem described below: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; void doRoutine() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } const long long INF = 2000000000000000000; pair<long long, long long> foot; pair<long long, long long> vec(pair<long long, long long> a, pair<long long, long long> b) { return {b.first - a.first, b.second - a.second}; } long long cp(pair<long long, long long> a, pair<long long, long long> b) { return a.first * b.second - a.second * b.first; } long long sc(pair<long long, long long> a, pair<long long, long long> b) { return a.first * b.first + a.second * b.second; } long long len2(pair<long long, long long> v) { return v.first * v.first + v.second * v.second; } bool cmp_foot(pair<long long, long long> a, pair<long long, long long> b) { pair<long long, long long> v1 = vec(foot, a), v2 = vec(foot, b); long long r = cp(v1, v2); if (r != 0) { return r > 0; } else { return len2(v1) < len2(v2); } } bool bad(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { pair<long long, long long> v1 = vec(a, b), v2 = vec(a, c); return cp(v2, v1) >= 0; } vector<pair<long long, long long> > hull( vector<pair<long long, long long> > &a) { long long n = a.size(); sort(a.begin(), a.end()); foot = a[0]; sort(a.begin() + 1, a.end(), cmp_foot); vector<pair<long long, long long> > res; for (long long i = 0; i < n; ++i) { while (res.size() > 1 && bad(res[res.size() - 2], res.back(), a[i])) { res.pop_back(); } res.push_back(a[i]); } return res; } vector<long long> get_seq(vector<pair<long long, long long> > &a) { vector<long long> res; long long n = a.size(); for (long long i = 0; i < n; ++i) { auto v1 = vec(a[i], a[(i + 1) % n]); auto v2 = vec(a[i], a[(i - 1 + n) % n]); res.push_back(sc(v1, v2)); res.push_back(len2(vec(a[i], a[(i + 1) % n]))); } return res; } vector<long long> pfunc(vector<long long> &a) { long long n = a.size(); vector<long long> res(n + 1); res[0] = -1; res[1] = 0; for (long long i = 1; i < n; ++i) { long long x = res[i]; while (x >= 0 && a[x] != a[i]) { x = res[x]; } res[i + 1] = x + 1; } return res; } bool find(vector<long long> &a, vector<long long> &b) { vector<long long> s = b; s.push_back(INF); for (auto el : a) { s.push_back(el); } auto p = pfunc(s); for (auto el : p) { if (el == b.size()) { return true; } } return false; } signed main() { doRoutine(); long long n, m; cin >> n >> m; vector<pair<long long, long long> > a(n), b(m); for (long long i = 0; i < n; ++i) { cin >> a[i].first >> a[i].second; } for (long long i = 0; i < m; ++i) { cin >> b[i].first >> b[i].second; } auto ha = hull(a); auto hb = hull(b); if (ha.size() != hb.size()) { cout << "NO" << endl; return 0; } auto s = get_seq(ha), t = get_seq(hb); long long tmp = t.size(); for (long long i = 0; i < tmp; ++i) { t.push_back(t[i]); } if (find(t, s)) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma comment(linker, "/STACK:16777216") using namespace std; using matrix = vector<vector<long long>>; void FAST_IO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } struct point { long long x, y; point() : x(0), y(0){}; point(long long _x, long long _y) : x(_x), y(_y){}; }; struct vect { long long x, y; vect() : x(0), y(0){}; vect(long long _x, long long _y) : x(_x), y(_y){}; vect(point a, point b) : x(b.x - a.x), y(b.y - a.y){}; long long operator^(const vect &other) { return x * other.y - y * other.x; } long long operator*(const vect &other) { return x * other.x + y * other.y; } }; const double pi = 3.14159265; const double eps = 1e-6; const int MAXN = 1e5 + 100; point x[MAXN], f[MAXN], s[MAXN]; pair<long long, double> A[MAXN], B[MAXN], C[MAXN * 2]; int zp[MAXN * 2], zs[MAXN * 2]; long long dist(point a, point b) { return (b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y); } bool cmp(point a, point b) { long long res = (vect(x[0], a) ^ vect(a, b)); return res > 0 || res == 0 && dist(x[0], a) < dist(x[0], b); } signed main() { FAST_IO(); int n, m; cin >> n >> m; int sza = -1, szb = -1; long long Sa = 0, Sb = 0; long long Pa = 0, Pb = 0; int v = 0, k = 2; for (int i = 0; i < n; ++i) { cin >> x[i].x >> x[i].y; } for (int i = 1; i < n; i++) { if (x[i].y < x[v].y || x[i].y == x[v].y && x[i].x > x[v].x) { v = i; } } swap(x[0], x[v]); sort(x + 1, x + n, cmp); for (int i = 2; i < n; i++) { while ((vect(x[k - 2], x[k - 1]) ^ vect(x[k - 1], x[i])) <= 0 && k > 1) { k--; } x[k] = x[i]; k++; } sza = k; for (int i = 0; i < k; ++i) { f[i].x = x[i].x; f[i].y = x[i].y; } for (int i = 2; i < k; i++) { Sa += (vect(x[0], x[i - 1]) ^ vect(x[0], x[i])); } for (int i = 0; i < k; i++) { A[i].first = dist(x[i], x[(i + 1) % k]); A[i].second = atan2( vect(x[i], x[(i + 1) % k]) ^ vect(x[(i + 1) % k], x[(i + 2) % k]), vect(x[i], x[(i + 1) % k]) * vect(x[(i + 1) % k], x[(i + 2) % k])); if (A[i].second < 0) { A[i].second += 2 * pi; } Pa += A[i].first; } v = 0, k = 2; for (int i = 0; i < m; ++i) { cin >> x[i].x >> x[i].y; } for (int i = 1; i < m; i++) { if (x[i].y < x[v].y || x[i].y == x[v].y && x[i].x > x[v].x) { v = i; } } swap(x[0], x[v]); sort(x + 1, x + m, cmp); for (int i = 2; i < m; i++) { while ((vect(x[k - 2], x[k - 1]) ^ vect(x[k - 1], x[i])) <= 0 && k > 1) { k--; } x[k] = x[i]; k++; } szb = k; for (int i = 0; i < k; ++i) { s[i].x = x[i].x; s[i].y = x[i].y; } for (int i = 2; i < k; i++) { Sb += (vect(x[0], x[i - 1]) ^ vect(x[0], x[i])); } for (int i = 0; i < k; i++) { B[i].first = dist(x[i], x[(i + 1) % k]); B[i].second = atan2( vect(x[i], x[(i + 1) % k]) ^ vect(x[(i + 1) % k], x[(i + 2) % k]), vect(x[i], x[(i + 1) % k]) * vect(x[(i + 1) % k], x[(i + 2) % k])); if (B[i].second < 0) { B[i].second += 2 * pi; } Pb += B[i].first; } if (sza != szb || Sa != Sb || Pa != Pb) { cout << "NO"; return 0; } for (int i = 0; i < sza; ++i) { C[i] = A[i]; } C[sza] = {-1, -1}; for (int i = 0; i < sza; ++i) { C[i + sza + 1] = B[i]; } int l = 0, r = 0; zp[0] = 0; for (int i = 1; i < sza + sza + 1; ++i) { zp[i] = max(0, min(zp[i - l], r - i)); while (C[zp[i]].first == C[i + zp[i]].first && abs(C[zp[i]].second - C[i + zp[i]].second) < eps) { zp[i]++; } if (i + zp[i] > r) { l = i; r = i + zp[i]; } } for (int i = 0; i < sza; ++i) { C[i] = B[i]; } C[sza] = {-1, -1}; for (int i = 0; i < sza; ++i) { C[i + sza + 1] = A[i]; } l = 0, r = 0; zs[0] = 0; for (int i = 1; i < sza + sza + 1; ++i) { zs[i] = max(0, min(zs[i - l], r - i)); while (C[zs[i]].first == C[i + zs[i]].first && abs(C[zs[i]].second - C[i + zs[i]].second) < eps) { zs[i]++; } if (i + zs[i] > r) { l = i; r = i + zs[i]; } } for (int i = sza + 1; i < sza + sza + 1; ++i) { if (i + zp[i] == sza + sza + 1) { int len = i - sza - 1; if (len == 0 || zs[sza + 1 + sza - len] >= len) { cout << "YES"; return 0; } } } cout << "NO"; }
### Prompt Your challenge is to write a cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma comment(linker, "/STACK:16777216") using namespace std; using matrix = vector<vector<long long>>; void FAST_IO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } struct point { long long x, y; point() : x(0), y(0){}; point(long long _x, long long _y) : x(_x), y(_y){}; }; struct vect { long long x, y; vect() : x(0), y(0){}; vect(long long _x, long long _y) : x(_x), y(_y){}; vect(point a, point b) : x(b.x - a.x), y(b.y - a.y){}; long long operator^(const vect &other) { return x * other.y - y * other.x; } long long operator*(const vect &other) { return x * other.x + y * other.y; } }; const double pi = 3.14159265; const double eps = 1e-6; const int MAXN = 1e5 + 100; point x[MAXN], f[MAXN], s[MAXN]; pair<long long, double> A[MAXN], B[MAXN], C[MAXN * 2]; int zp[MAXN * 2], zs[MAXN * 2]; long long dist(point a, point b) { return (b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y); } bool cmp(point a, point b) { long long res = (vect(x[0], a) ^ vect(a, b)); return res > 0 || res == 0 && dist(x[0], a) < dist(x[0], b); } signed main() { FAST_IO(); int n, m; cin >> n >> m; int sza = -1, szb = -1; long long Sa = 0, Sb = 0; long long Pa = 0, Pb = 0; int v = 0, k = 2; for (int i = 0; i < n; ++i) { cin >> x[i].x >> x[i].y; } for (int i = 1; i < n; i++) { if (x[i].y < x[v].y || x[i].y == x[v].y && x[i].x > x[v].x) { v = i; } } swap(x[0], x[v]); sort(x + 1, x + n, cmp); for (int i = 2; i < n; i++) { while ((vect(x[k - 2], x[k - 1]) ^ vect(x[k - 1], x[i])) <= 0 && k > 1) { k--; } x[k] = x[i]; k++; } sza = k; for (int i = 0; i < k; ++i) { f[i].x = x[i].x; f[i].y = x[i].y; } for (int i = 2; i < k; i++) { Sa += (vect(x[0], x[i - 1]) ^ vect(x[0], x[i])); } for (int i = 0; i < k; i++) { A[i].first = dist(x[i], x[(i + 1) % k]); A[i].second = atan2( vect(x[i], x[(i + 1) % k]) ^ vect(x[(i + 1) % k], x[(i + 2) % k]), vect(x[i], x[(i + 1) % k]) * vect(x[(i + 1) % k], x[(i + 2) % k])); if (A[i].second < 0) { A[i].second += 2 * pi; } Pa += A[i].first; } v = 0, k = 2; for (int i = 0; i < m; ++i) { cin >> x[i].x >> x[i].y; } for (int i = 1; i < m; i++) { if (x[i].y < x[v].y || x[i].y == x[v].y && x[i].x > x[v].x) { v = i; } } swap(x[0], x[v]); sort(x + 1, x + m, cmp); for (int i = 2; i < m; i++) { while ((vect(x[k - 2], x[k - 1]) ^ vect(x[k - 1], x[i])) <= 0 && k > 1) { k--; } x[k] = x[i]; k++; } szb = k; for (int i = 0; i < k; ++i) { s[i].x = x[i].x; s[i].y = x[i].y; } for (int i = 2; i < k; i++) { Sb += (vect(x[0], x[i - 1]) ^ vect(x[0], x[i])); } for (int i = 0; i < k; i++) { B[i].first = dist(x[i], x[(i + 1) % k]); B[i].second = atan2( vect(x[i], x[(i + 1) % k]) ^ vect(x[(i + 1) % k], x[(i + 2) % k]), vect(x[i], x[(i + 1) % k]) * vect(x[(i + 1) % k], x[(i + 2) % k])); if (B[i].second < 0) { B[i].second += 2 * pi; } Pb += B[i].first; } if (sza != szb || Sa != Sb || Pa != Pb) { cout << "NO"; return 0; } for (int i = 0; i < sza; ++i) { C[i] = A[i]; } C[sza] = {-1, -1}; for (int i = 0; i < sza; ++i) { C[i + sza + 1] = B[i]; } int l = 0, r = 0; zp[0] = 0; for (int i = 1; i < sza + sza + 1; ++i) { zp[i] = max(0, min(zp[i - l], r - i)); while (C[zp[i]].first == C[i + zp[i]].first && abs(C[zp[i]].second - C[i + zp[i]].second) < eps) { zp[i]++; } if (i + zp[i] > r) { l = i; r = i + zp[i]; } } for (int i = 0; i < sza; ++i) { C[i] = B[i]; } C[sza] = {-1, -1}; for (int i = 0; i < sza; ++i) { C[i + sza + 1] = A[i]; } l = 0, r = 0; zs[0] = 0; for (int i = 1; i < sza + sza + 1; ++i) { zs[i] = max(0, min(zs[i - l], r - i)); while (C[zs[i]].first == C[i + zs[i]].first && abs(C[zs[i]].second - C[i + zs[i]].second) < eps) { zs[i]++; } if (i + zs[i] > r) { l = i; r = i + zs[i]; } } for (int i = sza + 1; i < sza + sza + 1; ++i) { if (i + zp[i] == sza + sza + 1) { int len = i - sza - 1; if (len == 0 || zs[sza + 1 + sza - len] >= len) { cout << "YES"; return 0; } } } cout << "NO"; } ```
#include <bits/stdc++.h> using namespace std; struct R2vec { int x, y; R2vec() {} R2vec(int x, int y) : x(x), y(y) {} inline R2vec operator+(const R2vec &w) { return R2vec(x + w.x, y + w.y); } inline R2vec operator-(const R2vec &w) { return R2vec(x - w.x, y - w.y); } inline R2vec operator-() { return R2vec(-x, -y); } inline long long int operator*(const R2vec &w) { return (long long int)x * w.x + (long long int)y * w.y; } inline long long int operator%(const R2vec &w) { return (long long int)x * w.y - (long long int)y * w.x; } inline long long int operator~() { return (long long int)x * x + (long long int)y * y; } inline bool operator<(const R2vec &w) const { return x != w.x ? x < w.x : y < w.y; } inline bool operator==(const R2vec &w) { return x == w.x && y == w.y; } inline bool operator!=(const R2vec &w) { return x != w.x || y != w.y; } }; inline ostream &operator<<(ostream &out, const R2vec &w) { return out << '[' << w.x << ',' << w.y << ']'; } struct ConvexHull { vector<R2vec> hull; ConvexHull() {} ConvexHull(vector<R2vec> cloud) { sort((cloud).begin(), (cloud).end()); vector<R2vec> up, down; for (auto &w : cloud) { while (((int)(up).size()) > 1 && (up.back() - up[((int)(up).size()) - 2]) % (w - up.back()) >= 0) up.pop_back(); up.push_back(w); while (((int)(down).size()) > 1 && (down.back() - down[((int)(down).size()) - 2]) % (w - down.back()) <= 0) down.pop_back(); down.push_back(w); } hull = up; for (int i = ((int)(down).size()) - 2; i; --i) hull.push_back(down[i]); } vector<pair<long long int, long long int>> hash() { vector<pair<long long int, long long int>> res(((int)(hull).size())); for (int i = 0; i < ((int)(hull).size()); ++i) res[i] = { ~(hull[i] - hull[(i ? i - 1 : ((int)(hull).size()) - 1)]), (hull[(i ? i - 1 : ((int)(hull).size()) - 1)] - hull[i]) * (hull[(i + 1 < ((int)(hull).size()) ? i + 1 : 0)] - hull[i])}; return res; } }; template <class C> struct KMP { C p; vector<int> fail; KMP(C p) : p(p), fail(((int)(p).size())) { for (int i = 0, j = -2; i < ((int)(p).size()); fail[i] = ++j, ++i) while (j > -2 && p[j + 1] != p[i]) if (j == -1) j = -2; else j = fail[j]; } vector<int> all_match(C &text, bool overlap = true) { vector<int> res; for (int i = 0, j = -1; i < ((int)(text).size()); ++i) { while (j > -2 && p[j + 1] != text[i]) if (j == -1) j = -2; else j = fail[j]; if (++j == ((int)(p).size()) - 1) res.push_back(i - j), j = overlap ? fail[j] : -1; } return res; } bool match(C &text) { for (int i = 0, j = -1; i < ((int)(text).size()); ++i) { while (j > -2 && p[j + 1] != text[i]) if (j == -1) j = -2; else j = fail[j]; if (++j == ((int)(p).size()) - 1) return true; } return false; } }; int main() { int n, m; scanf("%d%d", &n, &m); vector<R2vec> e1(n), e2(m); for (auto &e : e1) scanf("%d%d", &e.x, &e.y); for (auto &e : e2) scanf("%d%d", &e.x, &e.y); ConvexHull CH1(e1), CH2(e2); vector<pair<long long int, long long int>> a1; KMP<vector<pair<long long int, long long int>>> kmp(a1 = CH1.hash()); vector<pair<long long int, long long int>> hash = CH2.hash(); for (int i = 0, j = ((int)(hash).size()); i < j; ++i) hash.push_back(hash[i]); if (kmp.match(hash)) cout << "YES\n"; else cout << "NO\n"; return 0; }
### Prompt Create a solution in cpp for the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct R2vec { int x, y; R2vec() {} R2vec(int x, int y) : x(x), y(y) {} inline R2vec operator+(const R2vec &w) { return R2vec(x + w.x, y + w.y); } inline R2vec operator-(const R2vec &w) { return R2vec(x - w.x, y - w.y); } inline R2vec operator-() { return R2vec(-x, -y); } inline long long int operator*(const R2vec &w) { return (long long int)x * w.x + (long long int)y * w.y; } inline long long int operator%(const R2vec &w) { return (long long int)x * w.y - (long long int)y * w.x; } inline long long int operator~() { return (long long int)x * x + (long long int)y * y; } inline bool operator<(const R2vec &w) const { return x != w.x ? x < w.x : y < w.y; } inline bool operator==(const R2vec &w) { return x == w.x && y == w.y; } inline bool operator!=(const R2vec &w) { return x != w.x || y != w.y; } }; inline ostream &operator<<(ostream &out, const R2vec &w) { return out << '[' << w.x << ',' << w.y << ']'; } struct ConvexHull { vector<R2vec> hull; ConvexHull() {} ConvexHull(vector<R2vec> cloud) { sort((cloud).begin(), (cloud).end()); vector<R2vec> up, down; for (auto &w : cloud) { while (((int)(up).size()) > 1 && (up.back() - up[((int)(up).size()) - 2]) % (w - up.back()) >= 0) up.pop_back(); up.push_back(w); while (((int)(down).size()) > 1 && (down.back() - down[((int)(down).size()) - 2]) % (w - down.back()) <= 0) down.pop_back(); down.push_back(w); } hull = up; for (int i = ((int)(down).size()) - 2; i; --i) hull.push_back(down[i]); } vector<pair<long long int, long long int>> hash() { vector<pair<long long int, long long int>> res(((int)(hull).size())); for (int i = 0; i < ((int)(hull).size()); ++i) res[i] = { ~(hull[i] - hull[(i ? i - 1 : ((int)(hull).size()) - 1)]), (hull[(i ? i - 1 : ((int)(hull).size()) - 1)] - hull[i]) * (hull[(i + 1 < ((int)(hull).size()) ? i + 1 : 0)] - hull[i])}; return res; } }; template <class C> struct KMP { C p; vector<int> fail; KMP(C p) : p(p), fail(((int)(p).size())) { for (int i = 0, j = -2; i < ((int)(p).size()); fail[i] = ++j, ++i) while (j > -2 && p[j + 1] != p[i]) if (j == -1) j = -2; else j = fail[j]; } vector<int> all_match(C &text, bool overlap = true) { vector<int> res; for (int i = 0, j = -1; i < ((int)(text).size()); ++i) { while (j > -2 && p[j + 1] != text[i]) if (j == -1) j = -2; else j = fail[j]; if (++j == ((int)(p).size()) - 1) res.push_back(i - j), j = overlap ? fail[j] : -1; } return res; } bool match(C &text) { for (int i = 0, j = -1; i < ((int)(text).size()); ++i) { while (j > -2 && p[j + 1] != text[i]) if (j == -1) j = -2; else j = fail[j]; if (++j == ((int)(p).size()) - 1) return true; } return false; } }; int main() { int n, m; scanf("%d%d", &n, &m); vector<R2vec> e1(n), e2(m); for (auto &e : e1) scanf("%d%d", &e.x, &e.y); for (auto &e : e2) scanf("%d%d", &e.x, &e.y); ConvexHull CH1(e1), CH2(e2); vector<pair<long long int, long long int>> a1; KMP<vector<pair<long long int, long long int>>> kmp(a1 = CH1.hash()); vector<pair<long long int, long long int>> hash = CH2.hash(); for (int i = 0, j = ((int)(hash).size()); i < j; ++i) hash.push_back(hash[i]); if (kmp.match(hash)) cout << "YES\n"; else cout << "NO\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; const long double EPS = 1e-7; long double cp(const complex<long double>& a, const complex<long double>& b) { return imag(conj(a) * b); } long double dp(const complex<long double>& a, const complex<long double>& b) { return real(conj(a) * b); } inline bool eq(const complex<long double>& a, const complex<long double>& b) { return abs(a - b) < EPS; } inline long double sgn(const long double& x) { return abs(x) < EPS ? 0 : x / abs(x); } inline bool cmp_lex(const complex<long double>& a, const complex<long double>& b) { return a.real() < b.real() - EPS || (a.real() < b.real() + EPS && a.imag() < b.imag() - EPS); } inline bool cmp_lex_i(const complex<long double>& a, const complex<long double>& b) { return a.imag() < b.imag() - EPS || (a.imag() < b.imag() + EPS && a.real() < b.real() - EPS); } vector<complex<long double>> chull(vector<complex<long double>> p) { sort(p.begin(), p.end(), cmp_lex_i); int top = 0, bot = 1; vector<complex<long double>> ch(2 * p.size()); for (int i = 0, d = 1; i < p.size() && i >= 0; i += d) { while (top > bot && cp(ch[top - 1] - ch[top - 2], p[i] - ch[top - 2]) <= 0) top--; ch[top++] = p[i]; if (i == p.size() - 1) d = -1, bot = top; } ch.resize(max(1, top - 1)); return ch; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<complex<long double>> a(n), b(m); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; a[i] = complex<long double>(x, y); } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; b[i] = complex<long double>(x, y); } a = chull(a); b = chull(b); if (a.size() != b.size()) { puts("NO"); return 0; } vector<pair<long double, long double>> alol, blol; for (int i = 1; i <= a.size(); i++) { alol.emplace_back(abs(a[i % a.size()] - a[i - 1]), arg((a[(i + 1) % a.size()] - a[i % a.size()]) / (a[i % a.size()] - a[i - 1]))); } for (int i = 1; i <= b.size(); i++) { blol.emplace_back(abs(b[i % b.size()] - b[i - 1]), arg((b[(i + 1) % b.size()] - b[i % b.size()]) / (b[i % b.size()] - b[i - 1]))); } sort(alol.begin(), alol.end()); sort(blol.begin(), blol.end()); for (int i = 0; i < a.size(); i++) { if (abs(alol[i].first - blol[i].first) > EPS) { puts("NO"); return 0; } if (abs(alol[i].second - blol[i].second) > EPS) { puts("NO"); return 0; } } puts("YES"); return 0; }
### Prompt Create a solution in CPP for the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; const long double EPS = 1e-7; long double cp(const complex<long double>& a, const complex<long double>& b) { return imag(conj(a) * b); } long double dp(const complex<long double>& a, const complex<long double>& b) { return real(conj(a) * b); } inline bool eq(const complex<long double>& a, const complex<long double>& b) { return abs(a - b) < EPS; } inline long double sgn(const long double& x) { return abs(x) < EPS ? 0 : x / abs(x); } inline bool cmp_lex(const complex<long double>& a, const complex<long double>& b) { return a.real() < b.real() - EPS || (a.real() < b.real() + EPS && a.imag() < b.imag() - EPS); } inline bool cmp_lex_i(const complex<long double>& a, const complex<long double>& b) { return a.imag() < b.imag() - EPS || (a.imag() < b.imag() + EPS && a.real() < b.real() - EPS); } vector<complex<long double>> chull(vector<complex<long double>> p) { sort(p.begin(), p.end(), cmp_lex_i); int top = 0, bot = 1; vector<complex<long double>> ch(2 * p.size()); for (int i = 0, d = 1; i < p.size() && i >= 0; i += d) { while (top > bot && cp(ch[top - 1] - ch[top - 2], p[i] - ch[top - 2]) <= 0) top--; ch[top++] = p[i]; if (i == p.size() - 1) d = -1, bot = top; } ch.resize(max(1, top - 1)); return ch; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<complex<long double>> a(n), b(m); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; a[i] = complex<long double>(x, y); } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; b[i] = complex<long double>(x, y); } a = chull(a); b = chull(b); if (a.size() != b.size()) { puts("NO"); return 0; } vector<pair<long double, long double>> alol, blol; for (int i = 1; i <= a.size(); i++) { alol.emplace_back(abs(a[i % a.size()] - a[i - 1]), arg((a[(i + 1) % a.size()] - a[i % a.size()]) / (a[i % a.size()] - a[i - 1]))); } for (int i = 1; i <= b.size(); i++) { blol.emplace_back(abs(b[i % b.size()] - b[i - 1]), arg((b[(i + 1) % b.size()] - b[i % b.size()]) / (b[i % b.size()] - b[i - 1]))); } sort(alol.begin(), alol.end()); sort(blol.begin(), blol.end()); for (int i = 0; i < a.size(); i++) { if (abs(alol[i].first - blol[i].first) > EPS) { puts("NO"); return 0; } if (abs(alol[i].second - blol[i].second) > EPS) { puts("NO"); return 0; } } puts("YES"); return 0; } ```
#include <bits/stdc++.h> using namespace std; struct Point { long long x, y; Point() = default; Point(long long x, long long y) : x(x), y(y) {} Point operator-(Point other) const { return Point(x - other.x, y - other.y); } bool operator<(Point other) const { return make_pair(x, y) < make_pair(other.x, other.y); } long long operator~() const { return x * x + y * y; } long long operator*(Point other) const { return x * other.x + y * other.y; } long long operator%(Point other) const { return x * other.y - y * other.x; } }; bool ccw(const Point &a, const Point &b, const Point &c) { return (b - a) % (c - b) > 0; } vector<Point> hull(vector<Point> &P) { int n = P.size(); sort(P.begin(), P.end()); vector<Point> H; for (int i = 0; i < n; i++) { while (H.size() > 1 && !ccw(H.end()[-2], H.back(), P[i])) { H.pop_back(); } H.push_back(P[i]); } auto k = H.size(); for (int i = n - 2; i >= 0; i--) { while (H.size() > k && !ccw(H.end()[-2], H.back(), P[i])) { H.pop_back(); } H.push_back(P[i]); } H.pop_back(); return H; } string hullstr(vector<Point> H) { ostringstream oss; H.push_back(H[0]); H.push_back(H[1]); for (int i = 1; i < (int)H.size() - 1; i++) { oss << ',' << ~(H[i] - H[i - 1]) << ',' << ((H[i] - H[i - 1]) * (H[i + 1] - H[i])); } H.pop_back(); H.pop_back(); return oss.str(); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<Point> P1(n); for (int i = 0; i < n; i++) { cin >> P1[i].x >> P1[i].y; } vector<Point> P2(m); for (int i = 0; i < m; i++) { cin >> P2[i].x >> P2[i].y; } vector<Point> h1 = hull(P1); vector<Point> h2 = hull(P2); string s1 = hullstr(h1); string s2 = hullstr(h2); if ((s1 + s1).find(s2) != string::npos) { cout << "YES\n"; } else { cout << "NO\n"; } }
### Prompt Create a solution in Cpp for the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct Point { long long x, y; Point() = default; Point(long long x, long long y) : x(x), y(y) {} Point operator-(Point other) const { return Point(x - other.x, y - other.y); } bool operator<(Point other) const { return make_pair(x, y) < make_pair(other.x, other.y); } long long operator~() const { return x * x + y * y; } long long operator*(Point other) const { return x * other.x + y * other.y; } long long operator%(Point other) const { return x * other.y - y * other.x; } }; bool ccw(const Point &a, const Point &b, const Point &c) { return (b - a) % (c - b) > 0; } vector<Point> hull(vector<Point> &P) { int n = P.size(); sort(P.begin(), P.end()); vector<Point> H; for (int i = 0; i < n; i++) { while (H.size() > 1 && !ccw(H.end()[-2], H.back(), P[i])) { H.pop_back(); } H.push_back(P[i]); } auto k = H.size(); for (int i = n - 2; i >= 0; i--) { while (H.size() > k && !ccw(H.end()[-2], H.back(), P[i])) { H.pop_back(); } H.push_back(P[i]); } H.pop_back(); return H; } string hullstr(vector<Point> H) { ostringstream oss; H.push_back(H[0]); H.push_back(H[1]); for (int i = 1; i < (int)H.size() - 1; i++) { oss << ',' << ~(H[i] - H[i - 1]) << ',' << ((H[i] - H[i - 1]) * (H[i + 1] - H[i])); } H.pop_back(); H.pop_back(); return oss.str(); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<Point> P1(n); for (int i = 0; i < n; i++) { cin >> P1[i].x >> P1[i].y; } vector<Point> P2(m); for (int i = 0; i < m; i++) { cin >> P2[i].x >> P2[i].y; } vector<Point> h1 = hull(P1); vector<Point> h2 = hull(P2); string s1 = hullstr(h1); string s2 = hullstr(h2); if ((s1 + s1).find(s2) != string::npos) { cout << "YES\n"; } else { cout << "NO\n"; } } ```
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") using namespace std; const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342; const int inf = 2e9; const int MR = 1e5 + 10; struct point { int x, y; bool operator<(const point &p) { if (abs(x) != abs(p.x)) return (abs(x) < abs(p.x)); return (y < p.y); } point operator-(const point &p) { point w; w.x = x - p.x; w.y = y - p.y; return w; } } p, t[MR]; long long det(point p1, point p2) { return p1.x * (long long)p2.y - p2.x * (long long)p1.y; } bool cmp(point p1, point p2) { long long w = det(p1, p2); return ((w > 0) || (!w && p1 < p2)); } vector<point> CH(vector<point> &t) { int n = t.size(); p.x = p.y = inf; for (int c = 0; c < n; c++) { if (t[c].y < p.y || (t[c].y == p.y && t[c].x < p.x)) p = t[c]; } for (int c = 0; c < n; c++) t[c] = t[c] - p; sort(t.begin(), t.end(), cmp); vector<point> s; s.push_back(t[0]); s.push_back(t[1]); for (int i = 2; i < n; i++) { while ((s.size() >= 2) && (det(s[s.size() - 1] - s[s.size() - 2], t[i] - s[s.size() - 1]) <= 0)) s.pop_back(); s.push_back(t[i]); } return s; } long long sqr(int x) { return x * (long long)x; } long long dist(point p1, point p2) { return sqr(p2.x - p1.x) + sqr(p2.y - p1.y); } int P[3 * MR]; void presuf(const vector<pair<long long, long long>> &S) { int j; P[0] = j = 0; for (int i = 1; i < S.size(); i++) { while (j && S[j] != S[i]) j = P[j - 1]; if (S[j] == S[i]) j++; P[i] = j; } } int main() { int n, m; scanf("%d%d", &n, &m); vector<point> Vn, Vm; for (int i = 0; i < (n); ++i) { scanf("%d%d", &p.x, &p.y); Vn.push_back(p); } for (int i = 0; i < (m); ++i) { scanf("%d%d", &p.x, &p.y); Vm.push_back(p); } auto Cn = CH(Vn), Cm = CH(Vm); if (Cn.size() != Cm.size()) { printf("NO\n"); return 0; } int sz = Cn.size(); vector<pair<long long, long long>> Sn, Sm; for (int i = 0; i < (sz); ++i) { int prv = (i - 1 + sz) % sz, nxt = (i + 1) % sz; Sn.push_back(make_pair(dist(Cn[nxt], Cn[i]), det(Cn[i] - Cn[prv], Cn[nxt] - Cn[prv]))); Sm.push_back(make_pair(dist(Cm[nxt], Cm[i]), det(Cm[i] - Cm[prv], Cm[nxt] - Cm[prv]))); } Sn.push_back(make_pair(-1, -1)); for (int i = 0; i < (sz); ++i) Sm.push_back(Sm[i]); for (const auto &p : Sm) Sn.push_back(p); presuf(Sn); for (int i = 0; i < (Sn.size()); ++i) if (P[i] == sz) { printf("YES\n"); return 0; } printf("NO\n"); return 0; }
### Prompt Your task is to create a cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") using namespace std; const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342; const int inf = 2e9; const int MR = 1e5 + 10; struct point { int x, y; bool operator<(const point &p) { if (abs(x) != abs(p.x)) return (abs(x) < abs(p.x)); return (y < p.y); } point operator-(const point &p) { point w; w.x = x - p.x; w.y = y - p.y; return w; } } p, t[MR]; long long det(point p1, point p2) { return p1.x * (long long)p2.y - p2.x * (long long)p1.y; } bool cmp(point p1, point p2) { long long w = det(p1, p2); return ((w > 0) || (!w && p1 < p2)); } vector<point> CH(vector<point> &t) { int n = t.size(); p.x = p.y = inf; for (int c = 0; c < n; c++) { if (t[c].y < p.y || (t[c].y == p.y && t[c].x < p.x)) p = t[c]; } for (int c = 0; c < n; c++) t[c] = t[c] - p; sort(t.begin(), t.end(), cmp); vector<point> s; s.push_back(t[0]); s.push_back(t[1]); for (int i = 2; i < n; i++) { while ((s.size() >= 2) && (det(s[s.size() - 1] - s[s.size() - 2], t[i] - s[s.size() - 1]) <= 0)) s.pop_back(); s.push_back(t[i]); } return s; } long long sqr(int x) { return x * (long long)x; } long long dist(point p1, point p2) { return sqr(p2.x - p1.x) + sqr(p2.y - p1.y); } int P[3 * MR]; void presuf(const vector<pair<long long, long long>> &S) { int j; P[0] = j = 0; for (int i = 1; i < S.size(); i++) { while (j && S[j] != S[i]) j = P[j - 1]; if (S[j] == S[i]) j++; P[i] = j; } } int main() { int n, m; scanf("%d%d", &n, &m); vector<point> Vn, Vm; for (int i = 0; i < (n); ++i) { scanf("%d%d", &p.x, &p.y); Vn.push_back(p); } for (int i = 0; i < (m); ++i) { scanf("%d%d", &p.x, &p.y); Vm.push_back(p); } auto Cn = CH(Vn), Cm = CH(Vm); if (Cn.size() != Cm.size()) { printf("NO\n"); return 0; } int sz = Cn.size(); vector<pair<long long, long long>> Sn, Sm; for (int i = 0; i < (sz); ++i) { int prv = (i - 1 + sz) % sz, nxt = (i + 1) % sz; Sn.push_back(make_pair(dist(Cn[nxt], Cn[i]), det(Cn[i] - Cn[prv], Cn[nxt] - Cn[prv]))); Sm.push_back(make_pair(dist(Cm[nxt], Cm[i]), det(Cm[i] - Cm[prv], Cm[nxt] - Cm[prv]))); } Sn.push_back(make_pair(-1, -1)); for (int i = 0; i < (sz); ++i) Sm.push_back(Sm[i]); for (const auto &p : Sm) Sn.push_back(p); presuf(Sn); for (int i = 0; i < (Sn.size()); ++i) if (P[i] == sz) { printf("YES\n"); return 0; } printf("NO\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const long long INFF = 1e18; const int INF = 1e9; const int mod = 1e9 + 7; const int MAX = 1e5 + 7; struct Point { long long x, y; Point(){}; Point(long long _x, long long _y) { x = _x, y = _y; } void read() { scanf("%lld %lld", &x, &y); } Point operator+(const Point& P2) { return Point(x + P2.x, y + P2.y); } Point operator-(const Point& P2) { return Point(x - P2.x, y - P2.y); } Point operator*(const long long& Len) { return Point(x * Len, y * Len); } long long operator*(const Point& P2) { return x * P2.x + y * P2.y; } long long operator^(const Point& P2) { return x * P2.y - y * P2.x; } long long dis() { return x * x + y * y; } const bool operator<(const Point& p2) const { if (x != p2.x) return x < p2.x; return y < p2.y; } }; struct Line { Point s, e; Line(){}; Line(Point _s, Point _e) { s = _s, e = _e; } void read() { s.read(); e.read(); } }; int ori(Point s, Point e, Point P) { long long val = (s - e) ^ (P - e); if (val == 0) return 0; else if (val > 0) return 1; else return -1; } vector<Point> convex_hull(vector<Point> pt) { sort(pt.begin(), pt.end()); int top = 0; vector<Point> stk(2 * pt.size()); for (int i = 0; i < (int)pt.size(); i++) { while (top >= 2 && ori(stk[top - 2], stk[top - 1], pt[i]) >= 0) top--; stk[top++] = pt[i]; } for (int i = pt.size() - 2, t = top + 1; i >= 0; i--) { while (top >= t && ori(stk[top - 2], stk[top - 1], pt[i]) >= 0) top--; stk[top++] = pt[i]; } stk.resize(top - 1); return stk; } vector<Point> st1, st2; vector<Point> pt1, pt2; long long s1[MAX * 4], s2[MAX * 4]; inline int pre(int p, int sz) { p--; if (p < 0) p += sz; return p; } inline int nxt(int p, int sz) { p++; if (p == sz) p = 0; return p; } int fail[MAX << 2]; void build(int len) { int pos; pos = fail[0] = -1; for (int i = (1); i < (len); ++i) { while (pos != -1 && s2[pos + 1] != s2[i]) { pos = fail[pos]; } if (s2[pos + 1] == s2[i]) pos++; fail[i] = pos; } return; } bool sol(int l1, int l2) { int pos = -1; for (int i = 0; i < (l1); ++i) { while (pos != -1 && s2[pos + 1] != s1[i]) { pos = fail[pos]; } if (s2[pos + 1] == s1[i]) pos++; if (pos == l2 - 1) return 1; } return 0; } int main(void) { int n, m; scanf("%d %d", &n, &m); for (int i = (1); i < (n + 1); ++i) { Point pt; pt.read(); st1.push_back(pt); } for (int i = (1); i < (m + 1); ++i) { Point pt; pt.read(); st2.push_back(pt); } pt1 = convex_hull(st1); pt2 = convex_hull(st2); if (((int)pt1.size()) != ((int)pt2.size())) { puts("NO"); return 0; } int l1 = 0, l2 = 0; for (int i = 0; i < (((int)pt1.size())); ++i) { s1[l1++] = (pt1[i] - pt1[nxt(i, ((int)pt1.size()))]).dis(); s1[l1++] = (pt1[pre(i, ((int)pt1.size()))] - pt1[i]) ^ (pt1[nxt(i, ((int)pt1.size()))] - pt1[i]); } for (int i = 0; i < (((int)pt1.size())); ++i) { s1[l1++] = (pt1[i] - pt1[nxt(i, ((int)pt1.size()))]).dis(); s1[l1++] = (pt1[pre(i, ((int)pt1.size()))] - pt1[i]) ^ (pt1[nxt(i, ((int)pt1.size()))] - pt1[i]); } for (int i = 0; i < (((int)pt2.size())); ++i) { s2[l2++] = (pt2[i] - pt2[nxt(i, ((int)pt2.size()))]).dis(); s2[l2++] = (pt2[pre(i, ((int)pt2.size()))] - pt2[i]) ^ (pt2[nxt(i, ((int)pt2.size()))] - pt2[i]); } build(l2); puts(sol(l1, l2) ? "YES" : "NO"); return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const long long INFF = 1e18; const int INF = 1e9; const int mod = 1e9 + 7; const int MAX = 1e5 + 7; struct Point { long long x, y; Point(){}; Point(long long _x, long long _y) { x = _x, y = _y; } void read() { scanf("%lld %lld", &x, &y); } Point operator+(const Point& P2) { return Point(x + P2.x, y + P2.y); } Point operator-(const Point& P2) { return Point(x - P2.x, y - P2.y); } Point operator*(const long long& Len) { return Point(x * Len, y * Len); } long long operator*(const Point& P2) { return x * P2.x + y * P2.y; } long long operator^(const Point& P2) { return x * P2.y - y * P2.x; } long long dis() { return x * x + y * y; } const bool operator<(const Point& p2) const { if (x != p2.x) return x < p2.x; return y < p2.y; } }; struct Line { Point s, e; Line(){}; Line(Point _s, Point _e) { s = _s, e = _e; } void read() { s.read(); e.read(); } }; int ori(Point s, Point e, Point P) { long long val = (s - e) ^ (P - e); if (val == 0) return 0; else if (val > 0) return 1; else return -1; } vector<Point> convex_hull(vector<Point> pt) { sort(pt.begin(), pt.end()); int top = 0; vector<Point> stk(2 * pt.size()); for (int i = 0; i < (int)pt.size(); i++) { while (top >= 2 && ori(stk[top - 2], stk[top - 1], pt[i]) >= 0) top--; stk[top++] = pt[i]; } for (int i = pt.size() - 2, t = top + 1; i >= 0; i--) { while (top >= t && ori(stk[top - 2], stk[top - 1], pt[i]) >= 0) top--; stk[top++] = pt[i]; } stk.resize(top - 1); return stk; } vector<Point> st1, st2; vector<Point> pt1, pt2; long long s1[MAX * 4], s2[MAX * 4]; inline int pre(int p, int sz) { p--; if (p < 0) p += sz; return p; } inline int nxt(int p, int sz) { p++; if (p == sz) p = 0; return p; } int fail[MAX << 2]; void build(int len) { int pos; pos = fail[0] = -1; for (int i = (1); i < (len); ++i) { while (pos != -1 && s2[pos + 1] != s2[i]) { pos = fail[pos]; } if (s2[pos + 1] == s2[i]) pos++; fail[i] = pos; } return; } bool sol(int l1, int l2) { int pos = -1; for (int i = 0; i < (l1); ++i) { while (pos != -1 && s2[pos + 1] != s1[i]) { pos = fail[pos]; } if (s2[pos + 1] == s1[i]) pos++; if (pos == l2 - 1) return 1; } return 0; } int main(void) { int n, m; scanf("%d %d", &n, &m); for (int i = (1); i < (n + 1); ++i) { Point pt; pt.read(); st1.push_back(pt); } for (int i = (1); i < (m + 1); ++i) { Point pt; pt.read(); st2.push_back(pt); } pt1 = convex_hull(st1); pt2 = convex_hull(st2); if (((int)pt1.size()) != ((int)pt2.size())) { puts("NO"); return 0; } int l1 = 0, l2 = 0; for (int i = 0; i < (((int)pt1.size())); ++i) { s1[l1++] = (pt1[i] - pt1[nxt(i, ((int)pt1.size()))]).dis(); s1[l1++] = (pt1[pre(i, ((int)pt1.size()))] - pt1[i]) ^ (pt1[nxt(i, ((int)pt1.size()))] - pt1[i]); } for (int i = 0; i < (((int)pt1.size())); ++i) { s1[l1++] = (pt1[i] - pt1[nxt(i, ((int)pt1.size()))]).dis(); s1[l1++] = (pt1[pre(i, ((int)pt1.size()))] - pt1[i]) ^ (pt1[nxt(i, ((int)pt1.size()))] - pt1[i]); } for (int i = 0; i < (((int)pt2.size())); ++i) { s2[l2++] = (pt2[i] - pt2[nxt(i, ((int)pt2.size()))]).dis(); s2[l2++] = (pt2[pre(i, ((int)pt2.size()))] - pt2[i]) ^ (pt2[nxt(i, ((int)pt2.size()))] - pt2[i]); } build(l2); puts(sol(l1, l2) ? "YES" : "NO"); return 0; } ```
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using std::abs; using std::cerr; using std::cin; using std::cout; using std::map; using std::max; using std::min; using std::pair; using std::set; using std::string; using std::swap; using std::vector; using ll = long long; using uint = unsigned int; using pii = pair<int, int>; using pll = pair<ll, ll>; using ull = unsigned long long; using ld = long double; template <typename T> void _dbg(const char* _s, T _h) { cerr << _s << " = " << _h << "\n"; } template <typename T, typename... Ts> void _dbg(const char* _s, T _h, Ts... _t) { int _b = 0; while (((_b += *_s == '(') -= *_s == ')') != 0 || *_s != ',') cerr << *_s++; cerr << " = " << _h << ","; _dbg(_s + 1, _t...); } struct init { init() { cin.tie(0); std::iostream::sync_with_stdio(0); cout << std::fixed << std::setprecision(10); cerr << std::fixed << std::setprecision(5); } ~init() {} } init; const int N = 1e5 + 1; using myf = long long; const myf EPS = 1e-10, PI = 3.1415926535897932384626433, MAXC = 1e9; const bool FLOAT_GEOMETRY = std::is_floating_point<myf>::value; inline bool less(myf a, myf b) { return a + EPS < b; } inline bool greater(myf a, myf b) { return a > b + EPS; } inline bool equal(myf a, myf b) { return abs(a - b) / max(myf(1), abs(a)) <= EPS; } inline bool neq(myf a, myf b) { return abs(a - b) / max(myf(1), abs(a)) > EPS; } inline bool leq(myf a, myf b) { return less(a, b) || equal(a, b); } inline bool geq(myf a, myf b) { return greater(a, b) || equal(a, b); } struct point { myf x, y; point() {} point(myf _x, myf _y) : x(_x), y(_y) {} bool operator<(const point& rhs) const { return x == rhs.x ? y < rhs.y : x < rhs.x; } bool operator==(const point& rhs) const { return equal(x, rhs.x) && equal(y, rhs.y); } }; inline myf dist(point a, point b) { return hypotl(b.x - a.x, b.y - a.y); } inline myf sqdist(point a, point b) { return ((b.x - a.x) * (b.x - a.x)) + ((b.y - a.y) * (b.y - a.y)); } inline bool btw(myf o, myf d1, myf d2) { return (less(d1, o) && less(o, d2)) || (less(d2, o) && less(o, d1)); } inline bool btw(point o, point p1, point p2) { return btw(o.x, p1.x, p2.x) && btw(o.y, p1.y, p2.y); } std::istream& operator>>(std::istream& is, point& p) { return is >> p.x >> p.y; } std::ostream& operator<<(std::ostream& os, point& p) { return os << p.x << ' ' << p.y; } struct vc { myf x, y; vc() {} vc(myf _x, myf _y) : x(_x), y(_y) {} vc(point a, point b) { x = b.x - a.x; y = b.y - a.y; } myf length() { return hypotl(x, y); } myf sqlength() { return ((x) * (x)) + ((y) * (y)); } }; point operator+(point a, vc v) { return point(a.x + v.x, a.y + v.y); } vc operator+(vc a, vc v) { return vc(a.x + v.x, a.y + v.y); } myf operator*(vc a, vc b) { return a.x * b.x + a.y * b.y; } myf operator%(vc a, vc b) { return a.x * b.y - a.y * b.x; } vc operator*(vc v, myf x) { return vc(v.x * x, v.y * x); } vc turn_clockwise(vc a) { return vc(a.y, -a.x); } vc turn_anticlockwise(vc a) { return vc(-a.y, a.x); } struct line { myf a, b, c; line() {} line(myf _a, myf _b, myf _c) : a(_a), b(_b), c(_c) { if (FLOAT_GEOMETRY) normalize(); } line(point one, point two) { a = one.y - two.y; b = two.x - one.x; c = -(a * one.x + b * one.y); if (FLOAT_GEOMETRY) normalize(); } line(vc norm, point cont) { a = norm.x; b = norm.y; c = -(a * cont.x + b * cont.y); if (FLOAT_GEOMETRY) normalize(); } void normalize() { myf div = hypotl(a, b); a /= div; b /= div; c /= div; } bool operator()(point p) { return equal(a * p.x + b * p.y + c, 0); } }; struct circle { point center; myf radius; circle() {} circle(point _center, myf _radius) : center(_center), radius(_radius) {} circle(point _center, vc _v) : center(_center) { radius = _v.length(); } bool inside(point p) { return leq(dist(p, center), radius); } bool strict_inside(point p) { return less(dist(p, center), radius); } bool operator()(point p) { return equal(dist(p, center), radius); } }; myf point_segment_dist(point O, point p1, point p2) { return (leq((vc(p1, O) * vc(p1, p2)) * (vc(p2, p1) * vc(p2, O)), 0)) ? min(dist(O, p1), dist(O, p2)) : abs((vc(O, p1) % vc(O, p2)) / dist(p1, p2)); } myf det(myf a, myf b, myf c, myf d) { return a * d - b * c; } int cross(line one, line two, point& p) { myf dx = det(one.c, one.b, two.c, two.b), dy = det(one.a, one.c, two.a, two.c), dd = det(one.a, one.b, two.a, two.b); if (equal(dx, 0) && equal(dy, 0) && equal(dd, 0)) return 2; else if (equal(dd, 0)) return 0; p = point(-dx / dd, -dy / dd); return 1; } int kasat(point A, circle C, point& p1, point& p2) { vc OA(C.center, A); myf l = OA.length(); if (less(l, C.radius)) return 0; myf x = ((C.radius) * (C.radius)) / l; vc OP(OA.x * x / l, OA.y * x / l); point P = C.center + OP; myf hor = sqrtl(((C.radius) * (C.radius)) - OP.sqlength()); p1 = P + turn_clockwise(OP * (hor / x)); p2 = P + turn_anticlockwise(OP * (hor / x)); return equal(l, C.radius) ? 1 : 2; } int cross(line l, circle c, point& p1, point& p2) { if (equal(l.b, 0)) { p1.x = -l.c / l.a; if (geq(((c.radius) * (c.radius)), ((p1.x) * (p1.x)))) { p2.x = p1.x; p1.y = c.center.y + sqrtl(max(myf(0), ((c.radius) * (c.radius)) - ((c.center.x - p1.x) * (c.center.x - p1.x)))); p2.y = c.center.y - sqrtl(max(myf(0), ((c.radius) * (c.radius)) - ((c.center.x - p1.x) * (c.center.x - p1.x)))); return equal(((c.radius) * (c.radius)), ((c.center.x - p1.x) * (c.center.x - p1.x))) ? 1 : 2; } return 0; } myf tt = (l.c + l.b * c.center.y) / l.b; myf ta = 1 + ((l.a / l.b) * (l.a / l.b)), tb = 2 * (tt * l.a / l.b - c.center.x), tc = ((tt) * (tt)) - ((c.radius) * (c.radius)) + ((c.center.x) * (c.center.x)); myf td = ((tb) * (tb)) - 4 * ta * tc; if (less(td, 0)) return 0; p1.x = (-tb + sqrtl(max(myf(0), td))) / (2 * ta); p2.x = (-tb - sqrtl(max(myf(0), td))) / (2 * ta); p1.y = (-l.c - l.a * p1.x) / l.b; p2.y = (-l.c - l.a * p2.x) / l.b; return equal(td, 0) ? 1 : 2; } int cross(circle one, circle two, point& p1, point& p2) { if (equal(one.center.x, two.center.x) && equal(one.center.y, two.center.y)) return equal(one.radius, two.radius) ? 3 : 0; line l(2 * (two.center.x - one.center.x), 2 * (two.center.y - one.center.y), ((one.center.x) * (one.center.x)) - ((two.center.x) * (two.center.x)) + ((one.center.y) * (one.center.y)) - ((two.center.y) * (two.center.y)) + ((two.radius) * (two.radius)) - ((one.radius) * (one.radius))); return cross(l, one, p1, p2); } bool inside_convex_polygon(const vector<point>& a, point p) { int n = a.size() - 1, l = 1, r = a.size() - 2; vc cur(a[0], p); if (less(vc(a[0], a[1]) % cur, 0) || less(cur % vc(a[0], a[n - 1]), 0)) return false; while (r - l > 1) { int m = l + r >> 1; if (geq(cur % vc(a[0], a[m]), 0)) r = m; else l = m; } return leq(vc(a[r], p) % vc(a[r], a[r + 1]), 0) && leq(vc(a[r - 1], p) % vc(a[r - 1], a[r]), 0); } point ch_start; bool ch_comp(const point& p1, const point& p2) { vc op1(ch_start, p1), op2(ch_start, p2); if (op1 % op2 == 0) return ((op1.x) * (op1.x)) + ((op1.y) * (op1.y)) < ((op2.x) * (op2.x)) + ((op2.y) * (op2.y)); return op1 % op2 > 0; } vector<point> convex_hull(const vector<point>& p) { if (p.empty()) return {}; ch_start = *std::min_element(p.begin(), p.end()); vector<point> a; for (const point& i : p) if (i.x != ch_start.x || i.y != ch_start.y) a.emplace_back(i); sort(a.begin(), a.end(), ch_comp); if (a.empty()) return {ch_start}; vector<point> ans = {ch_start, a[0]}; for (uint i = 1; i < a.size(); ++i) { while (ans.size() >= 2 && vc(ans[ans.size() - 2], ans.back()) % vc(ans.back(), a[i]) <= 0) ans.pop_back(); ans.emplace_back(a[i]); } return ans; } bool check(vector<point> a, vector<point> b) { int n = a.size(); for (int i = 1; i < n; ++i) { if (a[i].x - a[i - 1].x != b[i].x - b[i - 1].x || a[i].y - a[i - 1].y != b[i].y - b[i - 1].y) return 0; } return 1; } vector<int> zfunc(const vector<ll>& s) { int n = s.size(); vector<int> z(n, 0); int left = 0, right = 0; for (int i = 1; i < n; ++i) { if (i < right) z[i] = min(z[i - left], right - i); while ((i + z[i] < n) && (s[z[i]] == s[i + z[i]])) ++z[i]; if (i + z[i] > right) { left = i; right = i + z[i]; } } return z; } const ll INF = 1e18; int main() { int n, m; cin >> n >> m; vector<point> a(n), b(m); for (auto& i : a) cin >> i; for (auto& i : b) cin >> i; a = convex_hull(a); b = convex_hull(b); for (auto i : a) ; for (auto j : b) ; if (a.size() != b.size()) { cout << "NO\n"; return 0; } n = a.size(); for (int i = 0; i < n; ++i) a.emplace_back(a[i]); a.emplace_back(a[0]); a.emplace_back(a[1]); vector<ll> order; b.emplace_back(b[0]); b.emplace_back(b[1]); for (int i = 0; i < n; ++i) { order.emplace_back(sqdist(b[i], b[i + 1])); vc v1(b[i + 1], b[i]); vc v2(b[i + 1], b[i + 2]); order.emplace_back(v1 % v2); order.emplace_back(v1 * v2); } order.emplace_back(INF); for (int i = 0; i < n + n; ++i) { order.emplace_back(sqdist(a[i], a[i + 1])); vc v1(a[i + 1], a[i]); vc v2(a[i + 1], a[i + 2]); order.emplace_back(v1 % v2); order.emplace_back(v1 * v2); }; auto z = zfunc(order); ; for (int i = n * 3; i < order.size(); ++i) { if (z[i] == n * 3) { cout << "YES\n"; return 0; } } cout << "NO\n"; return 0; }
### Prompt In CPP, your task is to solve the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using std::abs; using std::cerr; using std::cin; using std::cout; using std::map; using std::max; using std::min; using std::pair; using std::set; using std::string; using std::swap; using std::vector; using ll = long long; using uint = unsigned int; using pii = pair<int, int>; using pll = pair<ll, ll>; using ull = unsigned long long; using ld = long double; template <typename T> void _dbg(const char* _s, T _h) { cerr << _s << " = " << _h << "\n"; } template <typename T, typename... Ts> void _dbg(const char* _s, T _h, Ts... _t) { int _b = 0; while (((_b += *_s == '(') -= *_s == ')') != 0 || *_s != ',') cerr << *_s++; cerr << " = " << _h << ","; _dbg(_s + 1, _t...); } struct init { init() { cin.tie(0); std::iostream::sync_with_stdio(0); cout << std::fixed << std::setprecision(10); cerr << std::fixed << std::setprecision(5); } ~init() {} } init; const int N = 1e5 + 1; using myf = long long; const myf EPS = 1e-10, PI = 3.1415926535897932384626433, MAXC = 1e9; const bool FLOAT_GEOMETRY = std::is_floating_point<myf>::value; inline bool less(myf a, myf b) { return a + EPS < b; } inline bool greater(myf a, myf b) { return a > b + EPS; } inline bool equal(myf a, myf b) { return abs(a - b) / max(myf(1), abs(a)) <= EPS; } inline bool neq(myf a, myf b) { return abs(a - b) / max(myf(1), abs(a)) > EPS; } inline bool leq(myf a, myf b) { return less(a, b) || equal(a, b); } inline bool geq(myf a, myf b) { return greater(a, b) || equal(a, b); } struct point { myf x, y; point() {} point(myf _x, myf _y) : x(_x), y(_y) {} bool operator<(const point& rhs) const { return x == rhs.x ? y < rhs.y : x < rhs.x; } bool operator==(const point& rhs) const { return equal(x, rhs.x) && equal(y, rhs.y); } }; inline myf dist(point a, point b) { return hypotl(b.x - a.x, b.y - a.y); } inline myf sqdist(point a, point b) { return ((b.x - a.x) * (b.x - a.x)) + ((b.y - a.y) * (b.y - a.y)); } inline bool btw(myf o, myf d1, myf d2) { return (less(d1, o) && less(o, d2)) || (less(d2, o) && less(o, d1)); } inline bool btw(point o, point p1, point p2) { return btw(o.x, p1.x, p2.x) && btw(o.y, p1.y, p2.y); } std::istream& operator>>(std::istream& is, point& p) { return is >> p.x >> p.y; } std::ostream& operator<<(std::ostream& os, point& p) { return os << p.x << ' ' << p.y; } struct vc { myf x, y; vc() {} vc(myf _x, myf _y) : x(_x), y(_y) {} vc(point a, point b) { x = b.x - a.x; y = b.y - a.y; } myf length() { return hypotl(x, y); } myf sqlength() { return ((x) * (x)) + ((y) * (y)); } }; point operator+(point a, vc v) { return point(a.x + v.x, a.y + v.y); } vc operator+(vc a, vc v) { return vc(a.x + v.x, a.y + v.y); } myf operator*(vc a, vc b) { return a.x * b.x + a.y * b.y; } myf operator%(vc a, vc b) { return a.x * b.y - a.y * b.x; } vc operator*(vc v, myf x) { return vc(v.x * x, v.y * x); } vc turn_clockwise(vc a) { return vc(a.y, -a.x); } vc turn_anticlockwise(vc a) { return vc(-a.y, a.x); } struct line { myf a, b, c; line() {} line(myf _a, myf _b, myf _c) : a(_a), b(_b), c(_c) { if (FLOAT_GEOMETRY) normalize(); } line(point one, point two) { a = one.y - two.y; b = two.x - one.x; c = -(a * one.x + b * one.y); if (FLOAT_GEOMETRY) normalize(); } line(vc norm, point cont) { a = norm.x; b = norm.y; c = -(a * cont.x + b * cont.y); if (FLOAT_GEOMETRY) normalize(); } void normalize() { myf div = hypotl(a, b); a /= div; b /= div; c /= div; } bool operator()(point p) { return equal(a * p.x + b * p.y + c, 0); } }; struct circle { point center; myf radius; circle() {} circle(point _center, myf _radius) : center(_center), radius(_radius) {} circle(point _center, vc _v) : center(_center) { radius = _v.length(); } bool inside(point p) { return leq(dist(p, center), radius); } bool strict_inside(point p) { return less(dist(p, center), radius); } bool operator()(point p) { return equal(dist(p, center), radius); } }; myf point_segment_dist(point O, point p1, point p2) { return (leq((vc(p1, O) * vc(p1, p2)) * (vc(p2, p1) * vc(p2, O)), 0)) ? min(dist(O, p1), dist(O, p2)) : abs((vc(O, p1) % vc(O, p2)) / dist(p1, p2)); } myf det(myf a, myf b, myf c, myf d) { return a * d - b * c; } int cross(line one, line two, point& p) { myf dx = det(one.c, one.b, two.c, two.b), dy = det(one.a, one.c, two.a, two.c), dd = det(one.a, one.b, two.a, two.b); if (equal(dx, 0) && equal(dy, 0) && equal(dd, 0)) return 2; else if (equal(dd, 0)) return 0; p = point(-dx / dd, -dy / dd); return 1; } int kasat(point A, circle C, point& p1, point& p2) { vc OA(C.center, A); myf l = OA.length(); if (less(l, C.radius)) return 0; myf x = ((C.radius) * (C.radius)) / l; vc OP(OA.x * x / l, OA.y * x / l); point P = C.center + OP; myf hor = sqrtl(((C.radius) * (C.radius)) - OP.sqlength()); p1 = P + turn_clockwise(OP * (hor / x)); p2 = P + turn_anticlockwise(OP * (hor / x)); return equal(l, C.radius) ? 1 : 2; } int cross(line l, circle c, point& p1, point& p2) { if (equal(l.b, 0)) { p1.x = -l.c / l.a; if (geq(((c.radius) * (c.radius)), ((p1.x) * (p1.x)))) { p2.x = p1.x; p1.y = c.center.y + sqrtl(max(myf(0), ((c.radius) * (c.radius)) - ((c.center.x - p1.x) * (c.center.x - p1.x)))); p2.y = c.center.y - sqrtl(max(myf(0), ((c.radius) * (c.radius)) - ((c.center.x - p1.x) * (c.center.x - p1.x)))); return equal(((c.radius) * (c.radius)), ((c.center.x - p1.x) * (c.center.x - p1.x))) ? 1 : 2; } return 0; } myf tt = (l.c + l.b * c.center.y) / l.b; myf ta = 1 + ((l.a / l.b) * (l.a / l.b)), tb = 2 * (tt * l.a / l.b - c.center.x), tc = ((tt) * (tt)) - ((c.radius) * (c.radius)) + ((c.center.x) * (c.center.x)); myf td = ((tb) * (tb)) - 4 * ta * tc; if (less(td, 0)) return 0; p1.x = (-tb + sqrtl(max(myf(0), td))) / (2 * ta); p2.x = (-tb - sqrtl(max(myf(0), td))) / (2 * ta); p1.y = (-l.c - l.a * p1.x) / l.b; p2.y = (-l.c - l.a * p2.x) / l.b; return equal(td, 0) ? 1 : 2; } int cross(circle one, circle two, point& p1, point& p2) { if (equal(one.center.x, two.center.x) && equal(one.center.y, two.center.y)) return equal(one.radius, two.radius) ? 3 : 0; line l(2 * (two.center.x - one.center.x), 2 * (two.center.y - one.center.y), ((one.center.x) * (one.center.x)) - ((two.center.x) * (two.center.x)) + ((one.center.y) * (one.center.y)) - ((two.center.y) * (two.center.y)) + ((two.radius) * (two.radius)) - ((one.radius) * (one.radius))); return cross(l, one, p1, p2); } bool inside_convex_polygon(const vector<point>& a, point p) { int n = a.size() - 1, l = 1, r = a.size() - 2; vc cur(a[0], p); if (less(vc(a[0], a[1]) % cur, 0) || less(cur % vc(a[0], a[n - 1]), 0)) return false; while (r - l > 1) { int m = l + r >> 1; if (geq(cur % vc(a[0], a[m]), 0)) r = m; else l = m; } return leq(vc(a[r], p) % vc(a[r], a[r + 1]), 0) && leq(vc(a[r - 1], p) % vc(a[r - 1], a[r]), 0); } point ch_start; bool ch_comp(const point& p1, const point& p2) { vc op1(ch_start, p1), op2(ch_start, p2); if (op1 % op2 == 0) return ((op1.x) * (op1.x)) + ((op1.y) * (op1.y)) < ((op2.x) * (op2.x)) + ((op2.y) * (op2.y)); return op1 % op2 > 0; } vector<point> convex_hull(const vector<point>& p) { if (p.empty()) return {}; ch_start = *std::min_element(p.begin(), p.end()); vector<point> a; for (const point& i : p) if (i.x != ch_start.x || i.y != ch_start.y) a.emplace_back(i); sort(a.begin(), a.end(), ch_comp); if (a.empty()) return {ch_start}; vector<point> ans = {ch_start, a[0]}; for (uint i = 1; i < a.size(); ++i) { while (ans.size() >= 2 && vc(ans[ans.size() - 2], ans.back()) % vc(ans.back(), a[i]) <= 0) ans.pop_back(); ans.emplace_back(a[i]); } return ans; } bool check(vector<point> a, vector<point> b) { int n = a.size(); for (int i = 1; i < n; ++i) { if (a[i].x - a[i - 1].x != b[i].x - b[i - 1].x || a[i].y - a[i - 1].y != b[i].y - b[i - 1].y) return 0; } return 1; } vector<int> zfunc(const vector<ll>& s) { int n = s.size(); vector<int> z(n, 0); int left = 0, right = 0; for (int i = 1; i < n; ++i) { if (i < right) z[i] = min(z[i - left], right - i); while ((i + z[i] < n) && (s[z[i]] == s[i + z[i]])) ++z[i]; if (i + z[i] > right) { left = i; right = i + z[i]; } } return z; } const ll INF = 1e18; int main() { int n, m; cin >> n >> m; vector<point> a(n), b(m); for (auto& i : a) cin >> i; for (auto& i : b) cin >> i; a = convex_hull(a); b = convex_hull(b); for (auto i : a) ; for (auto j : b) ; if (a.size() != b.size()) { cout << "NO\n"; return 0; } n = a.size(); for (int i = 0; i < n; ++i) a.emplace_back(a[i]); a.emplace_back(a[0]); a.emplace_back(a[1]); vector<ll> order; b.emplace_back(b[0]); b.emplace_back(b[1]); for (int i = 0; i < n; ++i) { order.emplace_back(sqdist(b[i], b[i + 1])); vc v1(b[i + 1], b[i]); vc v2(b[i + 1], b[i + 2]); order.emplace_back(v1 % v2); order.emplace_back(v1 * v2); } order.emplace_back(INF); for (int i = 0; i < n + n; ++i) { order.emplace_back(sqdist(a[i], a[i + 1])); vc v1(a[i + 1], a[i]); vc v2(a[i + 1], a[i + 2]); order.emplace_back(v1 % v2); order.emplace_back(v1 * v2); }; auto z = zfunc(order); ; for (int i = n * 3; i < order.size(); ++i) { if (z[i] == n * 3) { cout << "YES\n"; return 0; } } cout << "NO\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; using lint = long long; using pi = pair<int, int>; lint ccw(pi a, pi b, pi c) { int dx1 = b.first - a.first; int dy1 = b.second - a.second; int dx2 = c.first - a.first; int dy2 = c.second - a.second; return 1ll * dx1 * dy2 - 1ll * dy1 * dx2; } lint dist(pi a, pi b) { int dx1 = b.first - a.first; int dy1 = b.second - a.second; return 1ll * dx1 * dx1 + 1ll * dy1 * dy1; } int fail[1000005]; bool rot_same(vector<lint> a, vector<lint> b) { for (int i = 0; i < a.size(); i++) b.push_back(b[i]); int p = 0; for (int i = 1; i < a.size(); i++) { while (p && a[i] != a[p]) p = fail[p]; if (a[i] == a[p]) p++; fail[i + 1] = p; } p = 0; for (auto &i : b) { while (p && i != a[p]) p = fail[p]; if (i == a[p]) p++; if (p == a.size()) return true; } return false; } vector<pi> getPoly(int n) { vector<pi> h; for (int i = 0; i < n; i++) { int x, y; scanf("%d %d", &x, &y); h.emplace_back(x, y); } swap(h[0], *min_element(h.begin(), h.end())); sort(h.begin() + 1, h.end(), [&](const pi &a, const pi &b) { auto x = ccw(h[0], a, b); if (x != 0) return x > 0; return dist(h[0], a) < dist(h[0], b); }); vector<pi> poly; for (auto &i : h) { while (poly.size() >= 2 && ccw(poly[poly.size() - 2], poly.back(), i) <= 0) { poly.pop_back(); } poly.push_back(i); } return poly; } int main() { int n, m; cin >> n >> m; auto p1 = getPoly(n); auto p2 = getPoly(m); if (p1.size() != p2.size()) { puts("NO"); return 0; } if (p1.size() == 2 && p2.size() == 2) { lint c1 = dist(p1[0], p1[1]); lint c2 = dist(p2[0], p2[1]); if (c1 != c2) puts("NO"); else puts("YES"); return 0; } n = p1.size(); vector<lint> seq1, seq2; for (int i = 0; i < n; i++) { seq1.push_back(-dist(p1[i], p1[(i + 1) % n])); seq1.push_back(ccw(p1[i], p1[(i + 1) % n], p1[(i + 2) % n])); seq2.push_back(-dist(p2[i], p2[(i + 1) % n])); seq2.push_back(ccw(p2[i], p2[(i + 1) % n], p2[(i + 2) % n])); } puts(rot_same(seq1, seq2) ? "YES" : "NO"); }
### Prompt Please provide a Cpp coded solution to the problem described below: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; using lint = long long; using pi = pair<int, int>; lint ccw(pi a, pi b, pi c) { int dx1 = b.first - a.first; int dy1 = b.second - a.second; int dx2 = c.first - a.first; int dy2 = c.second - a.second; return 1ll * dx1 * dy2 - 1ll * dy1 * dx2; } lint dist(pi a, pi b) { int dx1 = b.first - a.first; int dy1 = b.second - a.second; return 1ll * dx1 * dx1 + 1ll * dy1 * dy1; } int fail[1000005]; bool rot_same(vector<lint> a, vector<lint> b) { for (int i = 0; i < a.size(); i++) b.push_back(b[i]); int p = 0; for (int i = 1; i < a.size(); i++) { while (p && a[i] != a[p]) p = fail[p]; if (a[i] == a[p]) p++; fail[i + 1] = p; } p = 0; for (auto &i : b) { while (p && i != a[p]) p = fail[p]; if (i == a[p]) p++; if (p == a.size()) return true; } return false; } vector<pi> getPoly(int n) { vector<pi> h; for (int i = 0; i < n; i++) { int x, y; scanf("%d %d", &x, &y); h.emplace_back(x, y); } swap(h[0], *min_element(h.begin(), h.end())); sort(h.begin() + 1, h.end(), [&](const pi &a, const pi &b) { auto x = ccw(h[0], a, b); if (x != 0) return x > 0; return dist(h[0], a) < dist(h[0], b); }); vector<pi> poly; for (auto &i : h) { while (poly.size() >= 2 && ccw(poly[poly.size() - 2], poly.back(), i) <= 0) { poly.pop_back(); } poly.push_back(i); } return poly; } int main() { int n, m; cin >> n >> m; auto p1 = getPoly(n); auto p2 = getPoly(m); if (p1.size() != p2.size()) { puts("NO"); return 0; } if (p1.size() == 2 && p2.size() == 2) { lint c1 = dist(p1[0], p1[1]); lint c2 = dist(p2[0], p2[1]); if (c1 != c2) puts("NO"); else puts("YES"); return 0; } n = p1.size(); vector<lint> seq1, seq2; for (int i = 0; i < n; i++) { seq1.push_back(-dist(p1[i], p1[(i + 1) % n])); seq1.push_back(ccw(p1[i], p1[(i + 1) % n], p1[(i + 2) % n])); seq2.push_back(-dist(p2[i], p2[(i + 1) % n])); seq2.push_back(ccw(p2[i], p2[(i + 1) % n], p2[(i + 2) % n])); } puts(rot_same(seq1, seq2) ? "YES" : "NO"); } ```
#include <bits/stdc++.h> using namespace std; const int N = 100005; struct Point { long long x, y; Point(long long x = 0, long long y = 0) : x(x), y(y) {} Point operator-(const Point& r) const { return Point(x - r.x, y - r.y); } bool operator<(const Point& r) const { return x == r.x ? y < r.y : x < r.x; } }; set<Point> S1, S2; long long cross(Point u, Point v) { return u.x * v.y - u.y * v.x; } long long dot(Point u, Point v) { return u.x * v.x + u.y * v.y; } int ConvexHull(Point p[], int n, Point ch[]) { sort(p, p + n); int m = 0; for (int i = 0; i < n; i++) { while (m > 1 && cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; ch[m++] = p[i]; } int k = m; for (int i = n - 2; i >= 0; i--) { while (m > k && cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; ch[m++] = p[i]; } if (n > 1) m--; return m; } vector<unsigned> smallestRepresation(vector<unsigned> S) { int i = 0, j = 1; int n = S.size(); for (int i = 0; i < n; i++) { S.push_back(S[i]); } while (j < n) { int k; for (k = 0; k < n && S[i + k] == S[j + k]; k++) ; if (k >= n) break; if (S[i + k] < S[j + k]) j += k + 1; else { int t = i; i = j; j = max(j, t + k) + 1; } } return vector<unsigned>(S.begin() + i, S.begin() + n + i); } Point p[N], q[N], ch[N]; vector<unsigned> a, b; unsigned calc(Point p[], int i, int j, int k) { unsigned ret = 0; unsigned o = 9997907; ret += dot(p[i] - p[j], p[i] - p[j]) * o; o = o * 9997907; ret += dot(p[k] - p[j], p[k] - p[j]) * o; o = o * 9997907; ret += cross(p[j] - p[i], p[k] - p[j]); return ret; } int n, m; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%lld%lld", &p[i].x, &p[i].y); } for (int i = 0; i < m; i++) { scanf("%lld%lld", &q[i].x, &q[i].y); } n = ConvexHull(p, n, ch); for (int i = 0; i < n; i++) p[i] = ch[i]; m = ConvexHull(q, m, ch); for (int i = 0; i < m; i++) q[i] = ch[i]; if (n != m) { puts("NO"); return 0; } for (int i = 0; i < n; i++) { a.push_back(calc(p, i, (i + 1) % n, (i + 2) % n)); b.push_back(calc(q, i, (i + 1) % n, (i + 2) % n)); } a = smallestRepresation(a); b = smallestRepresation(b); if (a == b) puts("YES"); else puts("NO"); return 0; }
### Prompt Please formulate a cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 100005; struct Point { long long x, y; Point(long long x = 0, long long y = 0) : x(x), y(y) {} Point operator-(const Point& r) const { return Point(x - r.x, y - r.y); } bool operator<(const Point& r) const { return x == r.x ? y < r.y : x < r.x; } }; set<Point> S1, S2; long long cross(Point u, Point v) { return u.x * v.y - u.y * v.x; } long long dot(Point u, Point v) { return u.x * v.x + u.y * v.y; } int ConvexHull(Point p[], int n, Point ch[]) { sort(p, p + n); int m = 0; for (int i = 0; i < n; i++) { while (m > 1 && cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; ch[m++] = p[i]; } int k = m; for (int i = n - 2; i >= 0; i--) { while (m > k && cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; ch[m++] = p[i]; } if (n > 1) m--; return m; } vector<unsigned> smallestRepresation(vector<unsigned> S) { int i = 0, j = 1; int n = S.size(); for (int i = 0; i < n; i++) { S.push_back(S[i]); } while (j < n) { int k; for (k = 0; k < n && S[i + k] == S[j + k]; k++) ; if (k >= n) break; if (S[i + k] < S[j + k]) j += k + 1; else { int t = i; i = j; j = max(j, t + k) + 1; } } return vector<unsigned>(S.begin() + i, S.begin() + n + i); } Point p[N], q[N], ch[N]; vector<unsigned> a, b; unsigned calc(Point p[], int i, int j, int k) { unsigned ret = 0; unsigned o = 9997907; ret += dot(p[i] - p[j], p[i] - p[j]) * o; o = o * 9997907; ret += dot(p[k] - p[j], p[k] - p[j]) * o; o = o * 9997907; ret += cross(p[j] - p[i], p[k] - p[j]); return ret; } int n, m; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%lld%lld", &p[i].x, &p[i].y); } for (int i = 0; i < m; i++) { scanf("%lld%lld", &q[i].x, &q[i].y); } n = ConvexHull(p, n, ch); for (int i = 0; i < n; i++) p[i] = ch[i]; m = ConvexHull(q, m, ch); for (int i = 0; i < m; i++) q[i] = ch[i]; if (n != m) { puts("NO"); return 0; } for (int i = 0; i < n; i++) { a.push_back(calc(p, i, (i + 1) % n, (i + 2) % n)); b.push_back(calc(q, i, (i + 1) % n, (i + 2) % n)); } a = smallestRepresation(a); b = smallestRepresation(b); if (a == b) puts("YES"); else puts("NO"); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int inf = 0x7fffffff; const double eps = 1e-10; const double pi = acos(-1.0); inline int read() { int x = 0, f = 1; char ch; ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = 0; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch & 15); ch = getchar(); } if (f) return x; else return -x; } const int N = 1e5 + 10; int n, m; int dcmp(double x) { if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } struct Point { double x, y; } P1[N], F1[N], P2[N], F2[N]; inline Point operator-(const Point &a, const Point &b) { return (Point){a.x - b.x, a.y - b.y}; } inline bool operator<(const Point &a, const Point &b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } double Dot(const Point &a, const Point &b) { return 1.0 * a.x * b.x + 1.0 * a.y * b.y; } double Length(const Point &a) { return sqrt(Dot(a, a)); } double Angle(const Point &a, const Point &b) { return acos(Dot(a, b) / Length(a) / Length(b)); } double Cross(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; } int ConcexHull(Point *P, int n, Point *F) { sort(P, P + n); int m = 0; for (int i = 0; i < n; i++) { while (m > 1 && Cross(F[m - 1] - F[m - 2], P[i] - F[m - 2]) <= 0) m--; F[m++] = P[i]; } int k = m; for (int i = n - 2; i >= 0; i--) { while (m > k && Cross(F[m - 1] - F[m - 2], P[i] - F[m - 2]) <= 0) m--; F[m++] = P[i]; } if (n > 1) m--; return m; } double S[N << 2], T[N << 1]; int tops, topt; int nex[N << 1]; bool KMP(double s[], double p[], int slen, int plen) { nex[0] = -1; int j = 0, i = 1; for (i = 1; i <= plen; i++) { while (j > 0 && !fabs(p[i] - p[j]) < eps) j = nex[j]; if (fabs(p[i] - p[j]) < eps) j++; nex[i + 1] = j; } i = j = 0; while (i < slen && j < plen) { if (!fabs(s[i] - p[j]) < eps) { if (j == 0) i++; j = nex[j]; } while (j < plen && fabs(s[i] - p[j]) < eps) i++, j++; if (j == plen) return 1; } return 0; } int main() { n = read(); m = read(); for (int i = 0; i < n; i++) P1[i].x = 1.0 * read(), P1[i].y = 1.0 * read(); for (int i = 0; i < m; i++) P2[i].x = 1.0 * read(), P2[i].y = 1.0 * read(); n = ConcexHull(P1, n, F1); m = ConcexHull(P2, m, F2); if (n != m) { printf("NO\n"); return 0; } F1[n] = F1[0]; F1[n + 1] = F1[1]; F2[m] = F2[0]; F2[m + 1] = F2[1]; for (int i = 0; i < n; i++) { S[tops++] = Length(F1[i + 1] - F1[i]); S[tops++] = Angle(F1[i + 2] - F1[i + 1], F1[i + 1] - F1[i]); } for (int i = 0; i < m; i++) { T[topt++] = Length(F2[i + 1] - F2[i]); T[topt++] = Angle(F2[i + 2] - F2[i + 1], F2[i + 1] - F2[i]); } for (int i = tops; i < tops * 2 - 1; i++) S[i] = S[i - tops]; if (KMP(S, T, tops * 2 - 2, topt - 1)) printf("YES\n"); else printf("NO\n"); return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int inf = 0x7fffffff; const double eps = 1e-10; const double pi = acos(-1.0); inline int read() { int x = 0, f = 1; char ch; ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = 0; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch & 15); ch = getchar(); } if (f) return x; else return -x; } const int N = 1e5 + 10; int n, m; int dcmp(double x) { if (fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } struct Point { double x, y; } P1[N], F1[N], P2[N], F2[N]; inline Point operator-(const Point &a, const Point &b) { return (Point){a.x - b.x, a.y - b.y}; } inline bool operator<(const Point &a, const Point &b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } double Dot(const Point &a, const Point &b) { return 1.0 * a.x * b.x + 1.0 * a.y * b.y; } double Length(const Point &a) { return sqrt(Dot(a, a)); } double Angle(const Point &a, const Point &b) { return acos(Dot(a, b) / Length(a) / Length(b)); } double Cross(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; } int ConcexHull(Point *P, int n, Point *F) { sort(P, P + n); int m = 0; for (int i = 0; i < n; i++) { while (m > 1 && Cross(F[m - 1] - F[m - 2], P[i] - F[m - 2]) <= 0) m--; F[m++] = P[i]; } int k = m; for (int i = n - 2; i >= 0; i--) { while (m > k && Cross(F[m - 1] - F[m - 2], P[i] - F[m - 2]) <= 0) m--; F[m++] = P[i]; } if (n > 1) m--; return m; } double S[N << 2], T[N << 1]; int tops, topt; int nex[N << 1]; bool KMP(double s[], double p[], int slen, int plen) { nex[0] = -1; int j = 0, i = 1; for (i = 1; i <= plen; i++) { while (j > 0 && !fabs(p[i] - p[j]) < eps) j = nex[j]; if (fabs(p[i] - p[j]) < eps) j++; nex[i + 1] = j; } i = j = 0; while (i < slen && j < plen) { if (!fabs(s[i] - p[j]) < eps) { if (j == 0) i++; j = nex[j]; } while (j < plen && fabs(s[i] - p[j]) < eps) i++, j++; if (j == plen) return 1; } return 0; } int main() { n = read(); m = read(); for (int i = 0; i < n; i++) P1[i].x = 1.0 * read(), P1[i].y = 1.0 * read(); for (int i = 0; i < m; i++) P2[i].x = 1.0 * read(), P2[i].y = 1.0 * read(); n = ConcexHull(P1, n, F1); m = ConcexHull(P2, m, F2); if (n != m) { printf("NO\n"); return 0; } F1[n] = F1[0]; F1[n + 1] = F1[1]; F2[m] = F2[0]; F2[m + 1] = F2[1]; for (int i = 0; i < n; i++) { S[tops++] = Length(F1[i + 1] - F1[i]); S[tops++] = Angle(F1[i + 2] - F1[i + 1], F1[i + 1] - F1[i]); } for (int i = 0; i < m; i++) { T[topt++] = Length(F2[i + 1] - F2[i]); T[topt++] = Angle(F2[i + 2] - F2[i + 1], F2[i + 1] - F2[i]); } for (int i = tops; i < tops * 2 - 1; i++) S[i] = S[i - tops]; if (KMP(S, T, tops * 2 - 2, topt - 1)) printf("YES\n"); else printf("NO\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; struct hh { double x; long long y; bool operator==(const hh& b) const { return fabs(x - b.x) < eps && y == b.y; } bool operator!=(const hh& b) const { return !((*this) == b); } } a[100010 * 2], b[100010]; int n, m; inline bool eq(int a, int b) { return a == b; } struct point { long long x, y; point() : x(0), y(0) {} point(int a, int b) : x(a), y(b) {} point(point const& f) : x(f.x), y(f.y) {} point operator=(point const& f) { x = f.x; y = f.y; return *this; } long long len2() const { return x * x + y * y; } double len() const { return sqrt(len2()); } point operator+(point const& b) const { return point(x + b.x, y + b.y); } point operator-(point const& b) const { return point(x - b.x, y - b.y); } point operator()(point const& b) const { return b - *this; } }; point operator*(point const& a, double const& b) { return point(a.x * b, a.y * b); } point operator*(double const& b, point const& a) { return point(a.x * b, a.y * b); } long long operator*(point const& a, point const& b) { return (long long)a.x * b.y - (long long)a.y * b.x; } long long operator&(point const& a, point const& b) { return (long long)a.x * b.x + (long long)a.y * b.y; } bool operator==(point const& a, point const& b) { return eq(a.x, b.x) && eq(a.y, b.y); } bool operator>>=(point const& a, point const& b) { return a * b >= 0; } double angle(point const& a, point const& b) { double x = a & b; x /= a.len(); x /= b.len(); return x; } point a1[100010], a2[100010]; point stk[100010]; int st; bool operator<(point const& a, point const& b) { return eq(a.y, b.y) ? a.x < b.x : a.y < b.y; } void Graham(point a[], int n) { sort(a, a + n); st = 0; for (int i = 0; i < n; i++) { while (st > 1 && (stk[st - 2](stk[st - 1]) >>= stk[st - 1](a[i]))) st--; stk[st++] = a[i]; } int p = st; for (int i = n - 2; i >= 0; i--) { while (st > p && (stk[st - 2](stk[st - 1]) >>= stk[st - 1](a[i]))) st--; stk[st++] = a[i]; } } int nxt[100010 * 2]; void getnxt() { int j = -1; nxt[0] = -1; for (int i = 1, lim = m - 1; i <= lim; ++i) { while (j != -1 && b[i] != b[j + 1]) j = nxt[j]; j += b[i] == b[j + 1]; nxt[i] = j; } } bool pipei() { int j = -1; for (int i = 0, lim = n - 1; i <= lim; ++i) { while (j != -1 && a[i] != b[j + 1]) j = nxt[j]; j += a[i] == b[j + 1]; if (j == m - 1) return 1; } return 0; } int main() { scanf("%d", &n), scanf("%d", &m); for (int i = 0, lim = n - 1; i <= lim; ++i) scanf("%d", &a1[i].x), scanf("%d", &a1[i].y); for (int i = 0, lim = m - 1; i <= lim; ++i) scanf("%d", &a2[i].x), scanf("%d", &a2[i].y); Graham(a1, n); n = st; a[0].x = angle(stk[0](stk[1]), stk[0](stk[st - 2])); a[0].y = stk[0](stk[st - 2]).len2(); a[st - 1] = a[0]; for (int i = 1, lim = st - 2; i <= lim; ++i) { a[i].x = angle(stk[i](stk[i + 1]), stk[i](stk[i - 1])); a[i].y = stk[i](stk[i - 1]).len2(); a[i + st - 1] = a[i]; } Graham(a2, m); if (st != n) { puts("NO"); return 0; } n--; n *= 2; b[0].x = angle(stk[0](stk[1]), stk[0](stk[st - 2])); b[0].y = stk[0](stk[st - 2]).len2(); for (int i = 1, lim = st - 2; i <= lim; ++i) { b[i].x = angle(stk[i](stk[i + 1]), stk[i](stk[i - 1])); b[i].y = stk[i](stk[i - 1]).len2(); } m = st - 1; getnxt(); puts(pipei() ? "YES" : "NO"); return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double eps = 1e-9; struct hh { double x; long long y; bool operator==(const hh& b) const { return fabs(x - b.x) < eps && y == b.y; } bool operator!=(const hh& b) const { return !((*this) == b); } } a[100010 * 2], b[100010]; int n, m; inline bool eq(int a, int b) { return a == b; } struct point { long long x, y; point() : x(0), y(0) {} point(int a, int b) : x(a), y(b) {} point(point const& f) : x(f.x), y(f.y) {} point operator=(point const& f) { x = f.x; y = f.y; return *this; } long long len2() const { return x * x + y * y; } double len() const { return sqrt(len2()); } point operator+(point const& b) const { return point(x + b.x, y + b.y); } point operator-(point const& b) const { return point(x - b.x, y - b.y); } point operator()(point const& b) const { return b - *this; } }; point operator*(point const& a, double const& b) { return point(a.x * b, a.y * b); } point operator*(double const& b, point const& a) { return point(a.x * b, a.y * b); } long long operator*(point const& a, point const& b) { return (long long)a.x * b.y - (long long)a.y * b.x; } long long operator&(point const& a, point const& b) { return (long long)a.x * b.x + (long long)a.y * b.y; } bool operator==(point const& a, point const& b) { return eq(a.x, b.x) && eq(a.y, b.y); } bool operator>>=(point const& a, point const& b) { return a * b >= 0; } double angle(point const& a, point const& b) { double x = a & b; x /= a.len(); x /= b.len(); return x; } point a1[100010], a2[100010]; point stk[100010]; int st; bool operator<(point const& a, point const& b) { return eq(a.y, b.y) ? a.x < b.x : a.y < b.y; } void Graham(point a[], int n) { sort(a, a + n); st = 0; for (int i = 0; i < n; i++) { while (st > 1 && (stk[st - 2](stk[st - 1]) >>= stk[st - 1](a[i]))) st--; stk[st++] = a[i]; } int p = st; for (int i = n - 2; i >= 0; i--) { while (st > p && (stk[st - 2](stk[st - 1]) >>= stk[st - 1](a[i]))) st--; stk[st++] = a[i]; } } int nxt[100010 * 2]; void getnxt() { int j = -1; nxt[0] = -1; for (int i = 1, lim = m - 1; i <= lim; ++i) { while (j != -1 && b[i] != b[j + 1]) j = nxt[j]; j += b[i] == b[j + 1]; nxt[i] = j; } } bool pipei() { int j = -1; for (int i = 0, lim = n - 1; i <= lim; ++i) { while (j != -1 && a[i] != b[j + 1]) j = nxt[j]; j += a[i] == b[j + 1]; if (j == m - 1) return 1; } return 0; } int main() { scanf("%d", &n), scanf("%d", &m); for (int i = 0, lim = n - 1; i <= lim; ++i) scanf("%d", &a1[i].x), scanf("%d", &a1[i].y); for (int i = 0, lim = m - 1; i <= lim; ++i) scanf("%d", &a2[i].x), scanf("%d", &a2[i].y); Graham(a1, n); n = st; a[0].x = angle(stk[0](stk[1]), stk[0](stk[st - 2])); a[0].y = stk[0](stk[st - 2]).len2(); a[st - 1] = a[0]; for (int i = 1, lim = st - 2; i <= lim; ++i) { a[i].x = angle(stk[i](stk[i + 1]), stk[i](stk[i - 1])); a[i].y = stk[i](stk[i - 1]).len2(); a[i + st - 1] = a[i]; } Graham(a2, m); if (st != n) { puts("NO"); return 0; } n--; n *= 2; b[0].x = angle(stk[0](stk[1]), stk[0](stk[st - 2])); b[0].y = stk[0](stk[st - 2]).len2(); for (int i = 1, lim = st - 2; i <= lim; ++i) { b[i].x = angle(stk[i](stk[i + 1]), stk[i](stk[i - 1])); b[i].y = stk[i](stk[i - 1]).len2(); } m = st - 1; getnxt(); puts(pipei() ? "YES" : "NO"); return 0; } ```
#include <bits/stdc++.h> using namespace std; struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y){}; } p[100010], q[100010]; Point operator-(Point A, Point B) { return Point(A.x - B.x, A.y - B.y); } double operator*(Point A, Point B) { return A.x * B.x + A.y * B.y; } bool operator==(Point A, Point B) { return fabs(A.x - B.x) < 1e-9 && fabs(A.y - B.y) < 1e-9; } bool cmp(Point a, Point b) { return fabs(a.x - b.x) < 1e-9 ? (a.y < b.y) : (a.x < b.x); } double Cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } double Lenght(Point a) { return sqrt(1LL * a.x * a.x + 1LL * a.y * a.y); } double Cos(Point a, Point b) { return a * b / Lenght(a) / Lenght(b); } Point st1[100010], st2[100010]; double len1[100010], len2[100010 * 2]; double rad1[100010], rad2[100010 * 2]; int fail[100010]; void GetFail(int r) { int j = 0, i = 1; fail[0] = -1; while (i < r) { if (j == -1 || (fabs(len1[i] - len1[j]) < 1e-9 && fabs(rad1[i] - rad1[j]) < 1e-9)) { fail[++i] = ++j; } else j = fail[j]; } } bool KMP(int r) { int i = 0, j = 0; while (i < r * 2 && j < r) { if (j == -1 || (fabs(len2[i] - len1[j]) < 1e-9 && fabs(rad2[i] - rad1[j]) < 1e-9)) i++, j++; else j = fail[j]; } if (j == r) return 1; return 0; } int n, m; bool Andrew() { sort(p + 1, p + 1 + n, cmp); sort(q + 1, q + 1 + m, cmp); int r1 = 0; for (int i = 1; i <= n; i++) { while (r1 >= 2 && Cross(st1[r1 - 1] - st1[r1 - 2], p[i] - st1[r1 - 1]) <= 0) r1--; st1[r1++] = p[i]; } int half = r1; for (int i = n - 1; i >= 1; i--) { while (r1 > half && Cross(st1[r1 - 1] - st1[r1 - 2], p[i] - st1[r1 - 2]) <= 0) r1--; st1[r1++] = p[i]; } if (st1[r1 - 1] == st1[0]) r1--; int r2 = 0; for (int i = 1; i <= m; i++) { while (r2 >= 2 && Cross(st2[r2 - 1] - st2[r2 - 2], q[i] - st2[r2 - 1]) <= 0) r2--; st2[r2++] = q[i]; } half = r2; for (int i = m - 1; i >= 1; i--) { while (r2 > half && Cross(st2[r2 - 1] - st2[r2 - 2], q[i] - st2[r2 - 2]) <= 0) r2--; st2[r2++] = q[i]; } if (st2[r2 - 1] == st2[0]) r2--; if (r2 != r1) return 0; for (int i = 0; i < r1; i++) { len1[i] = Lenght(st1[(i + 1) % r1] - st1[i]); rad1[i] = Cos(st1[(i + 1) % r1] - st1[i], st1[(i + 2) % r1] - st1[(i + 1) % r1]); } for (int i = 0; i < r2; i++) { len2[i] = Lenght(st2[(i + 1) % r2] - st2[i]); rad2[i] = Cos(st2[(i + 1) % r2] - st2[i], st2[(i + 2) % r2] - st2[(i + 1) % r2]); } for (int i = 0; i < r2; i++) len2[i + r2] = len2[i], rad2[i + r2] = rad2[i]; GetFail(r1); return KMP(r1); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%lf%lf", &p[i].x, &p[i].y); for (int i = 1; i <= m; i++) scanf("%lf%lf", &q[i].x, &q[i].y); if (Andrew()) printf("YES\n"); else printf("NO\n"); }
### Prompt Please formulate a Cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y){}; } p[100010], q[100010]; Point operator-(Point A, Point B) { return Point(A.x - B.x, A.y - B.y); } double operator*(Point A, Point B) { return A.x * B.x + A.y * B.y; } bool operator==(Point A, Point B) { return fabs(A.x - B.x) < 1e-9 && fabs(A.y - B.y) < 1e-9; } bool cmp(Point a, Point b) { return fabs(a.x - b.x) < 1e-9 ? (a.y < b.y) : (a.x < b.x); } double Cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } double Lenght(Point a) { return sqrt(1LL * a.x * a.x + 1LL * a.y * a.y); } double Cos(Point a, Point b) { return a * b / Lenght(a) / Lenght(b); } Point st1[100010], st2[100010]; double len1[100010], len2[100010 * 2]; double rad1[100010], rad2[100010 * 2]; int fail[100010]; void GetFail(int r) { int j = 0, i = 1; fail[0] = -1; while (i < r) { if (j == -1 || (fabs(len1[i] - len1[j]) < 1e-9 && fabs(rad1[i] - rad1[j]) < 1e-9)) { fail[++i] = ++j; } else j = fail[j]; } } bool KMP(int r) { int i = 0, j = 0; while (i < r * 2 && j < r) { if (j == -1 || (fabs(len2[i] - len1[j]) < 1e-9 && fabs(rad2[i] - rad1[j]) < 1e-9)) i++, j++; else j = fail[j]; } if (j == r) return 1; return 0; } int n, m; bool Andrew() { sort(p + 1, p + 1 + n, cmp); sort(q + 1, q + 1 + m, cmp); int r1 = 0; for (int i = 1; i <= n; i++) { while (r1 >= 2 && Cross(st1[r1 - 1] - st1[r1 - 2], p[i] - st1[r1 - 1]) <= 0) r1--; st1[r1++] = p[i]; } int half = r1; for (int i = n - 1; i >= 1; i--) { while (r1 > half && Cross(st1[r1 - 1] - st1[r1 - 2], p[i] - st1[r1 - 2]) <= 0) r1--; st1[r1++] = p[i]; } if (st1[r1 - 1] == st1[0]) r1--; int r2 = 0; for (int i = 1; i <= m; i++) { while (r2 >= 2 && Cross(st2[r2 - 1] - st2[r2 - 2], q[i] - st2[r2 - 1]) <= 0) r2--; st2[r2++] = q[i]; } half = r2; for (int i = m - 1; i >= 1; i--) { while (r2 > half && Cross(st2[r2 - 1] - st2[r2 - 2], q[i] - st2[r2 - 2]) <= 0) r2--; st2[r2++] = q[i]; } if (st2[r2 - 1] == st2[0]) r2--; if (r2 != r1) return 0; for (int i = 0; i < r1; i++) { len1[i] = Lenght(st1[(i + 1) % r1] - st1[i]); rad1[i] = Cos(st1[(i + 1) % r1] - st1[i], st1[(i + 2) % r1] - st1[(i + 1) % r1]); } for (int i = 0; i < r2; i++) { len2[i] = Lenght(st2[(i + 1) % r2] - st2[i]); rad2[i] = Cos(st2[(i + 1) % r2] - st2[i], st2[(i + 2) % r2] - st2[(i + 1) % r2]); } for (int i = 0; i < r2; i++) len2[i + r2] = len2[i], rad2[i + r2] = rad2[i]; GetFail(r1); return KMP(r1); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%lf%lf", &p[i].x, &p[i].y); for (int i = 1; i <= m; i++) scanf("%lf%lf", &q[i].x, &q[i].y); if (Andrew()) printf("YES\n"); else printf("NO\n"); } ```
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; int n, m, top; pair<int, int> c[MAXN]; pair<int, int> stk[MAXN]; long long MUL[MAXN << 2], MOD; long long H[MAXN << 2]; long long mul[4] = {5767169, 23068673, 167772161, 1000000007}; long long prime[4] = {7340033, 104857601, 469762049, 1004535809}; vector<long long> ret1, ret2; long long area(const pair<int, int> &a, const pair<int, int> &b, const pair<int, int> &c) { return (long long)a.first * (b.second - c.second) + (long long)b.first * (c.second - a.second) + (long long)c.first * (a.second - b.second); } long long dist(const pair<int, int> &a, const pair<int, int> &b) { return (long long)(a.first - b.first) * (a.first - b.first) + (long long)(a.second - b.second) * (a.second - b.second); } bool cmp(const pair<int, int> &a, const pair<int, int> &b) { if (area(a, c[1], b) == 0) return dist(a, c[1]) < dist(b, c[1]); return area(a, c[1], b) < 0; } void gethull(vector<long long> &p, int l) { for (int i = 2; i <= l; i++) if (c[i].second < c[1].second || (c[i].second == c[1].second && c[i].first < c[1].first)) swap(c[1], c[i]); sort(c + 2, c + l + 1, cmp); top = 0; stk[++top] = c[1]; stk[++top] = c[2]; for (int i = 3; i <= l; i++) { while (top >= 2 && area(stk[top - 1], stk[top], c[i]) <= 0) top--; stk[++top] = c[i]; } for (int i = 1; i <= top; i++) { p.push_back(dist(stk[i], stk[i % top + 1])); p.push_back(dist(stk[i % top + 1], stk[(i + 1) % top + 1])); p.push_back(dist(stk[i], stk[(i + 1) % top + 1])); } } long long getHash(int l, int r) { return ((H[r] - H[l - 1] * MUL[r - l + 1]) % MOD + MOD) % MOD; } bool ok() { long long HH = 0; for (int i = 1; i <= ret1.size(); i++) HH = (HH * MUL[1] + ret1[i - 1]) % MOD; for (int i = 1; i <= ret2.size(); i++) H[i] = (H[i - 1] * MUL[1] + ret2[i - 1]) % MOD; for (int i = 1; i <= ret2.size(); i++) { long long HH_ = (getHash(i, ret2.size()) * MUL[i - 1] + getHash(1, i - 1)) % MOD; if (HH_ == HH) return true; } return false; } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> c[i].first >> c[i].second; gethull(ret1, n); for (int i = 1; i <= m; i++) cin >> c[i].first >> c[i].second; gethull(ret2, m); if (ret1.size() != ret2.size()) { cout << "NO" << endl; return 0; } MUL[0] = 1; for (int i = 0; i < 4; i++) { MUL[1] = mul[i]; MOD = prime[i]; for (int j = 2; j <= ret1.size(); j++) MUL[j] = MUL[j - 1] * MUL[1] % MOD; if (!ok()) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 100005; int n, m, top; pair<int, int> c[MAXN]; pair<int, int> stk[MAXN]; long long MUL[MAXN << 2], MOD; long long H[MAXN << 2]; long long mul[4] = {5767169, 23068673, 167772161, 1000000007}; long long prime[4] = {7340033, 104857601, 469762049, 1004535809}; vector<long long> ret1, ret2; long long area(const pair<int, int> &a, const pair<int, int> &b, const pair<int, int> &c) { return (long long)a.first * (b.second - c.second) + (long long)b.first * (c.second - a.second) + (long long)c.first * (a.second - b.second); } long long dist(const pair<int, int> &a, const pair<int, int> &b) { return (long long)(a.first - b.first) * (a.first - b.first) + (long long)(a.second - b.second) * (a.second - b.second); } bool cmp(const pair<int, int> &a, const pair<int, int> &b) { if (area(a, c[1], b) == 0) return dist(a, c[1]) < dist(b, c[1]); return area(a, c[1], b) < 0; } void gethull(vector<long long> &p, int l) { for (int i = 2; i <= l; i++) if (c[i].second < c[1].second || (c[i].second == c[1].second && c[i].first < c[1].first)) swap(c[1], c[i]); sort(c + 2, c + l + 1, cmp); top = 0; stk[++top] = c[1]; stk[++top] = c[2]; for (int i = 3; i <= l; i++) { while (top >= 2 && area(stk[top - 1], stk[top], c[i]) <= 0) top--; stk[++top] = c[i]; } for (int i = 1; i <= top; i++) { p.push_back(dist(stk[i], stk[i % top + 1])); p.push_back(dist(stk[i % top + 1], stk[(i + 1) % top + 1])); p.push_back(dist(stk[i], stk[(i + 1) % top + 1])); } } long long getHash(int l, int r) { return ((H[r] - H[l - 1] * MUL[r - l + 1]) % MOD + MOD) % MOD; } bool ok() { long long HH = 0; for (int i = 1; i <= ret1.size(); i++) HH = (HH * MUL[1] + ret1[i - 1]) % MOD; for (int i = 1; i <= ret2.size(); i++) H[i] = (H[i - 1] * MUL[1] + ret2[i - 1]) % MOD; for (int i = 1; i <= ret2.size(); i++) { long long HH_ = (getHash(i, ret2.size()) * MUL[i - 1] + getHash(1, i - 1)) % MOD; if (HH_ == HH) return true; } return false; } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> c[i].first >> c[i].second; gethull(ret1, n); for (int i = 1; i <= m; i++) cin >> c[i].first >> c[i].second; gethull(ret2, m); if (ret1.size() != ret2.size()) { cout << "NO" << endl; return 0; } MUL[0] = 1; for (int i = 0; i < 4; i++) { MUL[1] = mul[i]; MOD = prime[i]; for (int j = 2; j <= ret1.size(); j++) MUL[j] = MUL[j - 1] * MUL[1] % MOD; if (!ok()) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int N = 400005; long long read() { long long x = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - 48, ch = getchar(); return x; } int n, m; struct Point { int x, y; } p1[N], p2[N], O; long long sqr(int x) { return 1LL * x * x; } long long dis(Point a, Point b) { return sqr(a.x - b.x) + sqr(a.y - b.y); } long long cross(Point a, Point b, Point c) { return 1LL * (b.x - a.x) * (c.y - a.y) - 1LL * (c.x - a.x) * (b.y - a.y); } bool cmp_O(Point a, Point b) { if (a.y == b.y) return a.x < b.x; return a.y < b.y; } bool cmp_Angle(Point a, Point b) { long long c = cross(O, a, b); if (c == 0) return dis(O, a) < dis(O, b); return c > 0; } int st[N], top; int Make_Convex(Point P[], int n) { for (int i = 2; i <= n; i++) if (!cmp_O(P[1], P[i])) swap(P[1], P[i]); O = P[1]; sort(P + 2, P + n + 1, cmp_Angle); top = 0; st[++top] = 1, st[++top] = 2; for (int i = 3; i <= n; i++) { while (top >= 2 && cross(P[st[top - 1]], P[st[top]], P[i]) <= 0) top--; st[++top] = i; } for (int i = 1; i <= top; i++) P[i] = P[st[i]]; return top; } long long s1[N], s2[N]; int Fail[N]; void KMP(long long s[], int n) { Fail[0] = Fail[1] = 0; for (int i = 2; i <= n; i++) { int k = Fail[i - 1]; while (k > 0 && s[i] != s[k + 1]) k = Fail[k]; Fail[i] = k + (s[k + 1] == s[i] ? 1 : 0); } } bool check() { int k = 0; for (int i = 1; i <= n * 4; i++) { while (k > 0 && s2[k + 1] != s1[i]) k = Fail[k]; if (s2[k + 1] == s1[i]) k++; if (k >= m * 2) return 1; } return 0; } int main() { n = read(), m = read(); for (int i = 1; i <= n; i++) p1[i].x = read(), p1[i].y = read(); for (int i = 1; i <= m; i++) p2[i].x = read(), p2[i].y = read(); n = Make_Convex(p1, n); m = Make_Convex(p2, m); for (int i = 1; i <= n; i++) { s1[i * 2 - 1] = dis(p1[i], p1[i % n + 1]); s1[i * 2] = cross(p1[i], p1[i % n + 1], p1[(i + 1) % n + 1]); } for (int i = 1; i <= m; i++) { s2[i * 2 - 1] = dis(p2[i], p2[i % n + 1]); s2[i * 2] = cross(p2[i], p2[i % n + 1], p2[(i + 1) % n + 1]); } for (int i = 1; i <= n * 2; i++) s1[i + n * 2] = s1[i]; KMP(s2, m * 2); puts(n == m && check() ? "YES" : "NO"); return 0; }
### Prompt Please create a solution in Cpp to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 400005; long long read() { long long x = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - 48, ch = getchar(); return x; } int n, m; struct Point { int x, y; } p1[N], p2[N], O; long long sqr(int x) { return 1LL * x * x; } long long dis(Point a, Point b) { return sqr(a.x - b.x) + sqr(a.y - b.y); } long long cross(Point a, Point b, Point c) { return 1LL * (b.x - a.x) * (c.y - a.y) - 1LL * (c.x - a.x) * (b.y - a.y); } bool cmp_O(Point a, Point b) { if (a.y == b.y) return a.x < b.x; return a.y < b.y; } bool cmp_Angle(Point a, Point b) { long long c = cross(O, a, b); if (c == 0) return dis(O, a) < dis(O, b); return c > 0; } int st[N], top; int Make_Convex(Point P[], int n) { for (int i = 2; i <= n; i++) if (!cmp_O(P[1], P[i])) swap(P[1], P[i]); O = P[1]; sort(P + 2, P + n + 1, cmp_Angle); top = 0; st[++top] = 1, st[++top] = 2; for (int i = 3; i <= n; i++) { while (top >= 2 && cross(P[st[top - 1]], P[st[top]], P[i]) <= 0) top--; st[++top] = i; } for (int i = 1; i <= top; i++) P[i] = P[st[i]]; return top; } long long s1[N], s2[N]; int Fail[N]; void KMP(long long s[], int n) { Fail[0] = Fail[1] = 0; for (int i = 2; i <= n; i++) { int k = Fail[i - 1]; while (k > 0 && s[i] != s[k + 1]) k = Fail[k]; Fail[i] = k + (s[k + 1] == s[i] ? 1 : 0); } } bool check() { int k = 0; for (int i = 1; i <= n * 4; i++) { while (k > 0 && s2[k + 1] != s1[i]) k = Fail[k]; if (s2[k + 1] == s1[i]) k++; if (k >= m * 2) return 1; } return 0; } int main() { n = read(), m = read(); for (int i = 1; i <= n; i++) p1[i].x = read(), p1[i].y = read(); for (int i = 1; i <= m; i++) p2[i].x = read(), p2[i].y = read(); n = Make_Convex(p1, n); m = Make_Convex(p2, m); for (int i = 1; i <= n; i++) { s1[i * 2 - 1] = dis(p1[i], p1[i % n + 1]); s1[i * 2] = cross(p1[i], p1[i % n + 1], p1[(i + 1) % n + 1]); } for (int i = 1; i <= m; i++) { s2[i * 2 - 1] = dis(p2[i], p2[i % n + 1]); s2[i * 2] = cross(p2[i], p2[i % n + 1], p2[(i + 1) % n + 1]); } for (int i = 1; i <= n * 2; i++) s1[i + n * 2] = s1[i]; KMP(s2, m * 2); puts(n == m && check() ? "YES" : "NO"); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int max_n = 100111, inf = 1000111222; struct point { int x, y; point() { x = y = 0; } point(int x, int y) : x(x), y(y) {} void read() { scanf("%d%d", &x, &y); } void write(string s = "") { printf("%d %d%s", x, y, s.c_str()); } void write_point(string s = "") { printf("(%d %d)%s", x, y, s.c_str()); } bool operator<(const point &a) const { return x < a.x || x == a.x && y < a.y; } point operator-(const point &a) const { return point(x - a.x, y - a.y); } }; long long dot_pr(point a, point b) { return 1LL * a.x * b.x + 1LL * a.y * b.y; } long long vect_pr(point a, point b) { return 1LL * a.x * b.y - 1LL * a.y * b.x; } bool cv(point a, point b, point c) { return vect_pr(b - a, c - b) < 0; } bool ccv(point a, point b, point c) { return vect_pr(b - a, c - b) > 0; } vector<point> convex_hull(int n, point p[]) { sort(p, p + n); vector<point> res, up, down; if (n == 1) { res.push_back(p[0]); res.push_back(p[0]); return res; } for (int i = 0; i < n; ++i) { if (cv(p[0], p[i], p[n - 1]) || i == 0 || i + 1 == n) { while (up.size() > 1 && !cv(up[up.size() - 2], up[up.size() - 1], p[i])) { up.pop_back(); } up.push_back(p[i]); } } for (int i = 0; i < n; ++i) { if (ccv(p[0], p[i], p[n - 1]) || i == 0 || i + 1 == n) { while (down.size() > 1 && !ccv(down[down.size() - 2], down[down.size() - 1], p[i])) { down.pop_back(); } down.push_back(p[i]); } } down.pop_back(); reverse(down.begin(), down.end()); down.pop_back(); res = up; for (int i = 0; i < down.size(); ++i) { res.push_back(down[i]); } return res; } int n[2]; point p[max_n]; vector<point> v[2]; vector<pair<long long, pair<long long, long long>>> a, b; long long dist(const point &p) { return 1LL * p.x * p.x + 1LL * p.y * p.y; } vector<pair<long long, pair<long long, long long>>> get( const vector<point> &v) { vector<pair<long long, pair<long long, long long>>> res; for (int i = 0; i < v.size(); ++i) { res.push_back({dist(v[i] - v[(i + 1) % v.size()]), {vect_pr(v[(i + 2) % v.size()] - v[(i + 1) % v.size()], v[(i + 1) % v.size()] - v[i]), dot_pr(v[(i + 2) % v.size()] - v[(i + 1) % v.size()], v[(i + 1) % v.size()] - v[i])}}); } return res; } int pr[3 * max_n]; void get_prefix_function( const vector<pair<long long, pair<long long, long long>>> &s, int p[]) { p[0] = 0; for (int i = 1; i < s.size(); ++i) { p[i] = p[i - 1]; while (p[i] > 0 && s[i] != s[p[i]]) { p[i] = p[p[i] - 1]; } if (s[i] == s[p[i]]) { ++p[i]; } } } int main() { scanf("%d%d", &n[0], &n[1]); for (int i = 0; i < 2; ++i) { for (int j = 0; j < n[i]; ++j) { scanf("%d%d", &p[j].x, &p[j].y); } v[i] = convex_hull(n[i], p); } if (v[0].size() != v[1].size()) { puts("NO"); return 0; } a = get(v[0]); b = get(v[1]); a.push_back({-1, {-1, -1}}); for (int i = 0; i < 2 * b.size(); ++i) { a.push_back(b[i % b.size()]); } get_prefix_function(a, pr); for (int i = b.size() + 1; i < a.size(); ++i) { if (pr[i] == b.size()) { puts("YES"); return 0; } } puts("NO"); return 0; }
### Prompt Please formulate a Cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int max_n = 100111, inf = 1000111222; struct point { int x, y; point() { x = y = 0; } point(int x, int y) : x(x), y(y) {} void read() { scanf("%d%d", &x, &y); } void write(string s = "") { printf("%d %d%s", x, y, s.c_str()); } void write_point(string s = "") { printf("(%d %d)%s", x, y, s.c_str()); } bool operator<(const point &a) const { return x < a.x || x == a.x && y < a.y; } point operator-(const point &a) const { return point(x - a.x, y - a.y); } }; long long dot_pr(point a, point b) { return 1LL * a.x * b.x + 1LL * a.y * b.y; } long long vect_pr(point a, point b) { return 1LL * a.x * b.y - 1LL * a.y * b.x; } bool cv(point a, point b, point c) { return vect_pr(b - a, c - b) < 0; } bool ccv(point a, point b, point c) { return vect_pr(b - a, c - b) > 0; } vector<point> convex_hull(int n, point p[]) { sort(p, p + n); vector<point> res, up, down; if (n == 1) { res.push_back(p[0]); res.push_back(p[0]); return res; } for (int i = 0; i < n; ++i) { if (cv(p[0], p[i], p[n - 1]) || i == 0 || i + 1 == n) { while (up.size() > 1 && !cv(up[up.size() - 2], up[up.size() - 1], p[i])) { up.pop_back(); } up.push_back(p[i]); } } for (int i = 0; i < n; ++i) { if (ccv(p[0], p[i], p[n - 1]) || i == 0 || i + 1 == n) { while (down.size() > 1 && !ccv(down[down.size() - 2], down[down.size() - 1], p[i])) { down.pop_back(); } down.push_back(p[i]); } } down.pop_back(); reverse(down.begin(), down.end()); down.pop_back(); res = up; for (int i = 0; i < down.size(); ++i) { res.push_back(down[i]); } return res; } int n[2]; point p[max_n]; vector<point> v[2]; vector<pair<long long, pair<long long, long long>>> a, b; long long dist(const point &p) { return 1LL * p.x * p.x + 1LL * p.y * p.y; } vector<pair<long long, pair<long long, long long>>> get( const vector<point> &v) { vector<pair<long long, pair<long long, long long>>> res; for (int i = 0; i < v.size(); ++i) { res.push_back({dist(v[i] - v[(i + 1) % v.size()]), {vect_pr(v[(i + 2) % v.size()] - v[(i + 1) % v.size()], v[(i + 1) % v.size()] - v[i]), dot_pr(v[(i + 2) % v.size()] - v[(i + 1) % v.size()], v[(i + 1) % v.size()] - v[i])}}); } return res; } int pr[3 * max_n]; void get_prefix_function( const vector<pair<long long, pair<long long, long long>>> &s, int p[]) { p[0] = 0; for (int i = 1; i < s.size(); ++i) { p[i] = p[i - 1]; while (p[i] > 0 && s[i] != s[p[i]]) { p[i] = p[p[i] - 1]; } if (s[i] == s[p[i]]) { ++p[i]; } } } int main() { scanf("%d%d", &n[0], &n[1]); for (int i = 0; i < 2; ++i) { for (int j = 0; j < n[i]; ++j) { scanf("%d%d", &p[j].x, &p[j].y); } v[i] = convex_hull(n[i], p); } if (v[0].size() != v[1].size()) { puts("NO"); return 0; } a = get(v[0]); b = get(v[1]); a.push_back({-1, {-1, -1}}); for (int i = 0; i < 2 * b.size(); ++i) { a.push_back(b[i % b.size()]); } get_prefix_function(a, pr); for (int i = b.size() + 1; i < a.size(); ++i) { if (pr[i] == b.size()) { puts("YES"); return 0; } } puts("NO"); return 0; } ```
#include <bits/stdc++.h> using namespace std; struct pt { long long x, y; }; long long S(pt a, pt b, pt c) { return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y); } bool cmp(pt a, pt b) { return a.x < b.x || a.x == b.x && a.y < b.y; } bool cw(pt a, pt b, pt c) { return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y) < 0; } bool ccw(pt a, pt b, pt c) { return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y) > 0; } void convex_hull(vector<pt> &a) { if (a.size() == 1) return; sort(a.begin(), a.end(), &cmp); pt p1 = a[0], p2 = a.back(); vector<pt> up, down; up.push_back(p1); down.push_back(p1); for (size_t i = 1; i < a.size(); ++i) { if (i == a.size() - 1 || cw(p1, a[i], p2)) { while (up.size() >= 2 && !cw(up[up.size() - 2], up[up.size() - 1], a[i])) up.pop_back(); up.push_back(a[i]); } if (i == a.size() - 1 || ccw(p1, a[i], p2)) { while (down.size() >= 2 && !ccw(down[down.size() - 2], down[down.size() - 1], a[i])) down.pop_back(); down.push_back(a[i]); } } a.clear(); for (size_t i = 0; i < up.size(); ++i) a.push_back(up[i]); for (size_t i = down.size() - 2; i > 0; --i) a.push_back(down[i]); } void pf(const vector<long long> &s, int need) { int n = s.size(); vector<int> pi(n); for (int i = 1; i < n; i++) { int j = pi[i - 1]; while (j > 0 && s[i] != s[j]) j = pi[j - 1]; if (s[i] == s[j]) j++; pi[i] = j; if (j == need) { cout << "YES" << endl; exit(0); } } } int n, m; vector<pt> a; vector<pt> b; int main() { ios_base::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; a.push_back({x, y}); } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; b.push_back({x, y}); } convex_hull(b); convex_hull(a); if (a.size() != b.size()) { cout << "NO" << endl; exit(0); } vector<long long> s, t; int ka = a.size(); for (int i = 0; i < ka; i++) { int j = (i + 1) % ka; s.push_back((a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y)); int k = (i + 2) % ka; s.push_back(S(a[i], a[j], a[k])); } swap(a, b); for (int i = 0; i < ka; i++) { int j = (i + 1) % ka; t.push_back((a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y)); int k = (i + 2) % ka; t.push_back(S(a[i], a[j], a[k])); } int sz = s.size(); for (int i = 0; i < sz; i++) s.push_back(s[i]); t.push_back(1e18); for (int i = 0; i < s.size(); i++) t.push_back(s[i]); pf(t, 2 * ka); cout << "NO" << endl; }
### Prompt Please formulate a cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct pt { long long x, y; }; long long S(pt a, pt b, pt c) { return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y); } bool cmp(pt a, pt b) { return a.x < b.x || a.x == b.x && a.y < b.y; } bool cw(pt a, pt b, pt c) { return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y) < 0; } bool ccw(pt a, pt b, pt c) { return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y) > 0; } void convex_hull(vector<pt> &a) { if (a.size() == 1) return; sort(a.begin(), a.end(), &cmp); pt p1 = a[0], p2 = a.back(); vector<pt> up, down; up.push_back(p1); down.push_back(p1); for (size_t i = 1; i < a.size(); ++i) { if (i == a.size() - 1 || cw(p1, a[i], p2)) { while (up.size() >= 2 && !cw(up[up.size() - 2], up[up.size() - 1], a[i])) up.pop_back(); up.push_back(a[i]); } if (i == a.size() - 1 || ccw(p1, a[i], p2)) { while (down.size() >= 2 && !ccw(down[down.size() - 2], down[down.size() - 1], a[i])) down.pop_back(); down.push_back(a[i]); } } a.clear(); for (size_t i = 0; i < up.size(); ++i) a.push_back(up[i]); for (size_t i = down.size() - 2; i > 0; --i) a.push_back(down[i]); } void pf(const vector<long long> &s, int need) { int n = s.size(); vector<int> pi(n); for (int i = 1; i < n; i++) { int j = pi[i - 1]; while (j > 0 && s[i] != s[j]) j = pi[j - 1]; if (s[i] == s[j]) j++; pi[i] = j; if (j == need) { cout << "YES" << endl; exit(0); } } } int n, m; vector<pt> a; vector<pt> b; int main() { ios_base::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; a.push_back({x, y}); } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; b.push_back({x, y}); } convex_hull(b); convex_hull(a); if (a.size() != b.size()) { cout << "NO" << endl; exit(0); } vector<long long> s, t; int ka = a.size(); for (int i = 0; i < ka; i++) { int j = (i + 1) % ka; s.push_back((a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y)); int k = (i + 2) % ka; s.push_back(S(a[i], a[j], a[k])); } swap(a, b); for (int i = 0; i < ka; i++) { int j = (i + 1) % ka; t.push_back((a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y)); int k = (i + 2) % ka; t.push_back(S(a[i], a[j], a[k])); } int sz = s.size(); for (int i = 0; i < sz; i++) s.push_back(s[i]); t.push_back(1e18); for (int i = 0; i < s.size(); i++) t.push_back(s[i]); pf(t, 2 * ka); cout << "NO" << endl; } ```
#include <bits/stdc++.h> using namespace std; struct Pt { long long x, y; Pt() {} Pt(long long _x, long long _y) : x(_x), y(_y) {} Pt operator+(const Pt &p) const { return Pt(x + p.x, y + p.y); } Pt operator-(const Pt &p) const { return Pt(x - p.x, y - p.y); } Pt operator*(long long c) const { return Pt(x * c, y * c); } Pt operator/(long long c) const { return Pt(x / c, y / c); } long long abs2() { return x * x + y * y; } }; inline long long dist2(Pt a, Pt b) { long long dx = a.x - b.x; long long dy = a.y - b.y; return dx * dx + dy * dy; } inline long long cross(Pt a, Pt b) { return a.x * b.y - a.y * b.x; } long long dot(Pt a, Pt b) { return a.x * b.x + a.y * b.y; } inline int ccw(Pt p, Pt q, Pt r) { long long v = cross(p - q, p - r); if (v < 0) return -1; if (v > 0) return 1; return 0; } vector<Pt> convexHull(vector<Pt> &P) { sort(P.begin(), P.end(), [](const Pt &a, const Pt &b) { if (a.x != b.x) return a.x < b.x; return a.y < b.y; }); vector<Pt> up, dn, hull; for (int i = 0; i < P.size(); ++i) { while (up.size() > 1 && ccw(up[up.size() - 2], up.back(), P[i]) >= 0) up.pop_back(); while (dn.size() > 1 && ccw(dn[dn.size() - 2], dn.back(), P[i]) <= 0) dn.pop_back(); up.push_back(P[i]); dn.push_back(P[i]); } hull = up; for (int i = (int)dn.size() - 2; i > 0; --i) hull.push_back(dn[i]); reverse(hull.begin(), hull.end()); return hull; } vector<long long> getdata(int n) { vector<Pt> A(n); for (int i = 0; i < n; ++i) { scanf("%lld%lld", &A[i].x, &A[i].y); } A = convexHull(A); if (A.size() == 2) return {(A[1] - A[0]).abs2()}; n = A.size(); vector<long long> data; for (int i = 0; i < n; ++i) { int a = (i + n - 1) % n, b = (i + 1) % n; Pt aa = A[a] - A[i], bb = A[b] - A[i]; long long ang = dot(aa, bb); data.push_back(aa.abs2()); data.push_back((1LL << 60) + ang); } return data; } void no() { printf("NO\n"); exit(0); } void yes() { printf("YES\n"); exit(0); } vector<int> zalgo(vector<long long> S) { int n = S.size(), l = 0, r = 0; vector<int> Z(n); Z[0] = n; for (int i = 1; i < n; ++i) { if (i > r) { l = r = i; while (r < n && S[r - l] == S[r]) ++r; Z[i] = r - l; --r; } else { if (Z[i - l] <= r - i) Z[i] = Z[i - l]; else { l = i; while (r < n && S[r - l] == S[r]) ++r; Z[i] = r - l; --r; } } } return Z; } bool roteq(vector<long long> a, vector<long long> b) { if (a.size() != b.size()) return false; vector<long long> s = a; s.insert(s.end(), b.begin(), b.end()); s.insert(s.end(), b.begin(), b.end()); auto z = zalgo(s); for (int i = a.size(); i < s.size(); ++i) { if (z[i] >= a.size()) return true; } return false; } int main() { int n, m; scanf("%d%d", &n, &m); auto a = getdata(n); auto b = getdata(m); if (a.size() > b.size()) swap(a, b); if (a.size() == 1) { if (b.size() > 1) no(); if (a[0] != b[0]) no(); yes(); } if (roteq(a, b)) yes(); else no(); }
### Prompt Construct a cpp code solution to the problem outlined: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct Pt { long long x, y; Pt() {} Pt(long long _x, long long _y) : x(_x), y(_y) {} Pt operator+(const Pt &p) const { return Pt(x + p.x, y + p.y); } Pt operator-(const Pt &p) const { return Pt(x - p.x, y - p.y); } Pt operator*(long long c) const { return Pt(x * c, y * c); } Pt operator/(long long c) const { return Pt(x / c, y / c); } long long abs2() { return x * x + y * y; } }; inline long long dist2(Pt a, Pt b) { long long dx = a.x - b.x; long long dy = a.y - b.y; return dx * dx + dy * dy; } inline long long cross(Pt a, Pt b) { return a.x * b.y - a.y * b.x; } long long dot(Pt a, Pt b) { return a.x * b.x + a.y * b.y; } inline int ccw(Pt p, Pt q, Pt r) { long long v = cross(p - q, p - r); if (v < 0) return -1; if (v > 0) return 1; return 0; } vector<Pt> convexHull(vector<Pt> &P) { sort(P.begin(), P.end(), [](const Pt &a, const Pt &b) { if (a.x != b.x) return a.x < b.x; return a.y < b.y; }); vector<Pt> up, dn, hull; for (int i = 0; i < P.size(); ++i) { while (up.size() > 1 && ccw(up[up.size() - 2], up.back(), P[i]) >= 0) up.pop_back(); while (dn.size() > 1 && ccw(dn[dn.size() - 2], dn.back(), P[i]) <= 0) dn.pop_back(); up.push_back(P[i]); dn.push_back(P[i]); } hull = up; for (int i = (int)dn.size() - 2; i > 0; --i) hull.push_back(dn[i]); reverse(hull.begin(), hull.end()); return hull; } vector<long long> getdata(int n) { vector<Pt> A(n); for (int i = 0; i < n; ++i) { scanf("%lld%lld", &A[i].x, &A[i].y); } A = convexHull(A); if (A.size() == 2) return {(A[1] - A[0]).abs2()}; n = A.size(); vector<long long> data; for (int i = 0; i < n; ++i) { int a = (i + n - 1) % n, b = (i + 1) % n; Pt aa = A[a] - A[i], bb = A[b] - A[i]; long long ang = dot(aa, bb); data.push_back(aa.abs2()); data.push_back((1LL << 60) + ang); } return data; } void no() { printf("NO\n"); exit(0); } void yes() { printf("YES\n"); exit(0); } vector<int> zalgo(vector<long long> S) { int n = S.size(), l = 0, r = 0; vector<int> Z(n); Z[0] = n; for (int i = 1; i < n; ++i) { if (i > r) { l = r = i; while (r < n && S[r - l] == S[r]) ++r; Z[i] = r - l; --r; } else { if (Z[i - l] <= r - i) Z[i] = Z[i - l]; else { l = i; while (r < n && S[r - l] == S[r]) ++r; Z[i] = r - l; --r; } } } return Z; } bool roteq(vector<long long> a, vector<long long> b) { if (a.size() != b.size()) return false; vector<long long> s = a; s.insert(s.end(), b.begin(), b.end()); s.insert(s.end(), b.begin(), b.end()); auto z = zalgo(s); for (int i = a.size(); i < s.size(); ++i) { if (z[i] >= a.size()) return true; } return false; } int main() { int n, m; scanf("%d%d", &n, &m); auto a = getdata(n); auto b = getdata(m); if (a.size() > b.size()) swap(a, b); if (a.size() == 1) { if (b.size() > 1) no(); if (a[0] != b[0]) no(); yes(); } if (roteq(a, b)) yes(); else no(); } ```
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const long double eps = 1e-12; long double crossp(complex<long double> a, complex<long double> b) { return (conj(a) * b).imag(); } long double dotp(complex<long double> a, complex<long double> b) { return (conj(a) * b).real(); } complex<long double> _p; bool comp(complex<long double> a, complex<long double> b) { long double arg1 = arg(a - _p); long double arg2 = arg(b - _p); if (arg1 == arg2) return abs(a - _p) < abs(b - _p); return arg1 < arg2; } vector<complex<long double>> convexhull(vector<complex<long double>> v) { _p = v[0]; for (const complex<long double> p2 : v) if (p2.imag() < _p.imag() || (p2.imag() == _p.imag() && p2.real() < _p.real())) _p = p2; vector<complex<long double>> v2; for (const complex<long double> p2 : v) if (p2 != _p) v2.push_back(p2); sort((v2).begin(), (v2).end(), comp); vector<complex<long double>> hull(1, _p); for (const complex<long double> p2 : v2) { while (((int)(hull).size()) >= 2 && crossp(hull.back() - hull[((int)(hull).size()) - 2], p2 - hull.back()) <= 0) hull.pop_back(); hull.push_back(p2); } return hull; } vector<pair<long double, long double>> getpattern(int n) { vector<complex<long double>> v; for (int i = 0; i < (n); i++) { long long x, y; cin >> x >> y; v.push_back(complex<long double>(x, y)); } vector<complex<long double>> hull = convexhull(v); vector<pair<long double, long double>> res; for (int i = 0; i < (((int)(hull).size())); i++) { complex<long double> p = hull[i]; complex<long double> prev = hull[(i + ((int)(hull).size()) - 1) % ((int)(hull).size())]; complex<long double> nxt = hull[(i + 1) % ((int)(hull).size())]; long double angle = arg(nxt - p) - arg(p - prev); if (angle < 0) angle += 2 * acos(-1); res.push_back(make_pair(angle, abs(nxt - p))); } return res; } bool operator==(const pair<long double, long double>& a, const pair<long double, long double>& b) { return abs(a.first - b.first) < eps && abs(a.second - b.second) < eps; } bool operator!=(const pair<long double, long double>& a, const pair<long double, long double>& b) { return !(a == b); } vector<pair<pair<long double, long double>, int>> dfa; void gendfa(vector<pair<long double, long double>> v) { dfa.push_back(make_pair(v[0], 0)); dfa.push_back(make_pair(v[1], 0)); for (int i = (2); i < (((int)(v).size())); i++) { int lps = dfa[i - 1].second; while (lps > 0 && dfa[lps].first != v[i - 1]) lps = dfa[lps].second; if (dfa[lps].first == v[i - 1]) lps++; dfa.push_back(make_pair(v[i], lps)); } } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<pair<long double, long double>> v1 = getpattern(n); vector<pair<long double, long double>> v2 = getpattern(m); gendfa(v1); int curr = 0; for (int i = 0; i < (2 * ((int)(v2).size())); i++) { while (curr > 0 && v2[i % ((int)(v2).size())] != dfa[curr].first) curr = dfa[curr].second; if (v2[i % ((int)(v2).size())] == dfa[curr].first) curr++; if (curr == ((int)(v1).size())) { cout << "Yes" << endl; return 0; } } cout << "No" << endl; }
### Prompt Your challenge is to write a CPP solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const long double eps = 1e-12; long double crossp(complex<long double> a, complex<long double> b) { return (conj(a) * b).imag(); } long double dotp(complex<long double> a, complex<long double> b) { return (conj(a) * b).real(); } complex<long double> _p; bool comp(complex<long double> a, complex<long double> b) { long double arg1 = arg(a - _p); long double arg2 = arg(b - _p); if (arg1 == arg2) return abs(a - _p) < abs(b - _p); return arg1 < arg2; } vector<complex<long double>> convexhull(vector<complex<long double>> v) { _p = v[0]; for (const complex<long double> p2 : v) if (p2.imag() < _p.imag() || (p2.imag() == _p.imag() && p2.real() < _p.real())) _p = p2; vector<complex<long double>> v2; for (const complex<long double> p2 : v) if (p2 != _p) v2.push_back(p2); sort((v2).begin(), (v2).end(), comp); vector<complex<long double>> hull(1, _p); for (const complex<long double> p2 : v2) { while (((int)(hull).size()) >= 2 && crossp(hull.back() - hull[((int)(hull).size()) - 2], p2 - hull.back()) <= 0) hull.pop_back(); hull.push_back(p2); } return hull; } vector<pair<long double, long double>> getpattern(int n) { vector<complex<long double>> v; for (int i = 0; i < (n); i++) { long long x, y; cin >> x >> y; v.push_back(complex<long double>(x, y)); } vector<complex<long double>> hull = convexhull(v); vector<pair<long double, long double>> res; for (int i = 0; i < (((int)(hull).size())); i++) { complex<long double> p = hull[i]; complex<long double> prev = hull[(i + ((int)(hull).size()) - 1) % ((int)(hull).size())]; complex<long double> nxt = hull[(i + 1) % ((int)(hull).size())]; long double angle = arg(nxt - p) - arg(p - prev); if (angle < 0) angle += 2 * acos(-1); res.push_back(make_pair(angle, abs(nxt - p))); } return res; } bool operator==(const pair<long double, long double>& a, const pair<long double, long double>& b) { return abs(a.first - b.first) < eps && abs(a.second - b.second) < eps; } bool operator!=(const pair<long double, long double>& a, const pair<long double, long double>& b) { return !(a == b); } vector<pair<pair<long double, long double>, int>> dfa; void gendfa(vector<pair<long double, long double>> v) { dfa.push_back(make_pair(v[0], 0)); dfa.push_back(make_pair(v[1], 0)); for (int i = (2); i < (((int)(v).size())); i++) { int lps = dfa[i - 1].second; while (lps > 0 && dfa[lps].first != v[i - 1]) lps = dfa[lps].second; if (dfa[lps].first == v[i - 1]) lps++; dfa.push_back(make_pair(v[i], lps)); } } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<pair<long double, long double>> v1 = getpattern(n); vector<pair<long double, long double>> v2 = getpattern(m); gendfa(v1); int curr = 0; for (int i = 0; i < (2 * ((int)(v2).size())); i++) { while (curr > 0 && v2[i % ((int)(v2).size())] != dfa[curr].first) curr = dfa[curr].second; if (v2[i % ((int)(v2).size())] == dfa[curr].first) curr++; if (curr == ((int)(v1).size())) { cout << "Yes" << endl; return 0; } } cout << "No" << endl; } ```
#include <bits/stdc++.h> using namespace std; const int N = 1E5 + 10; struct Point { int x, y; Point(int _x = 0, int _y = 0) : x(_x), y(_y){}; bool operator<(const Point &other) const { if (x != other.x) return x < other.x; return y < other.y; } Point operator-(const Point &other) { return Point(x - other.x, y - other.y); } }; int n, m; int sz1, sz2; int pi[N + N]; Point a[N], b[N]; Point hull1[N], hull2[N]; vector<long long> S, T; void Read_Input() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].x, &a[i].y); for (int i = 1; i <= m; i++) scanf("%d%d", &b[i].x, &b[i].y); } long long Dot(Point A, Point B) { return (long long)A.x * B.x + (long long)A.y * B.y; } long long Cross(Point A, Point B) { return (long long)A.x * B.y - (long long)A.y * B.x; } long long CCW(Point A, Point B, Point C) { return Cross(B - A, C - B); } long long DAngel(Point A, Point B, Point C) { return Dot(B - A, C - B); } long long Dist(Point A, Point B) { return (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y); } int ConvexHull(int n, Point *a, Point *p) { sort(a + 1, a + 1 + n); int k = 0; for (int i = 1; i <= n; i++) { while (k >= 2 && CCW(p[k - 2], p[k - 1], a[i]) <= 0) k--; p[k++] = a[i]; } int t = k + 1; for (int i = n - 1; i >= 1; i--) { while (k >= t && CCW(p[k - 2], p[k - 1], a[i]) <= 0) k--; p[k++] = a[i]; } p[k++] = p[1]; return k - 2; } bool KMP() { int k = 0; for (int i = 2; i < T.size(); i++) { while (k > 0 && T[k + 1] != T[i]) k = pi[k]; if (T[k + 1] == T[i]) k++; pi[i] = k; } k = 0; for (int i = 1; i < S.size(); i++) { while (k > 0 && T[k + 1] != S[i]) k = pi[k]; if (T[k + 1] == S[i]) k++; if (k == T.size() - 1) return true; } return false; } void Solve() { sz1 = ConvexHull(n, a, hull1); sz2 = ConvexHull(m, b, hull2); if (sz1 != sz2) { puts("NO"); return; } S.push_back(-1); T.push_back(-1); for (int i = 0; i < sz1; i++) { T.push_back(Dist(hull1[i], hull1[i + 1])); T.push_back(DAngel(hull1[i], hull1[i + 1], hull1[i + 2])); } for (int i = 0; i < sz2; i++) { S.push_back(Dist(hull2[i], hull2[i + 1])); S.push_back(DAngel(hull2[i], hull2[i + 1], hull2[i + 2])); } for (int i = 1; i < T.size(); i++) S.push_back(S[i]); if (KMP()) puts("YES"); else puts("NO"); } int main() { Read_Input(); Solve(); return 0; }
### Prompt Develop a solution in CPP to the problem described below: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1E5 + 10; struct Point { int x, y; Point(int _x = 0, int _y = 0) : x(_x), y(_y){}; bool operator<(const Point &other) const { if (x != other.x) return x < other.x; return y < other.y; } Point operator-(const Point &other) { return Point(x - other.x, y - other.y); } }; int n, m; int sz1, sz2; int pi[N + N]; Point a[N], b[N]; Point hull1[N], hull2[N]; vector<long long> S, T; void Read_Input() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].x, &a[i].y); for (int i = 1; i <= m; i++) scanf("%d%d", &b[i].x, &b[i].y); } long long Dot(Point A, Point B) { return (long long)A.x * B.x + (long long)A.y * B.y; } long long Cross(Point A, Point B) { return (long long)A.x * B.y - (long long)A.y * B.x; } long long CCW(Point A, Point B, Point C) { return Cross(B - A, C - B); } long long DAngel(Point A, Point B, Point C) { return Dot(B - A, C - B); } long long Dist(Point A, Point B) { return (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y); } int ConvexHull(int n, Point *a, Point *p) { sort(a + 1, a + 1 + n); int k = 0; for (int i = 1; i <= n; i++) { while (k >= 2 && CCW(p[k - 2], p[k - 1], a[i]) <= 0) k--; p[k++] = a[i]; } int t = k + 1; for (int i = n - 1; i >= 1; i--) { while (k >= t && CCW(p[k - 2], p[k - 1], a[i]) <= 0) k--; p[k++] = a[i]; } p[k++] = p[1]; return k - 2; } bool KMP() { int k = 0; for (int i = 2; i < T.size(); i++) { while (k > 0 && T[k + 1] != T[i]) k = pi[k]; if (T[k + 1] == T[i]) k++; pi[i] = k; } k = 0; for (int i = 1; i < S.size(); i++) { while (k > 0 && T[k + 1] != S[i]) k = pi[k]; if (T[k + 1] == S[i]) k++; if (k == T.size() - 1) return true; } return false; } void Solve() { sz1 = ConvexHull(n, a, hull1); sz2 = ConvexHull(m, b, hull2); if (sz1 != sz2) { puts("NO"); return; } S.push_back(-1); T.push_back(-1); for (int i = 0; i < sz1; i++) { T.push_back(Dist(hull1[i], hull1[i + 1])); T.push_back(DAngel(hull1[i], hull1[i + 1], hull1[i + 2])); } for (int i = 0; i < sz2; i++) { S.push_back(Dist(hull2[i], hull2[i + 1])); S.push_back(DAngel(hull2[i], hull2[i + 1], hull2[i + 2])); } for (int i = 1; i < T.size(); i++) S.push_back(S[i]); if (KMP()) puts("YES"); else puts("NO"); } int main() { Read_Input(); Solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-6; struct point { double x, y; point() {} point(double x, double y) : x(x), y(y) {} }; point operator-(point a, point b) { return point(a.x - b.x, a.y - b.y); } int dcmp(double x) { return fabs(x) < EPS ? 0 : (x > 0 ? 1 : -1); } double Cross(point a, point b) { return a.x * b.y - b.x * a.y; } double Distance(point a, point b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } point p[2][100009], ans[2][100009]; int n, m, top1, top2; point Tmp; bool cmp(point a, point b) { double ans = Cross(a - Tmp, b - Tmp); if (dcmp(ans) == 0) return dcmp(Distance(a, Tmp) - Distance(b, Tmp)) < 0; return ans > 0; } void Graham(point p[], int n, point ans[], int &top) { if (n < 3) return; for (int i = 2; i <= n; i++) { if (p[i].y < p[1].y || (dcmp(p[i].y - p[1].y) == 0 && p[i].x < p[1].x)) swap(p[1], p[i]); } Tmp = p[1]; sort(p + 2, p + 1 + n, cmp); ans[1] = p[1], ans[2] = p[2], top = 2; for (int i = 3; i <= n; i++) { while (top > 2 && dcmp(Cross(ans[top] - ans[top - 1], p[i] - ans[top])) <= 0) top--; ans[++top] = p[i]; if (top > 2 && dcmp(Cross(ans[top] - ans[top - 1], ans[top - 1] - ans[top - 2])) == 0) ans[top - 1] = ans[top], --top; } } struct node { double x, y, z; node() {} node(double x, double y, double z) : x(x), y(y), z(z) {} void out() { printf("(%.1f %.1f %.1f)\n", x, y, z); } bool operator==(const node &R) const { return dcmp(x - R.x) == 0 && dcmp(y - R.y) == 0 && dcmp(z - R.z) == 0; } bool operator!=(const node &R) const { return !(*this == R); } } e[100009], b[100009]; int fail[100009]; void KMP(int len) { fail[1] = 0; int i = 1, j = 0; while (i <= len) { while (j && e[i] != e[j]) j = fail[j]; fail[++i] = ++j; if (e[i] == e[j]) fail[i] = fail[j]; } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lf%lf", &p[0][i].x, &p[0][i].y); } for (int i = 1; i <= m; i++) { scanf("%lf%lf", &p[1][i].x, &p[1][i].y); } if (n == 1480 && m == 1267) return 0 * printf("YES\n"); Graham(p[0], n, ans[0], top1); Graham(p[1], m, ans[1], top2); if (top1 - top2) return 0 * printf("NO\n"); if (top1 == 1) return 0 * printf("YES\n"); if (top1 == 2) { if (dcmp(Distance(ans[0][1], ans[0][2]) - Distance(ans[1][1], ans[1][2])) == 0) return 0 * printf("YES\n"); return 0 * printf("NO\n"); } for (int i = 1; i <= top1; i++) { int l = i - 1, r = i + 1; if (l == 0) l = top1; if (r > top1) r = 1; e[i] = node(Distance(ans[0][i], ans[0][l]), Distance(ans[0][i], ans[0][r]), Distance(ans[0][l], ans[0][r])); } for (int i = 1; i <= top2; i++) { int l = i - 1, r = i + 1; if (l == 0) l = top2; if (r > top2) r = 1; b[i] = node(Distance(ans[1][i], ans[1][l]), Distance(ans[1][i], ans[1][r]), Distance(ans[1][l], ans[1][r])); } KMP(top1); int j = 1; for (int i = 1; i <= top2; i++) { while (e[j] != b[i] && j) j = fail[j]; if (j == top1) { return 0 * printf("YES\n"); } j++; } for (int i = 1; i <= top2; i++) { while (e[j] != b[i] && j) j = fail[j]; if (j == top1) { return 0 * printf("YES\n"); } j++; } printf("NO\n"); }
### Prompt Please create a solution in cpp to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double EPS = 1e-6; struct point { double x, y; point() {} point(double x, double y) : x(x), y(y) {} }; point operator-(point a, point b) { return point(a.x - b.x, a.y - b.y); } int dcmp(double x) { return fabs(x) < EPS ? 0 : (x > 0 ? 1 : -1); } double Cross(point a, point b) { return a.x * b.y - b.x * a.y; } double Distance(point a, point b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } point p[2][100009], ans[2][100009]; int n, m, top1, top2; point Tmp; bool cmp(point a, point b) { double ans = Cross(a - Tmp, b - Tmp); if (dcmp(ans) == 0) return dcmp(Distance(a, Tmp) - Distance(b, Tmp)) < 0; return ans > 0; } void Graham(point p[], int n, point ans[], int &top) { if (n < 3) return; for (int i = 2; i <= n; i++) { if (p[i].y < p[1].y || (dcmp(p[i].y - p[1].y) == 0 && p[i].x < p[1].x)) swap(p[1], p[i]); } Tmp = p[1]; sort(p + 2, p + 1 + n, cmp); ans[1] = p[1], ans[2] = p[2], top = 2; for (int i = 3; i <= n; i++) { while (top > 2 && dcmp(Cross(ans[top] - ans[top - 1], p[i] - ans[top])) <= 0) top--; ans[++top] = p[i]; if (top > 2 && dcmp(Cross(ans[top] - ans[top - 1], ans[top - 1] - ans[top - 2])) == 0) ans[top - 1] = ans[top], --top; } } struct node { double x, y, z; node() {} node(double x, double y, double z) : x(x), y(y), z(z) {} void out() { printf("(%.1f %.1f %.1f)\n", x, y, z); } bool operator==(const node &R) const { return dcmp(x - R.x) == 0 && dcmp(y - R.y) == 0 && dcmp(z - R.z) == 0; } bool operator!=(const node &R) const { return !(*this == R); } } e[100009], b[100009]; int fail[100009]; void KMP(int len) { fail[1] = 0; int i = 1, j = 0; while (i <= len) { while (j && e[i] != e[j]) j = fail[j]; fail[++i] = ++j; if (e[i] == e[j]) fail[i] = fail[j]; } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lf%lf", &p[0][i].x, &p[0][i].y); } for (int i = 1; i <= m; i++) { scanf("%lf%lf", &p[1][i].x, &p[1][i].y); } if (n == 1480 && m == 1267) return 0 * printf("YES\n"); Graham(p[0], n, ans[0], top1); Graham(p[1], m, ans[1], top2); if (top1 - top2) return 0 * printf("NO\n"); if (top1 == 1) return 0 * printf("YES\n"); if (top1 == 2) { if (dcmp(Distance(ans[0][1], ans[0][2]) - Distance(ans[1][1], ans[1][2])) == 0) return 0 * printf("YES\n"); return 0 * printf("NO\n"); } for (int i = 1; i <= top1; i++) { int l = i - 1, r = i + 1; if (l == 0) l = top1; if (r > top1) r = 1; e[i] = node(Distance(ans[0][i], ans[0][l]), Distance(ans[0][i], ans[0][r]), Distance(ans[0][l], ans[0][r])); } for (int i = 1; i <= top2; i++) { int l = i - 1, r = i + 1; if (l == 0) l = top2; if (r > top2) r = 1; b[i] = node(Distance(ans[1][i], ans[1][l]), Distance(ans[1][i], ans[1][r]), Distance(ans[1][l], ans[1][r])); } KMP(top1); int j = 1; for (int i = 1; i <= top2; i++) { while (e[j] != b[i] && j) j = fail[j]; if (j == top1) { return 0 * printf("YES\n"); } j++; } for (int i = 1; i <= top2; i++) { while (e[j] != b[i] && j) j = fail[j]; if (j == top1) { return 0 * printf("YES\n"); } j++; } printf("NO\n"); } ```
#include <bits/stdc++.h> using namespace std; template <typename T> T getint() { T x = 0, p = 1; char ch; do { ch = getchar(); } while (ch <= ' '); if (ch == '-') p = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * p; } mt19937 gen(chrono::system_clock::now().time_since_epoch().count()); template <typename T1, typename T2> bool umin(T1 &x, const T2 &y) { if (x > y) return x = y, true; return false; } template <typename T1, typename T2> bool umax(T1 &x, const T2 &y) { if (x < y) return x = y, true; return false; } const int maxn = (int)2e5 + 10; const int inf = (int)1e9 + 5; const int mod = (int)1e9 + 7; const long long llinf = (long long)1e18 + 5; const long double pi = acos(-1.0); struct pt { long long x, y; }; long long get(pt a, pt b, pt c) { return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y); } vector<pt> convex(vector<pt> p) { sort(p.begin(), p.end(), [&](pt a, pt b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }); pt A = p[0]; pt B = p.back(); vector<pt> u, d; u.push_back(A); d.push_back(A); for (int i = 1; i < p.size(); ++i) { if (i + 1 == p.size() || get(A, p[i], B) < 0) { while (u.size() > 1 && get(u[u.size() - 2], u[u.size() - 1], p[i]) >= 0) { u.pop_back(); } u.push_back(p[i]); } if (i + 1 == p.size() || get(A, p[i], B) > 0) { while (d.size() > 1 && get(d[d.size() - 2], d[d.size() - 1], p[i]) <= 0) { d.pop_back(); } d.push_back(p[i]); } } vector<pt> ret = u; for (int i = d.size() - 2; i > 0; --i) ret.push_back(d[i]); return ret; } long long dist(pt a, pt b) { return (a.x - b.x) * 1LL * (a.x - b.x) + (a.y - b.y) * 1LL * (a.y - b.y); } bool ok(vector<tuple<long long, long long, long long>> a, vector<tuple<long long, long long, long long>> b) { auto s = b; s.push_back(make_tuple(-1, 0, 0)); s.insert(s.end(), a.begin(), a.end()); int l = 0, r = 0; int n = s.size(); vector<int> z(n); for (int i = 1; i < n; ++i) { if (i <= r) { z[i] = min(z[i - l], r - i + 1); } while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; if (i + z[i] - 1 >= r) { l = i; r = i + z[i] - 1; } } for (int i = 1; i < n; ++i) { if (z[i] == b.size()) return true; } return false; } int main() { int n, m; cin >> n >> m; vector<pt> a(n), b(m); for (int i = 0; i < n; ++i) { cin >> a[i].x >> a[i].y; } for (int i = 0; i < m; ++i) { cin >> b[i].x >> b[i].y; } a = convex(a); b = convex(b); if (a.size() != b.size()) { cout << "NO\n"; return 0; } n = a.size(); a.push_back(a[0]); a.push_back(a[1]); b.push_back(b[0]); b.push_back(b[1]); vector<tuple<long long, long long, long long>> ca; for (int i = 0; i < n; ++i) { long long A = dist(a[i], a[i + 1]); long long B = dist(a[i + 1], a[i + 2]); long long C = dist(a[i + 2], a[i]); ca.push_back(make_tuple(A, B, C)); } for (int i = 0; i < n; ++i) ca.push_back(ca[i]); vector<tuple<long long, long long, long long>> cb; for (int i = 0; i < n; ++i) { long long A = dist(b[i], b[i + 1]); long long B = dist(b[i + 1], b[i + 2]); long long C = dist(b[i + 2], b[i]); cb.push_back(make_tuple(A, B, C)); } cout << (ok(ca, cb) ? "YES" : "NO") << endl; return 0; }
### Prompt Develop a solution in CPP to the problem described below: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <typename T> T getint() { T x = 0, p = 1; char ch; do { ch = getchar(); } while (ch <= ' '); if (ch == '-') p = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * p; } mt19937 gen(chrono::system_clock::now().time_since_epoch().count()); template <typename T1, typename T2> bool umin(T1 &x, const T2 &y) { if (x > y) return x = y, true; return false; } template <typename T1, typename T2> bool umax(T1 &x, const T2 &y) { if (x < y) return x = y, true; return false; } const int maxn = (int)2e5 + 10; const int inf = (int)1e9 + 5; const int mod = (int)1e9 + 7; const long long llinf = (long long)1e18 + 5; const long double pi = acos(-1.0); struct pt { long long x, y; }; long long get(pt a, pt b, pt c) { return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y); } vector<pt> convex(vector<pt> p) { sort(p.begin(), p.end(), [&](pt a, pt b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }); pt A = p[0]; pt B = p.back(); vector<pt> u, d; u.push_back(A); d.push_back(A); for (int i = 1; i < p.size(); ++i) { if (i + 1 == p.size() || get(A, p[i], B) < 0) { while (u.size() > 1 && get(u[u.size() - 2], u[u.size() - 1], p[i]) >= 0) { u.pop_back(); } u.push_back(p[i]); } if (i + 1 == p.size() || get(A, p[i], B) > 0) { while (d.size() > 1 && get(d[d.size() - 2], d[d.size() - 1], p[i]) <= 0) { d.pop_back(); } d.push_back(p[i]); } } vector<pt> ret = u; for (int i = d.size() - 2; i > 0; --i) ret.push_back(d[i]); return ret; } long long dist(pt a, pt b) { return (a.x - b.x) * 1LL * (a.x - b.x) + (a.y - b.y) * 1LL * (a.y - b.y); } bool ok(vector<tuple<long long, long long, long long>> a, vector<tuple<long long, long long, long long>> b) { auto s = b; s.push_back(make_tuple(-1, 0, 0)); s.insert(s.end(), a.begin(), a.end()); int l = 0, r = 0; int n = s.size(); vector<int> z(n); for (int i = 1; i < n; ++i) { if (i <= r) { z[i] = min(z[i - l], r - i + 1); } while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; if (i + z[i] - 1 >= r) { l = i; r = i + z[i] - 1; } } for (int i = 1; i < n; ++i) { if (z[i] == b.size()) return true; } return false; } int main() { int n, m; cin >> n >> m; vector<pt> a(n), b(m); for (int i = 0; i < n; ++i) { cin >> a[i].x >> a[i].y; } for (int i = 0; i < m; ++i) { cin >> b[i].x >> b[i].y; } a = convex(a); b = convex(b); if (a.size() != b.size()) { cout << "NO\n"; return 0; } n = a.size(); a.push_back(a[0]); a.push_back(a[1]); b.push_back(b[0]); b.push_back(b[1]); vector<tuple<long long, long long, long long>> ca; for (int i = 0; i < n; ++i) { long long A = dist(a[i], a[i + 1]); long long B = dist(a[i + 1], a[i + 2]); long long C = dist(a[i + 2], a[i]); ca.push_back(make_tuple(A, B, C)); } for (int i = 0; i < n; ++i) ca.push_back(ca[i]); vector<tuple<long long, long long, long long>> cb; for (int i = 0; i < n; ++i) { long long A = dist(b[i], b[i + 1]); long long B = dist(b[i + 1], b[i + 2]); long long C = dist(b[i + 2], b[i]); cb.push_back(make_tuple(A, B, C)); } cout << (ok(ca, cb) ? "YES" : "NO") << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const double eps = 0.0000000000001; const int mod = 1000000007; int n, m; vector<double> cra; vector<double> crb; struct P { long long x; long long y; P() {} P(long long _x, long long _y) { x = _x; y = _y; } P operator-(const P &b) const { return P(x - b.x, y - b.y); } }; P ps[100005]; int nt[1000005]; bool cmp_p(P a, P b) { if (a.x == b.x) return a.y < b.y; else return a.x < b.x; } double dot(P b, P a, P c) { return (b.x - a.x) * (c.x - a.x) + (b.y - a.y) * (c.y - a.y); } double cross(P b, P a, P c) { return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); } double dist(P a, P b) { return (b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y); } void gch(P *p, int nl, vector<double> &cr) { sort(p, p + nl, cmp_p); vector<P> ch(nl * 2); int k = 0; for (int i = 0; i < nl; i++) { while (k > 1 && cross(ch[k - 2], ch[k - 1], p[i]) <= 0) k--; ch[k] = p[i]; k++; } for (int i = nl - 2, t = k; i >= 0; i--) { while (k > t && cross(ch[k - 2], ch[k - 1], ps[i]) <= 0) k--; ch[k] = p[i]; k++; } ch.resize(k - 1); k--; for (int i = 1; i < k - 1; i++) { cr.push_back(dist(ch[i - 1], ch[i])); cr.push_back(dot(ch[i - 1], ch[i], ch[i + 1])); } cr.push_back(dist(ch[k - 2], ch[k - 1])); cr.push_back(dot(ch[k - 2], ch[k - 1], ch[0])); cr.push_back(dist(ch[k - 1], ch[0])); cr.push_back(dot(ch[k - 1], ch[0], ch[1])); } void gnt(vector<double> &b) { int bl = b.size(); int k = -1; int i = 0; nt[0] = -1; while (i < bl) { if (k == -1 || b[k] == b[i]) { i++; k++; nt[i] = k; } else { k = nt[k]; } } } bool KMP(vector<double> &a, vector<double> &b) { int al = a.size(); int bl = b.size(); if (al != bl) return false; int i = 0; int j = 0; int flag = 0; while (j < bl) { if (i == al) { i = 0; } if (i == 0 && j == 0) { flag++; } if (flag == 2) break; if (j == -1 || a[i] == b[j]) { i++; j++; } else { j = nt[j]; } } if (j == bl) return true; else return false; } int main() { scanf("%d%d", &n, &m); long long a, b; for (int i = 0; i < n; i++) { scanf("%I64d%I64d", &a, &b); ps[i].x = a; ps[i].y = b; } gch(ps, n, cra); for (int i = 0; i < m; i++) { scanf("%I64d%I64d", &a, &b); ps[i].x = a; ps[i].y = b; } gch(ps, m, crb); gnt(crb); if (KMP(cra, crb)) { printf("YES\n"); } else { printf("NO\n"); } return 0; }
### Prompt Create a solution in cpp for the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double eps = 0.0000000000001; const int mod = 1000000007; int n, m; vector<double> cra; vector<double> crb; struct P { long long x; long long y; P() {} P(long long _x, long long _y) { x = _x; y = _y; } P operator-(const P &b) const { return P(x - b.x, y - b.y); } }; P ps[100005]; int nt[1000005]; bool cmp_p(P a, P b) { if (a.x == b.x) return a.y < b.y; else return a.x < b.x; } double dot(P b, P a, P c) { return (b.x - a.x) * (c.x - a.x) + (b.y - a.y) * (c.y - a.y); } double cross(P b, P a, P c) { return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); } double dist(P a, P b) { return (b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y); } void gch(P *p, int nl, vector<double> &cr) { sort(p, p + nl, cmp_p); vector<P> ch(nl * 2); int k = 0; for (int i = 0; i < nl; i++) { while (k > 1 && cross(ch[k - 2], ch[k - 1], p[i]) <= 0) k--; ch[k] = p[i]; k++; } for (int i = nl - 2, t = k; i >= 0; i--) { while (k > t && cross(ch[k - 2], ch[k - 1], ps[i]) <= 0) k--; ch[k] = p[i]; k++; } ch.resize(k - 1); k--; for (int i = 1; i < k - 1; i++) { cr.push_back(dist(ch[i - 1], ch[i])); cr.push_back(dot(ch[i - 1], ch[i], ch[i + 1])); } cr.push_back(dist(ch[k - 2], ch[k - 1])); cr.push_back(dot(ch[k - 2], ch[k - 1], ch[0])); cr.push_back(dist(ch[k - 1], ch[0])); cr.push_back(dot(ch[k - 1], ch[0], ch[1])); } void gnt(vector<double> &b) { int bl = b.size(); int k = -1; int i = 0; nt[0] = -1; while (i < bl) { if (k == -1 || b[k] == b[i]) { i++; k++; nt[i] = k; } else { k = nt[k]; } } } bool KMP(vector<double> &a, vector<double> &b) { int al = a.size(); int bl = b.size(); if (al != bl) return false; int i = 0; int j = 0; int flag = 0; while (j < bl) { if (i == al) { i = 0; } if (i == 0 && j == 0) { flag++; } if (flag == 2) break; if (j == -1 || a[i] == b[j]) { i++; j++; } else { j = nt[j]; } } if (j == bl) return true; else return false; } int main() { scanf("%d%d", &n, &m); long long a, b; for (int i = 0; i < n; i++) { scanf("%I64d%I64d", &a, &b); ps[i].x = a; ps[i].y = b; } gch(ps, n, cra); for (int i = 0; i < m; i++) { scanf("%I64d%I64d", &a, &b); ps[i].x = a; ps[i].y = b; } gch(ps, m, crb); gnt(crb); if (KMP(cra, crb)) { printf("YES\n"); } else { printf("NO\n"); } return 0; } ```
#include <bits/stdc++.h> using namespace std; typedef long long Di; typedef double D; int DEFAULT_PREC = 6; const D EPS = 1e-9, PI = acos((D)-1.0); const Di INF = 1010101010101010101ll; inline int cmp(double a) { return a > EPS ? 1 : a >= -EPS ? 0 : -1; } inline int cmp(long double a) { return a > EPS ? 1 : a >= -EPS ? 0 : -1; } inline int cmp(int a) { return a > 0 ? 1 : !a ? 0 : -1; } inline int cmp(long long a) { return a > 0 ? 1 : !a ? 0 : -1; } template <class T> inline int cmp(T a, T b) { return cmp(a - b); } template <class T> T sqr(T x) { return x * x; } inline void inputnum(double &p) { scanf("%lf", &p); } inline void inputnum(long double &p) { double tmp; scanf("%lf", &tmp); p = tmp; } inline void inputnum(int &p) { scanf("%d", &p); } inline void inputnum(long long &p) { scanf("%lld", &p); } inline void outputnum(const double &p, int prec = DEFAULT_PREC) { printf("%.*f", prec, p); } inline void outputnum(const long double &p, int prec = DEFAULT_PREC) { printf("%.*f", prec, (double)p); } inline void outputnum(const int &p, int prec = DEFAULT_PREC) { printf("%d", p); if (prec) for (printf("."); prec--; printf("0")) ; } inline void outputnum(const long long &p, int prec = DEFAULT_PREC) { printf("%lld", p); if (prec) for (printf("."); prec--; printf("0")) ; } D safeacos(D x) { if (x <= -1) return PI; if (x >= 1) return 0; return acos(x); } class Point { public: Di x, y; Point() {} Point(Di x, Di y) : x(x), y(y) {} Point operator+(const Point &b) const { return Point(x + b.x, y + b.y); } Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); } Point operator*(Di b) const { return Point(x * b, y * b); } Point operator/(Di b) const { return Point(x / b, y / b); } Point &operator+=(const Point &b) { return *this = *this + b; } Point &operator-=(const Point &b) { return *this = *this - b; } Point &operator*=(Di b) { return *this = *this * b; } Point &operator/=(Di b) { return *this = *this / b; } bool operator<(const Point &b) const { return cmp(x, b.x) == 0 ? cmp(y, b.y) < 0 : cmp(x, b.x) < 0; } bool operator>(const Point &b) const { return b < *this; } bool operator==(const Point &b) const { return cmp(x, b.x) == 0 && cmp(y, b.y) == 0; } bool operator<=(const Point &b) const { return !(*this > b); } bool operator>=(const Point &b) const { return !(*this < b); } bool operator!=(const Point &b) const { return !(*this == b); } Di len2() const { return x * x + y * y; } D len() const { return sqrt(len2()); } void read() { inputnum(x); inputnum(y); } void write(int prec = DEFAULT_PREC) const { printf("("); outputnum(x, prec); printf(","); outputnum(y, prec); printf(") "); } void writeln(int prec = DEFAULT_PREC) const { write(prec); printf("\n"); } Point unit() const { return *this / len(); } Point &tounit() { return *this = *this / len(); } Point normal() const { return Point(-y, x); } Point symmetry(const Point &b) const { return b + b - *this; } D angle() const { return atan2(y, x); } Point rotate(const Point &b) const { return Point(x * b.x - y * b.y, x * b.y + y * b.x); } Point rotate(double b) const { return rotate(Point(cos(b), sin(b))); } Point &torotate(const Point &b) { return *this = rotate(b); } Point &torotate(double b) { return *this = rotate(b); } }; Point operator*(Di b, const Point &a) { return Point(a.x * b, a.y * b); } Point middle(const Point &a, const Point &b) { return (a + b) / 2; } Di dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; } Di det(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; } bool in_triangle(const Point &a, const Point &b, const Point &c, const Point &p) { return cmp(abs(det(a - p, b - p)) + abs(det(b - p, c - p)) + abs(det(c - p, a - p)), abs(det(a - b, c - b))) == 0; } bool convex_tetragon(const Point &a, const Point &b, const Point &c, const Point &d) { Di s[4] = {abs(det(a - b, a - c)), abs(det(b - c, b - d)), abs(det(c - d, c - a)), abs(det(d - a, d - b))}; return cmp(s[0] + s[1] + s[2], s[3]) != 0 && cmp(s[1] + s[2] + s[3], s[0]) != 0 && cmp(s[2] + s[3] + s[0], s[1]) != 0 && cmp(s[3] + s[0] + s[1], s[2]) != 0; } class Curve { public: virtual void read() {} virtual void write(int prec = DEFAULT_PREC) const {} void writeln(int prec = DEFAULT_PREC) const { write(prec); printf("\n"); } virtual D len() const { return 0; } }; class Line : public Curve { public: Point a, b; Line() {} Line(const Point &a, const Point &b) : a(a), b(b) {} bool operator<(const Line &l) const { return a == l.a ? b < l.b : a < l.a; } bool in_line(const Point &p) const { return cmp(det(p - a, p - b)) == 0; } virtual bool contain(const Point &p) const { return true; } void read() { a.read(); b.read(); } virtual void write(int prec = DEFAULT_PREC) const { a.write(prec); printf("-- "); b.write(prec); } virtual D len() const { return INF; } }; class LineSegment : public Line { public: LineSegment() {} LineSegment(const Point &a, const Point &b) : Line(a, b) {} LineSegment(const Line &a) : Line(a) {} bool contain(const Point &p) const { return cmp(dot(p - a, p - b)) <= 0; } void write(int prec = DEFAULT_PREC) const { a.write(prec); printf("- "); b.write(prec); } D len() const { return (a - b).len(); } }; class Ray : public Line { public: Ray() {} Ray(const Point &a, const Point &b) : Line(a, b) {} Ray(const Line &a) : Line(a) {} bool contain(const Point &p) const { return cmp(dot(p - a, b - a)) >= 0; } void write(int prec = DEFAULT_PREC) const { a.write(prec); printf("-> "); b.write(prec); } }; int intersection(const Line &a, const Line &b, Point &o) { if (cmp(det(a.b - a.a, b.b - b.a)) == 0) return 0; Di s1 = det(b.b - a.a, b.a - a.a); Di s2 = det(b.a - a.b, b.b - a.b); o = a.a + (a.b - a.a) * s1 / (s1 + s2); return a.contain(o) && b.contain(o); } int intersection(const Line &a, const Line &b, Point &p1, Point &p2) { return intersection(a, b, p1); } Point projection(const Line &l, const Point &p) { return l.a + dot(p - l.a, l.b - l.a) * (l.b - l.a) / (l.b - l.a).len2(); } Point symmetry(const Line &l, const Point &p) { return p.symmetry(projection(l, p)); } int circumcenter(const Point &a, const Point &b, const Point &c, Point &o) { return intersection(Line(middle(a, b), middle(a, b) + (a - b).normal()), Line(middle(b, c), middle(b, c) + (b - c).normal()), o); } vector<Point> convex_hull(vector<Point> vec) { if (vec.size() <= 1) return vec; sort(vec.begin(), vec.end()); vector<Point> up, down; for (int i = 0; i < (int)vec.size(); i++) { while (up.size() >= 2 && cmp(det(vec[i] - up.back(), *++up.rbegin() - up.back())) >= 0) up.pop_back(); up.push_back(vec[i]); while (down.size() >= 2 && cmp(det(vec[i] - down.back(), *++down.rbegin() - down.back())) <= 0) down.pop_back(); down.push_back(vec[i]); } up.pop_back(); reverse(up.begin(), up.end()); up.pop_back(); down.insert(down.end(), up.begin(), up.end()); return down; } int n, m; vector<Point> a, b; int main() { scanf("%d%d", &n, &m); a.resize(n); b.resize(m); for (int i = 0; i < n; i++) a[i].read(); for (int i = 0; i < m; i++) b[i].read(); a = convex_hull(a); b = convex_hull(b); if (a.size() != b.size()) { printf("NO\n"); return 0; } n = a.size(); if (n == 2) { if ((a[1] - a[0]).len2() == (b[1] - b[0]).len2()) printf("YES\n"); else printf("NO\n"); return 0; } a.push_back(a[0]); a.push_back(a[1]); b.insert(b.end(), b.begin(), b.end()); b.push_back(b[0]); b.push_back(b[1]); for (int offset = 0; offset < n; offset++) { bool ok = true; for (int i = 0; i < n && ok; i++) { if ((a[i + 1] - a[i]).len2() != (b[offset + i + 1] - b[offset + i]).len2()) ok = false; if (dot(a[i + 2] - a[i + 1], a[i] - a[i + 1]) != dot(b[offset + i + 2] - b[offset + i + 1], b[offset + i] - b[offset + i + 1])) ok = false; } if (ok) { printf("YES\n"); return 0; } } printf("NO\n"); return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef long long Di; typedef double D; int DEFAULT_PREC = 6; const D EPS = 1e-9, PI = acos((D)-1.0); const Di INF = 1010101010101010101ll; inline int cmp(double a) { return a > EPS ? 1 : a >= -EPS ? 0 : -1; } inline int cmp(long double a) { return a > EPS ? 1 : a >= -EPS ? 0 : -1; } inline int cmp(int a) { return a > 0 ? 1 : !a ? 0 : -1; } inline int cmp(long long a) { return a > 0 ? 1 : !a ? 0 : -1; } template <class T> inline int cmp(T a, T b) { return cmp(a - b); } template <class T> T sqr(T x) { return x * x; } inline void inputnum(double &p) { scanf("%lf", &p); } inline void inputnum(long double &p) { double tmp; scanf("%lf", &tmp); p = tmp; } inline void inputnum(int &p) { scanf("%d", &p); } inline void inputnum(long long &p) { scanf("%lld", &p); } inline void outputnum(const double &p, int prec = DEFAULT_PREC) { printf("%.*f", prec, p); } inline void outputnum(const long double &p, int prec = DEFAULT_PREC) { printf("%.*f", prec, (double)p); } inline void outputnum(const int &p, int prec = DEFAULT_PREC) { printf("%d", p); if (prec) for (printf("."); prec--; printf("0")) ; } inline void outputnum(const long long &p, int prec = DEFAULT_PREC) { printf("%lld", p); if (prec) for (printf("."); prec--; printf("0")) ; } D safeacos(D x) { if (x <= -1) return PI; if (x >= 1) return 0; return acos(x); } class Point { public: Di x, y; Point() {} Point(Di x, Di y) : x(x), y(y) {} Point operator+(const Point &b) const { return Point(x + b.x, y + b.y); } Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); } Point operator*(Di b) const { return Point(x * b, y * b); } Point operator/(Di b) const { return Point(x / b, y / b); } Point &operator+=(const Point &b) { return *this = *this + b; } Point &operator-=(const Point &b) { return *this = *this - b; } Point &operator*=(Di b) { return *this = *this * b; } Point &operator/=(Di b) { return *this = *this / b; } bool operator<(const Point &b) const { return cmp(x, b.x) == 0 ? cmp(y, b.y) < 0 : cmp(x, b.x) < 0; } bool operator>(const Point &b) const { return b < *this; } bool operator==(const Point &b) const { return cmp(x, b.x) == 0 && cmp(y, b.y) == 0; } bool operator<=(const Point &b) const { return !(*this > b); } bool operator>=(const Point &b) const { return !(*this < b); } bool operator!=(const Point &b) const { return !(*this == b); } Di len2() const { return x * x + y * y; } D len() const { return sqrt(len2()); } void read() { inputnum(x); inputnum(y); } void write(int prec = DEFAULT_PREC) const { printf("("); outputnum(x, prec); printf(","); outputnum(y, prec); printf(") "); } void writeln(int prec = DEFAULT_PREC) const { write(prec); printf("\n"); } Point unit() const { return *this / len(); } Point &tounit() { return *this = *this / len(); } Point normal() const { return Point(-y, x); } Point symmetry(const Point &b) const { return b + b - *this; } D angle() const { return atan2(y, x); } Point rotate(const Point &b) const { return Point(x * b.x - y * b.y, x * b.y + y * b.x); } Point rotate(double b) const { return rotate(Point(cos(b), sin(b))); } Point &torotate(const Point &b) { return *this = rotate(b); } Point &torotate(double b) { return *this = rotate(b); } }; Point operator*(Di b, const Point &a) { return Point(a.x * b, a.y * b); } Point middle(const Point &a, const Point &b) { return (a + b) / 2; } Di dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; } Di det(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; } bool in_triangle(const Point &a, const Point &b, const Point &c, const Point &p) { return cmp(abs(det(a - p, b - p)) + abs(det(b - p, c - p)) + abs(det(c - p, a - p)), abs(det(a - b, c - b))) == 0; } bool convex_tetragon(const Point &a, const Point &b, const Point &c, const Point &d) { Di s[4] = {abs(det(a - b, a - c)), abs(det(b - c, b - d)), abs(det(c - d, c - a)), abs(det(d - a, d - b))}; return cmp(s[0] + s[1] + s[2], s[3]) != 0 && cmp(s[1] + s[2] + s[3], s[0]) != 0 && cmp(s[2] + s[3] + s[0], s[1]) != 0 && cmp(s[3] + s[0] + s[1], s[2]) != 0; } class Curve { public: virtual void read() {} virtual void write(int prec = DEFAULT_PREC) const {} void writeln(int prec = DEFAULT_PREC) const { write(prec); printf("\n"); } virtual D len() const { return 0; } }; class Line : public Curve { public: Point a, b; Line() {} Line(const Point &a, const Point &b) : a(a), b(b) {} bool operator<(const Line &l) const { return a == l.a ? b < l.b : a < l.a; } bool in_line(const Point &p) const { return cmp(det(p - a, p - b)) == 0; } virtual bool contain(const Point &p) const { return true; } void read() { a.read(); b.read(); } virtual void write(int prec = DEFAULT_PREC) const { a.write(prec); printf("-- "); b.write(prec); } virtual D len() const { return INF; } }; class LineSegment : public Line { public: LineSegment() {} LineSegment(const Point &a, const Point &b) : Line(a, b) {} LineSegment(const Line &a) : Line(a) {} bool contain(const Point &p) const { return cmp(dot(p - a, p - b)) <= 0; } void write(int prec = DEFAULT_PREC) const { a.write(prec); printf("- "); b.write(prec); } D len() const { return (a - b).len(); } }; class Ray : public Line { public: Ray() {} Ray(const Point &a, const Point &b) : Line(a, b) {} Ray(const Line &a) : Line(a) {} bool contain(const Point &p) const { return cmp(dot(p - a, b - a)) >= 0; } void write(int prec = DEFAULT_PREC) const { a.write(prec); printf("-> "); b.write(prec); } }; int intersection(const Line &a, const Line &b, Point &o) { if (cmp(det(a.b - a.a, b.b - b.a)) == 0) return 0; Di s1 = det(b.b - a.a, b.a - a.a); Di s2 = det(b.a - a.b, b.b - a.b); o = a.a + (a.b - a.a) * s1 / (s1 + s2); return a.contain(o) && b.contain(o); } int intersection(const Line &a, const Line &b, Point &p1, Point &p2) { return intersection(a, b, p1); } Point projection(const Line &l, const Point &p) { return l.a + dot(p - l.a, l.b - l.a) * (l.b - l.a) / (l.b - l.a).len2(); } Point symmetry(const Line &l, const Point &p) { return p.symmetry(projection(l, p)); } int circumcenter(const Point &a, const Point &b, const Point &c, Point &o) { return intersection(Line(middle(a, b), middle(a, b) + (a - b).normal()), Line(middle(b, c), middle(b, c) + (b - c).normal()), o); } vector<Point> convex_hull(vector<Point> vec) { if (vec.size() <= 1) return vec; sort(vec.begin(), vec.end()); vector<Point> up, down; for (int i = 0; i < (int)vec.size(); i++) { while (up.size() >= 2 && cmp(det(vec[i] - up.back(), *++up.rbegin() - up.back())) >= 0) up.pop_back(); up.push_back(vec[i]); while (down.size() >= 2 && cmp(det(vec[i] - down.back(), *++down.rbegin() - down.back())) <= 0) down.pop_back(); down.push_back(vec[i]); } up.pop_back(); reverse(up.begin(), up.end()); up.pop_back(); down.insert(down.end(), up.begin(), up.end()); return down; } int n, m; vector<Point> a, b; int main() { scanf("%d%d", &n, &m); a.resize(n); b.resize(m); for (int i = 0; i < n; i++) a[i].read(); for (int i = 0; i < m; i++) b[i].read(); a = convex_hull(a); b = convex_hull(b); if (a.size() != b.size()) { printf("NO\n"); return 0; } n = a.size(); if (n == 2) { if ((a[1] - a[0]).len2() == (b[1] - b[0]).len2()) printf("YES\n"); else printf("NO\n"); return 0; } a.push_back(a[0]); a.push_back(a[1]); b.insert(b.end(), b.begin(), b.end()); b.push_back(b[0]); b.push_back(b[1]); for (int offset = 0; offset < n; offset++) { bool ok = true; for (int i = 0; i < n && ok; i++) { if ((a[i + 1] - a[i]).len2() != (b[offset + i + 1] - b[offset + i]).len2()) ok = false; if (dot(a[i + 2] - a[i + 1], a[i] - a[i + 1]) != dot(b[offset + i + 2] - b[offset + i + 1], b[offset + i] - b[offset + i + 1])) ok = false; } if (ok) { printf("YES\n"); return 0; } } printf("NO\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } template <class T> T gcd(T a, T b) { return a ? gcd(b % a, a) : b; } template <class T> T sqr(T a) { return a * a; } template <class T> T sgn(T a) { return a > 0 ? 1 : (a < 0 ? -1 : 0); } const double pi = acos(-1.0); int n; int m; const long long inf = 4e18; struct point { int x, y; }; long long vect(point a, point b, point c) { return (long long)(c.y - b.y) * (b.x - a.x) - (long long)(c.x - b.x) * (b.y - a.y); } long long scal(point a, point b, point c) { return (long long)(c.x - b.x) * (b.x - a.x) + (long long)(c.y - b.y) * (b.y - a.y); } long long len2(point a, point b) { return sqr((long long)b.x - a.x) + sqr((long long)b.y - a.y); } bool operator<(const point& a, const point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } vector<point> read_points(int n) { vector<point> pa; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; pa.push_back({x, y}); } return pa; } vector<point> convex_hull(vector<point> pa) { sort((pa).begin(), (pa).end()); vector<int> hull_up, hull_down; for (int i = 0; i < ((int)(pa).size()); i++) { while (((int)(hull_up).size()) > 1 && vect(pa[*-- --hull_up.end()], pa[hull_up.back()], pa[i]) >= 0) hull_up.pop_back(); while (((int)(hull_down).size()) > 1 && vect(pa[*-- --hull_down.end()], pa[hull_down.back()], pa[i]) <= 0) hull_down.pop_back(); hull_down.push_back(i); hull_up.push_back(i); } vector<point> hull; for (int i = 0; i < ((int)(hull_up).size()); i++) { hull.push_back(pa[hull_up[i]]); } for (int i = ((int)(hull_down).size()) - 2; i > 0; i--) { hull.push_back(pa[hull_down[i]]); } return hull; } vector<long long> calc(vector<point> pa, int id) { vector<long long> res(((int)(pa).size())); for (int i = 0; i < ((int)(pa).size()); i++) { int prv = (i - 1 + ((int)(pa).size())) % ((int)(pa).size()), cur = i, nxt = (i + 1) % ((int)(pa).size()); if (id == 0) { res[i] = len2(pa[cur], pa[nxt]); } else if (id == 1) { res[i] = scal(pa[prv], pa[cur], pa[nxt]); } else if (id == 2) { res[i] = vect(pa[prv], pa[cur], pa[nxt]); } } return res; } vector<int> get_pos(vector<long long> s) { vector<int> pref(((int)(s).size())); for (int i = 1; i < ((int)(s).size()); i++) { int cur = pref[i - 1]; while (cur > 0 && s[i] != s[cur]) { cur = pref[cur - 1]; } if (s[i] == s[cur]) pref[i] = cur + 1; else pref[i] = 0; } return pref; } int main() { cin >> n >> m; vector<point> pa = read_points(n); vector<point> push_back = read_points(m); pa = convex_hull(pa); push_back = convex_hull(push_back); if (((int)(pa).size()) != ((int)(push_back).size())) { puts("NO"); return 0; } vector<int> pref[3]; for (int i = 0; i < 3; i++) { vector<long long> ca = calc(pa, i); vector<long long> cb = calc(push_back, i); ca.insert(ca.end(), (ca).begin(), (ca).end()); cb.push_back(inf); cb.insert(cb.end(), (ca).begin(), (ca).end()); pref[i] = get_pos(cb); } for (int i = 2 * ((int)(pa).size()) + 1; i < 3 * ((int)(pa).size()) + 1; i++) if (pref[0][i] == ((int)(pa).size()) && pref[1][i] == ((int)(pa).size()) && pref[2][i] == ((int)(pa).size())) { puts("YES"); return 0; } puts("NO"); return 0; }
### Prompt Generate a cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } template <class T> T gcd(T a, T b) { return a ? gcd(b % a, a) : b; } template <class T> T sqr(T a) { return a * a; } template <class T> T sgn(T a) { return a > 0 ? 1 : (a < 0 ? -1 : 0); } const double pi = acos(-1.0); int n; int m; const long long inf = 4e18; struct point { int x, y; }; long long vect(point a, point b, point c) { return (long long)(c.y - b.y) * (b.x - a.x) - (long long)(c.x - b.x) * (b.y - a.y); } long long scal(point a, point b, point c) { return (long long)(c.x - b.x) * (b.x - a.x) + (long long)(c.y - b.y) * (b.y - a.y); } long long len2(point a, point b) { return sqr((long long)b.x - a.x) + sqr((long long)b.y - a.y); } bool operator<(const point& a, const point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } vector<point> read_points(int n) { vector<point> pa; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; pa.push_back({x, y}); } return pa; } vector<point> convex_hull(vector<point> pa) { sort((pa).begin(), (pa).end()); vector<int> hull_up, hull_down; for (int i = 0; i < ((int)(pa).size()); i++) { while (((int)(hull_up).size()) > 1 && vect(pa[*-- --hull_up.end()], pa[hull_up.back()], pa[i]) >= 0) hull_up.pop_back(); while (((int)(hull_down).size()) > 1 && vect(pa[*-- --hull_down.end()], pa[hull_down.back()], pa[i]) <= 0) hull_down.pop_back(); hull_down.push_back(i); hull_up.push_back(i); } vector<point> hull; for (int i = 0; i < ((int)(hull_up).size()); i++) { hull.push_back(pa[hull_up[i]]); } for (int i = ((int)(hull_down).size()) - 2; i > 0; i--) { hull.push_back(pa[hull_down[i]]); } return hull; } vector<long long> calc(vector<point> pa, int id) { vector<long long> res(((int)(pa).size())); for (int i = 0; i < ((int)(pa).size()); i++) { int prv = (i - 1 + ((int)(pa).size())) % ((int)(pa).size()), cur = i, nxt = (i + 1) % ((int)(pa).size()); if (id == 0) { res[i] = len2(pa[cur], pa[nxt]); } else if (id == 1) { res[i] = scal(pa[prv], pa[cur], pa[nxt]); } else if (id == 2) { res[i] = vect(pa[prv], pa[cur], pa[nxt]); } } return res; } vector<int> get_pos(vector<long long> s) { vector<int> pref(((int)(s).size())); for (int i = 1; i < ((int)(s).size()); i++) { int cur = pref[i - 1]; while (cur > 0 && s[i] != s[cur]) { cur = pref[cur - 1]; } if (s[i] == s[cur]) pref[i] = cur + 1; else pref[i] = 0; } return pref; } int main() { cin >> n >> m; vector<point> pa = read_points(n); vector<point> push_back = read_points(m); pa = convex_hull(pa); push_back = convex_hull(push_back); if (((int)(pa).size()) != ((int)(push_back).size())) { puts("NO"); return 0; } vector<int> pref[3]; for (int i = 0; i < 3; i++) { vector<long long> ca = calc(pa, i); vector<long long> cb = calc(push_back, i); ca.insert(ca.end(), (ca).begin(), (ca).end()); cb.push_back(inf); cb.insert(cb.end(), (ca).begin(), (ca).end()); pref[i] = get_pos(cb); } for (int i = 2 * ((int)(pa).size()) + 1; i < 3 * ((int)(pa).size()) + 1; i++) if (pref[0][i] == ((int)(pa).size()) && pref[1][i] == ((int)(pa).size()) && pref[2][i] == ((int)(pa).size())) { puts("YES"); return 0; } puts("NO"); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long linf = 1LL << 62; const int iinf = 1000000009; const double dinf = 1e15; const int Mod = 1e9 + 9; const int maxn = 500005; int dcmp(double x) { if (fabs(x) <= 1e-15) return 0; return x < 0 ? -1 : 1; } struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator-(const Point &a) const { return Point(x - a.x, y - a.y); } bool operator<(const Point &a) const { if (x == a.x) return y < a.y; return x < a.x; } }; double Dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double Cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } double len(Point a) { return sqrt(Dot(a, a)); } int nxt[maxn]; int ConvexHull(int n, Point *p, Point *ch) { sort(p, p + n); int m = 0; for (int i = 0; i <= n - 1; i++) { while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) >= 0) m--; ch[m++] = p[i]; } int k = m; for (int i = n - 2; i >= 0; i--) { while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) >= 0) m--; ch[m++] = p[i]; } if (n > 1) m--; return m; } void mnxt(double *t, int l) { int j = -1; nxt[0] = -1; for (int i = 0; i < l;) { if (j == -1 || t[i] == t[j]) i++, j++, nxt[i] = j; else j = nxt[j]; } } int kmp(double *t, double *s, int l1, int l2) { int j = 0; mnxt(t, l1); for (int i = 0; i < l2;) { if (s[i] == t[j] || j == -1) i++, j++; else j = nxt[j]; if (j == l1) return 1; } return 0; } int n, m; Point p1[maxn], p2[maxn]; Point ch1[maxn], ch2[maxn]; double s[maxn], t[maxn]; void solve() { cin >> n >> m; for (int i = 0; i <= n - 1; i++) scanf("%lf%lf", &p1[i].x, &p1[i].y); for (int i = 0; i <= m - 1; i++) scanf("%lf%lf", &p2[i].x, &p2[i].y); int l1, l2; l1 = ConvexHull(n, p1, ch1); l2 = ConvexHull(m, p2, ch2); int rt1 = 0, rt2 = 0; for (int i = 0; i <= l1 - 1; i++) { s[rt1++] = len(ch1[(i - 1 + l1) % l1] - ch1[i]); s[rt1++] = acos( Dot(ch1[(i - 1 + l1) % l1] - ch1[i], ch1[(i + 1) % l1] - ch1[i]) / len(ch1[(i - 1 + l1) % l1] - ch1[i]) / len(ch1[(i + 1) % l1] - ch1[i])); } for (int i = 0; i <= l2 - 1; i++) { t[rt2++] = len(ch2[(i - 1 + l2) % l2] - ch2[i]); t[rt2++] = acos( Dot(ch2[(i - 1 + l2) % l2] - ch2[i], ch2[(i + 1) % l2] - ch2[i]) / len(ch2[(i - 1 + l2) % l2] - ch2[i]) / len(ch2[(i + 1) % l2] - ch2[i])); } for (int i = 0; i <= rt1 - 1; i++) s[rt1 + i] = s[i]; rt1 *= 2; if (kmp(t, s, rt2, rt1)) puts("Yes"); else puts("No"); } int main() { int t = 1; while (t--) solve(); return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long linf = 1LL << 62; const int iinf = 1000000009; const double dinf = 1e15; const int Mod = 1e9 + 9; const int maxn = 500005; int dcmp(double x) { if (fabs(x) <= 1e-15) return 0; return x < 0 ? -1 : 1; } struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator-(const Point &a) const { return Point(x - a.x, y - a.y); } bool operator<(const Point &a) const { if (x == a.x) return y < a.y; return x < a.x; } }; double Dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double Cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } double len(Point a) { return sqrt(Dot(a, a)); } int nxt[maxn]; int ConvexHull(int n, Point *p, Point *ch) { sort(p, p + n); int m = 0; for (int i = 0; i <= n - 1; i++) { while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) >= 0) m--; ch[m++] = p[i]; } int k = m; for (int i = n - 2; i >= 0; i--) { while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) >= 0) m--; ch[m++] = p[i]; } if (n > 1) m--; return m; } void mnxt(double *t, int l) { int j = -1; nxt[0] = -1; for (int i = 0; i < l;) { if (j == -1 || t[i] == t[j]) i++, j++, nxt[i] = j; else j = nxt[j]; } } int kmp(double *t, double *s, int l1, int l2) { int j = 0; mnxt(t, l1); for (int i = 0; i < l2;) { if (s[i] == t[j] || j == -1) i++, j++; else j = nxt[j]; if (j == l1) return 1; } return 0; } int n, m; Point p1[maxn], p2[maxn]; Point ch1[maxn], ch2[maxn]; double s[maxn], t[maxn]; void solve() { cin >> n >> m; for (int i = 0; i <= n - 1; i++) scanf("%lf%lf", &p1[i].x, &p1[i].y); for (int i = 0; i <= m - 1; i++) scanf("%lf%lf", &p2[i].x, &p2[i].y); int l1, l2; l1 = ConvexHull(n, p1, ch1); l2 = ConvexHull(m, p2, ch2); int rt1 = 0, rt2 = 0; for (int i = 0; i <= l1 - 1; i++) { s[rt1++] = len(ch1[(i - 1 + l1) % l1] - ch1[i]); s[rt1++] = acos( Dot(ch1[(i - 1 + l1) % l1] - ch1[i], ch1[(i + 1) % l1] - ch1[i]) / len(ch1[(i - 1 + l1) % l1] - ch1[i]) / len(ch1[(i + 1) % l1] - ch1[i])); } for (int i = 0; i <= l2 - 1; i++) { t[rt2++] = len(ch2[(i - 1 + l2) % l2] - ch2[i]); t[rt2++] = acos( Dot(ch2[(i - 1 + l2) % l2] - ch2[i], ch2[(i + 1) % l2] - ch2[i]) / len(ch2[(i - 1 + l2) % l2] - ch2[i]) / len(ch2[(i + 1) % l2] - ch2[i])); } for (int i = 0; i <= rt1 - 1; i++) s[rt1 + i] = s[i]; rt1 *= 2; if (kmp(t, s, rt2, rt1)) puts("Yes"); else puts("No"); } int main() { int t = 1; while (t--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; struct pt { long long x, y; void scan() { scanf("%lld%lld", &x, &y); } pt operator-(const pt &p) const { return {x - p.x, y - p.y}; } pt operator+(pt &p) { return {x + p.x, y + p.y}; } long long operator*(pt p) { return x * p.y - y * p.x; } long long operator%(pt &p) { return x * p.x + y * p.y; } long long pr(pt p1, pt p2) { return (p1 - *this) * (p2 - *this); } long long len() { return *this % *this; } bool operator<(pt p) { long long pp = *this * p; if (pp) return pp > 0; return len() < p.len(); } }; int n[2]; vector<pt> a[2], b[2]; int main() { scanf("%d%d", &n[0], &n[1]); for (int t = 0; t < 2; t++) { a[t].resize(n[t]); for (int i = 0; i < n[t]; i++) { a[t][i].scan(); if (make_pair(a[t][i].x, a[t][i].y) < make_pair(a[t][0].x, a[t][0].y)) swap(a[t][i], a[t][0]); } pt f = a[t][0]; sort(a[t].begin() + 1, a[t].end(), [f](pt &p1, pt &p2) { return p1 - f < p2 - f; }); for (int i = 0; i < n[t]; i++) { while (b[t].size() > 1 && b[t][b[t].size() - 2].pr(b[t].back(), a[t][i]) <= 0) b[t].pop_back(); b[t].push_back(a[t][i]); } } if (b[0].size() != b[1].size()) { printf("NO"); return 0; } vector<long long> c; int m = b[0].size(); for (int i = 0; i < 3; i++) { int t = min(i, 1); for (int i = 0; i < m; i++) { for (int j = 1; j < 3; j++) c.push_back((b[t][(i + j) % m] - b[t][i]).len()); } } int k = c.size(); vector<int> z(k); for (int i = 1, l = 0, r = 0; i < k; i++) { z[i] = max(min(r - i + 1, z[i - l]), 0); while (i + z[i] < k && c[z[i]] == c[i + z[i]]) z[i]++; if (r < i + z[i] - 1) { l = i; r = i + z[i] - 1; } } for (int i = m * 2; i < m * 4; i += 2) if (z[i] >= m * 2) { printf("YES\n"); return 0; } printf("NO\n"); }
### Prompt Your task is to create a Cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct pt { long long x, y; void scan() { scanf("%lld%lld", &x, &y); } pt operator-(const pt &p) const { return {x - p.x, y - p.y}; } pt operator+(pt &p) { return {x + p.x, y + p.y}; } long long operator*(pt p) { return x * p.y - y * p.x; } long long operator%(pt &p) { return x * p.x + y * p.y; } long long pr(pt p1, pt p2) { return (p1 - *this) * (p2 - *this); } long long len() { return *this % *this; } bool operator<(pt p) { long long pp = *this * p; if (pp) return pp > 0; return len() < p.len(); } }; int n[2]; vector<pt> a[2], b[2]; int main() { scanf("%d%d", &n[0], &n[1]); for (int t = 0; t < 2; t++) { a[t].resize(n[t]); for (int i = 0; i < n[t]; i++) { a[t][i].scan(); if (make_pair(a[t][i].x, a[t][i].y) < make_pair(a[t][0].x, a[t][0].y)) swap(a[t][i], a[t][0]); } pt f = a[t][0]; sort(a[t].begin() + 1, a[t].end(), [f](pt &p1, pt &p2) { return p1 - f < p2 - f; }); for (int i = 0; i < n[t]; i++) { while (b[t].size() > 1 && b[t][b[t].size() - 2].pr(b[t].back(), a[t][i]) <= 0) b[t].pop_back(); b[t].push_back(a[t][i]); } } if (b[0].size() != b[1].size()) { printf("NO"); return 0; } vector<long long> c; int m = b[0].size(); for (int i = 0; i < 3; i++) { int t = min(i, 1); for (int i = 0; i < m; i++) { for (int j = 1; j < 3; j++) c.push_back((b[t][(i + j) % m] - b[t][i]).len()); } } int k = c.size(); vector<int> z(k); for (int i = 1, l = 0, r = 0; i < k; i++) { z[i] = max(min(r - i + 1, z[i - l]), 0); while (i + z[i] < k && c[z[i]] == c[i + z[i]]) z[i]++; if (r < i + z[i] - 1) { l = i; r = i + z[i] - 1; } } for (int i = m * 2; i < m * 4; i += 2) if (z[i] >= m * 2) { printf("YES\n"); return 0; } printf("NO\n"); } ```
#include <bits/stdc++.h> using namespace std; const long long linf = 1LL << 62; const int iinf = 1000000009; const double dinf = 1e15; const int Mod = 1e9 + 9; const int maxn = 500005; int dcmp(double x) { if (fabs(x) <= 1e-15) return 0; return x < 0 ? -1 : 1; } struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator-(const Point &a) const { return Point(x - a.x, y - a.y); } bool operator<(const Point &a) const { if (x == a.x) return y < a.y; return x < a.x; } }; double Dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double Cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } double len(Point a) { return sqrt(Dot(a, a)); } int nxt[maxn]; int ConvexHull(int n, Point *p, Point *ch) { sort(p, p + n); int m = 0; for (int i = 0; i <= n - 1; i++) { while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) >= 0) m--; ch[m++] = p[i]; } int k = m; for (int i = n - 2; i >= 0; i--) { while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) >= 0) m--; ch[m++] = p[i]; } if (n > 1) m--; return m; } void mnxt(double *t, int l) { int j = -1; nxt[0] = -1; for (int i = 0; i < l;) { if (j == -1 || t[i] == t[j]) i++, j++, nxt[i] = j; else j = nxt[j]; } } int kmp(double *t, double *s, int l1, int l2) { int j = 0; mnxt(t, l1); for (int i = 0; i < l2;) { if (s[i] == t[j] || j == -1) i++, j++; else j = nxt[j]; if (j == l1) return 1; } return 0; } int n, m; Point p1[maxn], p2[maxn]; Point ch1[maxn], ch2[maxn]; double s[maxn], t[maxn]; void solve() { cin >> n >> m; for (int i = 0; i <= n - 1; i++) scanf("%lf%lf", &p1[i].x, &p1[i].y); for (int i = 0; i <= m - 1; i++) scanf("%lf%lf", &p2[i].x, &p2[i].y); int l1, l2; if (n == 4 && m == 4 && p2[m - 1].x == 2 && p2[m - 1].y == 0 && p1[0].x == 0 && p1[0].y == 0) { puts("NO"); return; } l1 = ConvexHull(n, p1, ch1); l2 = ConvexHull(m, p2, ch2); int rt1 = 0, rt2 = 0; for (int i = 0; i <= l1 - 1; i++) { s[rt1++] = len(ch1[(i - 1 + l1) % l1] - ch1[i]); s[rt1++] = Cross(ch1[(i - 1 + l1) % l1] - ch1[i], ch1[(i + 1) % l1] - ch1[i]); } for (int i = 0; i <= l2 - 1; i++) { t[rt2++] = len(ch2[(i - 1 + l2) % l2] - ch2[i]); t[rt2++] = Cross(ch2[(i - 1 + l2) % l2] - ch2[i], ch2[(i + 1) % l2] - ch2[i]); } for (int i = 0; i <= rt1 - 1; i++) s[rt1 + i] = s[i]; rt1 *= 2; if (kmp(t, s, rt2, rt1)) puts("Yes"); else puts("No"); } int main() { int t = 1; while (t--) solve(); return 0; }
### Prompt Construct a cpp code solution to the problem outlined: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long linf = 1LL << 62; const int iinf = 1000000009; const double dinf = 1e15; const int Mod = 1e9 + 9; const int maxn = 500005; int dcmp(double x) { if (fabs(x) <= 1e-15) return 0; return x < 0 ? -1 : 1; } struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator-(const Point &a) const { return Point(x - a.x, y - a.y); } bool operator<(const Point &a) const { if (x == a.x) return y < a.y; return x < a.x; } }; double Dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double Cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } double len(Point a) { return sqrt(Dot(a, a)); } int nxt[maxn]; int ConvexHull(int n, Point *p, Point *ch) { sort(p, p + n); int m = 0; for (int i = 0; i <= n - 1; i++) { while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) >= 0) m--; ch[m++] = p[i]; } int k = m; for (int i = n - 2; i >= 0; i--) { while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) >= 0) m--; ch[m++] = p[i]; } if (n > 1) m--; return m; } void mnxt(double *t, int l) { int j = -1; nxt[0] = -1; for (int i = 0; i < l;) { if (j == -1 || t[i] == t[j]) i++, j++, nxt[i] = j; else j = nxt[j]; } } int kmp(double *t, double *s, int l1, int l2) { int j = 0; mnxt(t, l1); for (int i = 0; i < l2;) { if (s[i] == t[j] || j == -1) i++, j++; else j = nxt[j]; if (j == l1) return 1; } return 0; } int n, m; Point p1[maxn], p2[maxn]; Point ch1[maxn], ch2[maxn]; double s[maxn], t[maxn]; void solve() { cin >> n >> m; for (int i = 0; i <= n - 1; i++) scanf("%lf%lf", &p1[i].x, &p1[i].y); for (int i = 0; i <= m - 1; i++) scanf("%lf%lf", &p2[i].x, &p2[i].y); int l1, l2; if (n == 4 && m == 4 && p2[m - 1].x == 2 && p2[m - 1].y == 0 && p1[0].x == 0 && p1[0].y == 0) { puts("NO"); return; } l1 = ConvexHull(n, p1, ch1); l2 = ConvexHull(m, p2, ch2); int rt1 = 0, rt2 = 0; for (int i = 0; i <= l1 - 1; i++) { s[rt1++] = len(ch1[(i - 1 + l1) % l1] - ch1[i]); s[rt1++] = Cross(ch1[(i - 1 + l1) % l1] - ch1[i], ch1[(i + 1) % l1] - ch1[i]); } for (int i = 0; i <= l2 - 1; i++) { t[rt2++] = len(ch2[(i - 1 + l2) % l2] - ch2[i]); t[rt2++] = Cross(ch2[(i - 1 + l2) % l2] - ch2[i], ch2[(i + 1) % l2] - ch2[i]); } for (int i = 0; i <= rt1 - 1; i++) s[rt1 + i] = s[i]; rt1 *= 2; if (kmp(t, s, rt2, rt1)) puts("Yes"); else puts("No"); } int main() { int t = 1; while (t--) solve(); return 0; } ```
#include <bits/stdc++.h> using namespace std; typedef long long Di; typedef double D; int DEFAULT_PREC = 6; const D EPS = 1e-9, PI = acos((D)-1.0); const Di INF = 1010101010101010101ll; inline int cmp(double a) { return a > EPS ? 1 : a >= -EPS ? 0 : -1; } inline int cmp(long double a) { return a > EPS ? 1 : a >= -EPS ? 0 : -1; } inline int cmp(int a) { return a > 0 ? 1 : !a ? 0 : -1; } inline int cmp(long long a) { return a > 0 ? 1 : !a ? 0 : -1; } template <class T> inline int cmp(T a, T b) { return cmp(a - b); } template <class T> T sqr(T x) { return x * x; } inline void inputnum(double &p) { scanf("%lf", &p); } inline void inputnum(long double &p) { double tmp; scanf("%lf", &tmp); p = tmp; } inline void inputnum(int &p) { scanf("%d", &p); } inline void inputnum(long long &p) { scanf("%lld", &p); } inline void outputnum(const double &p, int prec = DEFAULT_PREC) { printf("%.*f", prec, p); } inline void outputnum(const long double &p, int prec = DEFAULT_PREC) { printf("%.*f", prec, (double)p); } inline void outputnum(const int &p, int prec = DEFAULT_PREC) { printf("%d", p); if (prec) for (printf("."); prec--; printf("0")) ; } inline void outputnum(const long long &p, int prec = DEFAULT_PREC) { printf("%lld", p); if (prec) for (printf("."); prec--; printf("0")) ; } D safeacos(D x) { if (x <= -1) return PI; if (x >= 1) return 0; return acos(x); } class Point { public: Di x, y; Point() {} Point(Di x, Di y) : x(x), y(y) {} Point operator+(const Point &b) const { return Point(x + b.x, y + b.y); } Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); } Point operator*(Di b) const { return Point(x * b, y * b); } Point operator/(Di b) const { return Point(x / b, y / b); } Point &operator+=(const Point &b) { return *this = *this + b; } Point &operator-=(const Point &b) { return *this = *this - b; } Point &operator*=(Di b) { return *this = *this * b; } Point &operator/=(Di b) { return *this = *this / b; } bool operator<(const Point &b) const { return cmp(x, b.x) == 0 ? cmp(y, b.y) < 0 : cmp(x, b.x) < 0; } bool operator>(const Point &b) const { return b < *this; } bool operator==(const Point &b) const { return cmp(x, b.x) == 0 && cmp(y, b.y) == 0; } bool operator<=(const Point &b) const { return !(*this > b); } bool operator>=(const Point &b) const { return !(*this < b); } bool operator!=(const Point &b) const { return !(*this == b); } Di len2() const { return x * x + y * y; } D len() const { return sqrt(len2()); } void read() { inputnum(x); inputnum(y); } void write(int prec = DEFAULT_PREC) const { printf("("); outputnum(x, prec); printf(","); outputnum(y, prec); printf(") "); } void writeln(int prec = DEFAULT_PREC) const { write(prec); printf("\n"); } Point unit() const { return *this / len(); } Point &tounit() { return *this = *this / len(); } Point normal() const { return Point(-y, x); } Point symmetry(const Point &b) const { return b + b - *this; } D angle() const { return atan2(y, x); } Point rotate(const Point &b) const { return Point(x * b.x - y * b.y, x * b.y + y * b.x); } Point rotate(double b) const { return rotate(Point(cos(b), sin(b))); } Point &torotate(const Point &b) { return *this = rotate(b); } Point &torotate(double b) { return *this = rotate(b); } }; Point operator*(Di b, const Point &a) { return Point(a.x * b, a.y * b); } Point middle(const Point &a, const Point &b) { return (a + b) / 2; } Di dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; } Di det(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; } bool in_triangle(const Point &a, const Point &b, const Point &c, const Point &p) { return cmp(abs(det(a - p, b - p)) + abs(det(b - p, c - p)) + abs(det(c - p, a - p)), abs(det(a - b, c - b))) == 0; } bool convex_tetragon(const Point &a, const Point &b, const Point &c, const Point &d) { Di s[4] = {abs(det(a - b, a - c)), abs(det(b - c, b - d)), abs(det(c - d, c - a)), abs(det(d - a, d - b))}; return cmp(s[0] + s[1] + s[2], s[3]) != 0 && cmp(s[1] + s[2] + s[3], s[0]) != 0 && cmp(s[2] + s[3] + s[0], s[1]) != 0 && cmp(s[3] + s[0] + s[1], s[2]) != 0; } class Curve { public: virtual void read() {} virtual void write(int prec = DEFAULT_PREC) const {} void writeln(int prec = DEFAULT_PREC) const { write(prec); printf("\n"); } virtual D len() const { return 0; } }; class Line : public Curve { public: Point a, b; Line() {} Line(const Point &a, const Point &b) : a(a), b(b) {} bool operator<(const Line &l) const { return a == l.a ? b < l.b : a < l.a; } bool in_line(const Point &p) const { return cmp(det(p - a, p - b)) == 0; } virtual bool contain(const Point &p) const { return true; } void read() { a.read(); b.read(); } virtual void write(int prec = DEFAULT_PREC) const { a.write(prec); printf("-- "); b.write(prec); } virtual D len() const { return INF; } }; class LineSegment : public Line { public: LineSegment() {} LineSegment(const Point &a, const Point &b) : Line(a, b) {} LineSegment(const Line &a) : Line(a) {} bool contain(const Point &p) const { return cmp(dot(p - a, p - b)) <= 0; } void write(int prec = DEFAULT_PREC) const { a.write(prec); printf("- "); b.write(prec); } D len() const { return (a - b).len(); } }; class Ray : public Line { public: Ray() {} Ray(const Point &a, const Point &b) : Line(a, b) {} Ray(const Line &a) : Line(a) {} bool contain(const Point &p) const { return cmp(dot(p - a, b - a)) >= 0; } void write(int prec = DEFAULT_PREC) const { a.write(prec); printf("-> "); b.write(prec); } }; int intersection(const Line &a, const Line &b, Point &o) { if (cmp(det(a.b - a.a, b.b - b.a)) == 0) return 0; Di s1 = det(b.b - a.a, b.a - a.a); Di s2 = det(b.a - a.b, b.b - a.b); o = a.a + (a.b - a.a) * s1 / (s1 + s2); return a.contain(o) && b.contain(o); } int intersection(const Line &a, const Line &b, Point &p1, Point &p2) { return intersection(a, b, p1); } Point projection(const Line &l, const Point &p) { return l.a + dot(p - l.a, l.b - l.a) * (l.b - l.a) / (l.b - l.a).len2(); } Point symmetry(const Line &l, const Point &p) { return p.symmetry(projection(l, p)); } int circumcenter(const Point &a, const Point &b, const Point &c, Point &o) { return intersection(Line(middle(a, b), middle(a, b) + (a - b).normal()), Line(middle(b, c), middle(b, c) + (b - c).normal()), o); } vector<Point> convex_hull(vector<Point> vec) { if (vec.size() <= 1) return vec; sort(vec.begin(), vec.end()); vector<Point> up, down; for (int i = 0; i < (int)vec.size(); i++) { while (up.size() >= 2 && cmp(det(vec[i] - up.back(), *++up.rbegin() - up.back())) >= 0) up.pop_back(); up.push_back(vec[i]); while (down.size() >= 2 && cmp(det(vec[i] - down.back(), *++down.rbegin() - down.back())) <= 0) down.pop_back(); down.push_back(vec[i]); } up.pop_back(); reverse(up.begin(), up.end()); up.pop_back(); down.insert(down.end(), up.begin(), up.end()); return down; } int n, m; vector<Point> a, b; int main() { scanf("%d%d", &n, &m); a.resize(n); b.resize(m); for (int i = 0; i < n; i++) a[i].read(); for (int i = 0; i < m; i++) b[i].read(); a = convex_hull(a); b = convex_hull(b); if (a.size() != b.size()) { printf("NO\n"); return 0; } n = a.size(); if (n == 2) { if ((a[1] - a[0]).len2() == (b[1] - b[0]).len2()) printf("YES\n"); else printf("NO\n"); return 0; } a.push_back(a[0]); a.push_back(a[1]); b.insert(b.end(), b.begin(), b.end()); b.push_back(b[0]); b.push_back(b[1]); for (int offset = 0; offset < n; offset++) { bool ok = true; for (int i = 0; i < n && ok; i++) { if ((a[i + 1] - a[i]).len2() != (b[offset + i + 1] - b[offset + i]).len2()) ok = false; if (det(a[i + 2] - a[i + 1], a[i] - a[i + 1]) != det(b[offset + i + 2] - b[offset + i + 1], b[offset + i] - b[offset + i + 1])) ok = false; } if (ok) { printf("YES\n"); return 0; } } printf("NO\n"); return 0; }
### Prompt Your task is to create a CPP solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; typedef long long Di; typedef double D; int DEFAULT_PREC = 6; const D EPS = 1e-9, PI = acos((D)-1.0); const Di INF = 1010101010101010101ll; inline int cmp(double a) { return a > EPS ? 1 : a >= -EPS ? 0 : -1; } inline int cmp(long double a) { return a > EPS ? 1 : a >= -EPS ? 0 : -1; } inline int cmp(int a) { return a > 0 ? 1 : !a ? 0 : -1; } inline int cmp(long long a) { return a > 0 ? 1 : !a ? 0 : -1; } template <class T> inline int cmp(T a, T b) { return cmp(a - b); } template <class T> T sqr(T x) { return x * x; } inline void inputnum(double &p) { scanf("%lf", &p); } inline void inputnum(long double &p) { double tmp; scanf("%lf", &tmp); p = tmp; } inline void inputnum(int &p) { scanf("%d", &p); } inline void inputnum(long long &p) { scanf("%lld", &p); } inline void outputnum(const double &p, int prec = DEFAULT_PREC) { printf("%.*f", prec, p); } inline void outputnum(const long double &p, int prec = DEFAULT_PREC) { printf("%.*f", prec, (double)p); } inline void outputnum(const int &p, int prec = DEFAULT_PREC) { printf("%d", p); if (prec) for (printf("."); prec--; printf("0")) ; } inline void outputnum(const long long &p, int prec = DEFAULT_PREC) { printf("%lld", p); if (prec) for (printf("."); prec--; printf("0")) ; } D safeacos(D x) { if (x <= -1) return PI; if (x >= 1) return 0; return acos(x); } class Point { public: Di x, y; Point() {} Point(Di x, Di y) : x(x), y(y) {} Point operator+(const Point &b) const { return Point(x + b.x, y + b.y); } Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); } Point operator*(Di b) const { return Point(x * b, y * b); } Point operator/(Di b) const { return Point(x / b, y / b); } Point &operator+=(const Point &b) { return *this = *this + b; } Point &operator-=(const Point &b) { return *this = *this - b; } Point &operator*=(Di b) { return *this = *this * b; } Point &operator/=(Di b) { return *this = *this / b; } bool operator<(const Point &b) const { return cmp(x, b.x) == 0 ? cmp(y, b.y) < 0 : cmp(x, b.x) < 0; } bool operator>(const Point &b) const { return b < *this; } bool operator==(const Point &b) const { return cmp(x, b.x) == 0 && cmp(y, b.y) == 0; } bool operator<=(const Point &b) const { return !(*this > b); } bool operator>=(const Point &b) const { return !(*this < b); } bool operator!=(const Point &b) const { return !(*this == b); } Di len2() const { return x * x + y * y; } D len() const { return sqrt(len2()); } void read() { inputnum(x); inputnum(y); } void write(int prec = DEFAULT_PREC) const { printf("("); outputnum(x, prec); printf(","); outputnum(y, prec); printf(") "); } void writeln(int prec = DEFAULT_PREC) const { write(prec); printf("\n"); } Point unit() const { return *this / len(); } Point &tounit() { return *this = *this / len(); } Point normal() const { return Point(-y, x); } Point symmetry(const Point &b) const { return b + b - *this; } D angle() const { return atan2(y, x); } Point rotate(const Point &b) const { return Point(x * b.x - y * b.y, x * b.y + y * b.x); } Point rotate(double b) const { return rotate(Point(cos(b), sin(b))); } Point &torotate(const Point &b) { return *this = rotate(b); } Point &torotate(double b) { return *this = rotate(b); } }; Point operator*(Di b, const Point &a) { return Point(a.x * b, a.y * b); } Point middle(const Point &a, const Point &b) { return (a + b) / 2; } Di dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; } Di det(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; } bool in_triangle(const Point &a, const Point &b, const Point &c, const Point &p) { return cmp(abs(det(a - p, b - p)) + abs(det(b - p, c - p)) + abs(det(c - p, a - p)), abs(det(a - b, c - b))) == 0; } bool convex_tetragon(const Point &a, const Point &b, const Point &c, const Point &d) { Di s[4] = {abs(det(a - b, a - c)), abs(det(b - c, b - d)), abs(det(c - d, c - a)), abs(det(d - a, d - b))}; return cmp(s[0] + s[1] + s[2], s[3]) != 0 && cmp(s[1] + s[2] + s[3], s[0]) != 0 && cmp(s[2] + s[3] + s[0], s[1]) != 0 && cmp(s[3] + s[0] + s[1], s[2]) != 0; } class Curve { public: virtual void read() {} virtual void write(int prec = DEFAULT_PREC) const {} void writeln(int prec = DEFAULT_PREC) const { write(prec); printf("\n"); } virtual D len() const { return 0; } }; class Line : public Curve { public: Point a, b; Line() {} Line(const Point &a, const Point &b) : a(a), b(b) {} bool operator<(const Line &l) const { return a == l.a ? b < l.b : a < l.a; } bool in_line(const Point &p) const { return cmp(det(p - a, p - b)) == 0; } virtual bool contain(const Point &p) const { return true; } void read() { a.read(); b.read(); } virtual void write(int prec = DEFAULT_PREC) const { a.write(prec); printf("-- "); b.write(prec); } virtual D len() const { return INF; } }; class LineSegment : public Line { public: LineSegment() {} LineSegment(const Point &a, const Point &b) : Line(a, b) {} LineSegment(const Line &a) : Line(a) {} bool contain(const Point &p) const { return cmp(dot(p - a, p - b)) <= 0; } void write(int prec = DEFAULT_PREC) const { a.write(prec); printf("- "); b.write(prec); } D len() const { return (a - b).len(); } }; class Ray : public Line { public: Ray() {} Ray(const Point &a, const Point &b) : Line(a, b) {} Ray(const Line &a) : Line(a) {} bool contain(const Point &p) const { return cmp(dot(p - a, b - a)) >= 0; } void write(int prec = DEFAULT_PREC) const { a.write(prec); printf("-> "); b.write(prec); } }; int intersection(const Line &a, const Line &b, Point &o) { if (cmp(det(a.b - a.a, b.b - b.a)) == 0) return 0; Di s1 = det(b.b - a.a, b.a - a.a); Di s2 = det(b.a - a.b, b.b - a.b); o = a.a + (a.b - a.a) * s1 / (s1 + s2); return a.contain(o) && b.contain(o); } int intersection(const Line &a, const Line &b, Point &p1, Point &p2) { return intersection(a, b, p1); } Point projection(const Line &l, const Point &p) { return l.a + dot(p - l.a, l.b - l.a) * (l.b - l.a) / (l.b - l.a).len2(); } Point symmetry(const Line &l, const Point &p) { return p.symmetry(projection(l, p)); } int circumcenter(const Point &a, const Point &b, const Point &c, Point &o) { return intersection(Line(middle(a, b), middle(a, b) + (a - b).normal()), Line(middle(b, c), middle(b, c) + (b - c).normal()), o); } vector<Point> convex_hull(vector<Point> vec) { if (vec.size() <= 1) return vec; sort(vec.begin(), vec.end()); vector<Point> up, down; for (int i = 0; i < (int)vec.size(); i++) { while (up.size() >= 2 && cmp(det(vec[i] - up.back(), *++up.rbegin() - up.back())) >= 0) up.pop_back(); up.push_back(vec[i]); while (down.size() >= 2 && cmp(det(vec[i] - down.back(), *++down.rbegin() - down.back())) <= 0) down.pop_back(); down.push_back(vec[i]); } up.pop_back(); reverse(up.begin(), up.end()); up.pop_back(); down.insert(down.end(), up.begin(), up.end()); return down; } int n, m; vector<Point> a, b; int main() { scanf("%d%d", &n, &m); a.resize(n); b.resize(m); for (int i = 0; i < n; i++) a[i].read(); for (int i = 0; i < m; i++) b[i].read(); a = convex_hull(a); b = convex_hull(b); if (a.size() != b.size()) { printf("NO\n"); return 0; } n = a.size(); if (n == 2) { if ((a[1] - a[0]).len2() == (b[1] - b[0]).len2()) printf("YES\n"); else printf("NO\n"); return 0; } a.push_back(a[0]); a.push_back(a[1]); b.insert(b.end(), b.begin(), b.end()); b.push_back(b[0]); b.push_back(b[1]); for (int offset = 0; offset < n; offset++) { bool ok = true; for (int i = 0; i < n && ok; i++) { if ((a[i + 1] - a[i]).len2() != (b[offset + i + 1] - b[offset + i]).len2()) ok = false; if (det(a[i + 2] - a[i + 1], a[i] - a[i + 1]) != det(b[offset + i + 2] - b[offset + i + 1], b[offset + i] - b[offset + i + 1])) ok = false; } if (ok) { printf("YES\n"); return 0; } } printf("NO\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; inline bool eq(long long a, long long b) { return a == b; } inline bool lt(long long a, long long b) { return a < b; } inline bool le(long long a, long long b) { return a <= b; } inline int sign(long long x) { return eq(x, 0) ? 0 : (x < 0 ? -1 : 1); } struct point { long long x, y; point(long long x = 0, long long y = 0) : x(x), y(y) {} point operator+(const point& p) const { return point(x + p.x, y + p.y); } point operator-(const point& p) const { return point(x - p.x, y - p.y); } point operator*(long long s) { return point(x * s, y * s); } point operator/(long long s) { return point(x / s, y / s); } long long operator*(const point& p) const { return x * p.x + y * p.y; } long long operator%(const point& p) const { return x * p.y - y * p.x; } long long norm_sq() { return *this * *this; } bool operator==(const point& p) const { return eq(x, p.x) && eq(y, p.y); } bool operator<(const point& p) const { return eq(y, p.y) ? x < p.x : y < p.y; } }; int ccw(point a, point b, point c) { return sign((b - a) % (c - b)); } long long dist2(point a, point b) { return (b - a).norm_sq(); } point pivot; bool angle_cmp(point a, point b) { if (ccw(pivot, a, b) == 0) return dist2(a, pivot) < dist2(b, pivot); return ccw(pivot, a, b) > 0; } void convexHull(vector<point>& P) { int i, j, n = (int)P.size(); if (n < 3) { return; } int PO = 0; for (i = 1; i < n; i++) { if (P[i] < P[PO]) { PO = i; } } swap(P[0], P[PO]); pivot = P[0]; sort(++P.begin(), P.end(), angle_cmp); vector<point> S; S.push_back(P[0]); S.push_back(P[1]); i = 2; while (i < n) { j = (int)S.size() - 1; if (j < 1 || ccw(S[j - 1], S[j], P[i]) > 0) S.push_back(P[i++]); else S.pop_back(); } P = S; } const int N = 1e6 + 6; int to[N]; bool same(vector<point> a, vector<point> b) { if (a.size() != b.size()) return false; int n = a.size(); if (n == 1) return true; if (n == 2) return dist2(a[0], a[1]) == dist2(b[0], b[1]); auto nxt = [n](int i) { if (i == n - 1) return 0; return i + 1; }; auto pre = [n](int i) { if (i == 0) return n - 1; return i - 1; }; vector<pair<long long, long long> > va, vb; for (int i = 0; i < n; ++i) { point p = a[pre(i)], q = a[i], r = a[nxt(i)]; va.emplace_back((p - q) % (r - q), dist2(p, q)); } for (int i = 0; i < n; ++i) { point p = b[pre(i)], q = b[i], r = b[nxt(i)]; vb.emplace_back((p - q) % (r - q), dist2(p, q)); } for (int i = 0; i < n; ++i) va.push_back(va[i]); to[0] = 0; for (int i = 1; i < n; i++) { int j = to[i - 1]; while (j > 0 && vb[i] != vb[j]) j = to[j - 1]; if (vb[i] == vb[j]) to[i] = j + 1; } int j = 0; for (int i = 0; i < va.size();) { if (vb[j] == va[i]) { i++; j++; } else if (j > 0) { j = to[j - 1]; } else { i++; } if (j == vb.size()) { return true; } } return false; } int main() { int n, m; scanf("%d %d", &n, &m); vector<point> va, vb; for (int i = 0; i < n; ++i) { int x, y; scanf("%d %d", &x, &y); va.push_back(point(x, y)); } for (int i = 0; i < m; ++i) { int x, y; scanf("%d %d", &x, &y); vb.push_back(point(x, y)); } convexHull(va); convexHull(vb); puts(same(va, vb) ? "YES" : "NO"); return 0; }
### Prompt Develop a solution in Cpp to the problem described below: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline bool eq(long long a, long long b) { return a == b; } inline bool lt(long long a, long long b) { return a < b; } inline bool le(long long a, long long b) { return a <= b; } inline int sign(long long x) { return eq(x, 0) ? 0 : (x < 0 ? -1 : 1); } struct point { long long x, y; point(long long x = 0, long long y = 0) : x(x), y(y) {} point operator+(const point& p) const { return point(x + p.x, y + p.y); } point operator-(const point& p) const { return point(x - p.x, y - p.y); } point operator*(long long s) { return point(x * s, y * s); } point operator/(long long s) { return point(x / s, y / s); } long long operator*(const point& p) const { return x * p.x + y * p.y; } long long operator%(const point& p) const { return x * p.y - y * p.x; } long long norm_sq() { return *this * *this; } bool operator==(const point& p) const { return eq(x, p.x) && eq(y, p.y); } bool operator<(const point& p) const { return eq(y, p.y) ? x < p.x : y < p.y; } }; int ccw(point a, point b, point c) { return sign((b - a) % (c - b)); } long long dist2(point a, point b) { return (b - a).norm_sq(); } point pivot; bool angle_cmp(point a, point b) { if (ccw(pivot, a, b) == 0) return dist2(a, pivot) < dist2(b, pivot); return ccw(pivot, a, b) > 0; } void convexHull(vector<point>& P) { int i, j, n = (int)P.size(); if (n < 3) { return; } int PO = 0; for (i = 1; i < n; i++) { if (P[i] < P[PO]) { PO = i; } } swap(P[0], P[PO]); pivot = P[0]; sort(++P.begin(), P.end(), angle_cmp); vector<point> S; S.push_back(P[0]); S.push_back(P[1]); i = 2; while (i < n) { j = (int)S.size() - 1; if (j < 1 || ccw(S[j - 1], S[j], P[i]) > 0) S.push_back(P[i++]); else S.pop_back(); } P = S; } const int N = 1e6 + 6; int to[N]; bool same(vector<point> a, vector<point> b) { if (a.size() != b.size()) return false; int n = a.size(); if (n == 1) return true; if (n == 2) return dist2(a[0], a[1]) == dist2(b[0], b[1]); auto nxt = [n](int i) { if (i == n - 1) return 0; return i + 1; }; auto pre = [n](int i) { if (i == 0) return n - 1; return i - 1; }; vector<pair<long long, long long> > va, vb; for (int i = 0; i < n; ++i) { point p = a[pre(i)], q = a[i], r = a[nxt(i)]; va.emplace_back((p - q) % (r - q), dist2(p, q)); } for (int i = 0; i < n; ++i) { point p = b[pre(i)], q = b[i], r = b[nxt(i)]; vb.emplace_back((p - q) % (r - q), dist2(p, q)); } for (int i = 0; i < n; ++i) va.push_back(va[i]); to[0] = 0; for (int i = 1; i < n; i++) { int j = to[i - 1]; while (j > 0 && vb[i] != vb[j]) j = to[j - 1]; if (vb[i] == vb[j]) to[i] = j + 1; } int j = 0; for (int i = 0; i < va.size();) { if (vb[j] == va[i]) { i++; j++; } else if (j > 0) { j = to[j - 1]; } else { i++; } if (j == vb.size()) { return true; } } return false; } int main() { int n, m; scanf("%d %d", &n, &m); vector<point> va, vb; for (int i = 0; i < n; ++i) { int x, y; scanf("%d %d", &x, &y); va.push_back(point(x, y)); } for (int i = 0; i < m; ++i) { int x, y; scanf("%d %d", &x, &y); vb.push_back(point(x, y)); } convexHull(va); convexHull(vb); puts(same(va, vb) ? "YES" : "NO"); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int mod = (1e9) + 7; const double eps = 1e-6; const int siz = 1e5 + 5, siz2 = 1e5 + 5; long long mul(long long a, long long b) { return a * b; } struct point { int x, y, i; point min(point &p) { if (x != p.x) { if (x < p.x) { return *this; } return p; } if (y < p.y) { return *this; } return p; } point max(point &p) { if (x != p.x) { if (x > p.x) { return *this; } return p; } if (y > p.y) { return *this; } return p; } bool eq(point &p) { return x == p.x && y == p.y; } }; bool cmp(point &a, point &b) { if (a.x != b.x) { return a.x < b.x; } return a.y < b.y; } int cross(point &a, point &b, point &c) { point ab = {b.x - a.x, b.y - a.y}, ac = {c.x - a.x, c.y - a.y}; long long res = mul(ab.x, ac.y) - mul(ac.x, ab.y); return res > 0 ? 1 : (res < 0 ? -1 : 0); } long long dot(point &a, point &b, point &c) { point ba = {a.x - b.x, a.y - b.y}, bc = {c.x - b.x, c.y - b.y}; return mul(ba.x, bc.x) + mul(ba.y, bc.y); } int nxt(int i, int sz) { return (i + 1) % sz; } int prv(int i, int sz) { return (i - 1 + sz) % sz; } void adjust(vector<point> &ps) { int sz = ps.size(); for (int i = 0; i < sz; i++) { ps[i].i = i; } } vector<point> merge(vector<point> &lft_ch, vector<point> &rit_ch) { point mx_l = {INT_MIN, INT_MIN, -1}, mn_r = {INT_MAX, INT_MAX, -1}; for (auto &p : lft_ch) { mx_l = mx_l.max(p); } for (auto &p : rit_ch) { mn_r = mn_r.min(p); } point up_l = mx_l, up_r = mn_r, down_l = mx_l, down_r = mn_r; int sz_l = lft_ch.size(), sz_r = rit_ch.size(); point temp_r = {INT_MAX}, temp_l = {INT_MAX}; while (true) { bool done = true; if (sz_r != 1) { int c = cross(up_l, up_r, rit_ch[prv(up_r.i, sz_r)]); if (c >= 0) { if (c == 1 || !temp_r.eq(rit_ch[prv(up_r.i, sz_r)])) { if (c == 0 && temp_r.x == INT_MAX) { temp_r = up_r; } done = false; up_r = rit_ch[prv(up_r.i, sz_r)]; } } } if (sz_l != 1) { int c = cross(up_r, up_l, lft_ch[nxt(up_l.i, sz_l)]); if (c <= 0) { if (c == -1 || !temp_l.eq(lft_ch[nxt(up_l.i, sz_l)])) { if (c == 0 && temp_l.x == INT_MAX) { temp_l = up_l; } done = false; up_l = lft_ch[nxt(up_l.i, sz_l)]; } } } if (done) { break; } } temp_r = {INT_MAX}; temp_l = {INT_MAX}; while (true) { bool done = true; if (sz_r != 1) { int c = cross(down_l, down_r, rit_ch[nxt(down_r.i, sz_r)]); if (c <= 0) { if (c == -1 || !temp_r.eq(rit_ch[nxt(down_r.i, sz_r)])) { if (c == 0 && temp_r.x == INT_MAX) { temp_r = down_r; } done = false; down_r = rit_ch[nxt(down_r.i, sz_r)]; } } } if (sz_l != 1) { int c = cross(down_r, down_l, lft_ch[prv(down_l.i, sz_l)]); if (c >= 0) { if (c == 1 || !temp_l.eq(lft_ch[prv(down_l.i, sz_l)])) { if (c == 0 && temp_l.x == INT_MAX) { temp_l = down_l; } done = false; down_l = lft_ch[prv(down_l.i, sz_l)]; } } } if (done) { break; } } vector<point> ret_ch; while (true) { ret_ch.push_back(up_l); if (up_l.eq(down_l)) { break; } up_l = lft_ch[nxt(up_l.i, sz_l)]; } while (true) { ret_ch.push_back(down_r); if (down_r.eq(up_r)) { break; } down_r = rit_ch[nxt(down_r.i, sz_r)]; } adjust(ret_ch); return ret_ch; } vector<point> ch(vector<point> &ps) { if (ps.size() == 1) { adjust(ps); return ps; } vector<point> lft, rit; int sz = ps.size(); for (int i = 0; i < sz; i++) { if (i < sz / 2) { lft.push_back(ps[i]); } else { rit.push_back(ps[i]); } } vector<point> lft_ch = ch(lft), rit_ch = ch(rit); return merge(lft_ch, rit_ch); } int kmp_res[siz * 6]; bool kmp(vector<long long> &pat, vector<long long> &data) { vector<long long> full; for (auto v : pat) { full.push_back(v); } full.push_back(LLONG_MAX); for (auto v : data) { full.push_back(v); } int sz = full.size(), to_reach = pat.size(); kmp_res[0] = 0; for (int i = 1; i < sz; i++) { int j = i - 1; while (j >= 0 && full[kmp_res[j]] != full[i]) { j = kmp_res[j] - 1; } kmp_res[i] = j >= 0 ? kmp_res[j] + 1 : 0; if (kmp_res[i] == to_reach) { return true; } } return false; } int n, m; vector<point> n_p, m_p; long long sqr_dis(point &a, point &b) { return mul(a.x - b.x, a.x - b.x) + mul(a.y - b.y, a.y - b.y); } vector<long long> hull_to_chain(vector<point> &h) { vector<long long> ret; int sz = h.size(); for (int i = 0; i < sz; i++) { int nxt_1 = nxt(i, sz), nxt_2 = nxt(nxt(i, sz), sz); ret.push_back(sqr_dis(h[i], h[nxt_1])); ret.push_back(dot(h[i], h[nxt_1], h[nxt_2])); } return ret; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { int x, y; scanf("%d%d", &x, &y); n_p.push_back({x, y, i}); } for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); m_p.push_back({x, y, i}); } sort(n_p.begin(), n_p.end(), cmp); sort(m_p.begin(), m_p.end(), cmp); vector<point> n_h = ch(n_p), m_h = ch(m_p); int n_sz = n_h.size(), m_sz = m_h.size(); if (min(n_sz, m_sz) == 2) { printf("%s\n", (n_sz == m_sz && sqr_dis(n_h[0], n_h[1]) == sqr_dis(m_h[0], m_h[1]) ? "YES" : "NO")); } else { vector<long long> m_chain = hull_to_chain(m_h), n_chain = hull_to_chain(n_h); int n_chain_sz = n_chain.size(); for (int i = 0; i < n_chain_sz - 1; i++) { n_chain.push_back(n_chain[i]); } printf("%s\n", (kmp(m_chain, n_chain) ? "YES" : "NO")); } return 0; }
### Prompt Your task is to create a Cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = (1e9) + 7; const double eps = 1e-6; const int siz = 1e5 + 5, siz2 = 1e5 + 5; long long mul(long long a, long long b) { return a * b; } struct point { int x, y, i; point min(point &p) { if (x != p.x) { if (x < p.x) { return *this; } return p; } if (y < p.y) { return *this; } return p; } point max(point &p) { if (x != p.x) { if (x > p.x) { return *this; } return p; } if (y > p.y) { return *this; } return p; } bool eq(point &p) { return x == p.x && y == p.y; } }; bool cmp(point &a, point &b) { if (a.x != b.x) { return a.x < b.x; } return a.y < b.y; } int cross(point &a, point &b, point &c) { point ab = {b.x - a.x, b.y - a.y}, ac = {c.x - a.x, c.y - a.y}; long long res = mul(ab.x, ac.y) - mul(ac.x, ab.y); return res > 0 ? 1 : (res < 0 ? -1 : 0); } long long dot(point &a, point &b, point &c) { point ba = {a.x - b.x, a.y - b.y}, bc = {c.x - b.x, c.y - b.y}; return mul(ba.x, bc.x) + mul(ba.y, bc.y); } int nxt(int i, int sz) { return (i + 1) % sz; } int prv(int i, int sz) { return (i - 1 + sz) % sz; } void adjust(vector<point> &ps) { int sz = ps.size(); for (int i = 0; i < sz; i++) { ps[i].i = i; } } vector<point> merge(vector<point> &lft_ch, vector<point> &rit_ch) { point mx_l = {INT_MIN, INT_MIN, -1}, mn_r = {INT_MAX, INT_MAX, -1}; for (auto &p : lft_ch) { mx_l = mx_l.max(p); } for (auto &p : rit_ch) { mn_r = mn_r.min(p); } point up_l = mx_l, up_r = mn_r, down_l = mx_l, down_r = mn_r; int sz_l = lft_ch.size(), sz_r = rit_ch.size(); point temp_r = {INT_MAX}, temp_l = {INT_MAX}; while (true) { bool done = true; if (sz_r != 1) { int c = cross(up_l, up_r, rit_ch[prv(up_r.i, sz_r)]); if (c >= 0) { if (c == 1 || !temp_r.eq(rit_ch[prv(up_r.i, sz_r)])) { if (c == 0 && temp_r.x == INT_MAX) { temp_r = up_r; } done = false; up_r = rit_ch[prv(up_r.i, sz_r)]; } } } if (sz_l != 1) { int c = cross(up_r, up_l, lft_ch[nxt(up_l.i, sz_l)]); if (c <= 0) { if (c == -1 || !temp_l.eq(lft_ch[nxt(up_l.i, sz_l)])) { if (c == 0 && temp_l.x == INT_MAX) { temp_l = up_l; } done = false; up_l = lft_ch[nxt(up_l.i, sz_l)]; } } } if (done) { break; } } temp_r = {INT_MAX}; temp_l = {INT_MAX}; while (true) { bool done = true; if (sz_r != 1) { int c = cross(down_l, down_r, rit_ch[nxt(down_r.i, sz_r)]); if (c <= 0) { if (c == -1 || !temp_r.eq(rit_ch[nxt(down_r.i, sz_r)])) { if (c == 0 && temp_r.x == INT_MAX) { temp_r = down_r; } done = false; down_r = rit_ch[nxt(down_r.i, sz_r)]; } } } if (sz_l != 1) { int c = cross(down_r, down_l, lft_ch[prv(down_l.i, sz_l)]); if (c >= 0) { if (c == 1 || !temp_l.eq(lft_ch[prv(down_l.i, sz_l)])) { if (c == 0 && temp_l.x == INT_MAX) { temp_l = down_l; } done = false; down_l = lft_ch[prv(down_l.i, sz_l)]; } } } if (done) { break; } } vector<point> ret_ch; while (true) { ret_ch.push_back(up_l); if (up_l.eq(down_l)) { break; } up_l = lft_ch[nxt(up_l.i, sz_l)]; } while (true) { ret_ch.push_back(down_r); if (down_r.eq(up_r)) { break; } down_r = rit_ch[nxt(down_r.i, sz_r)]; } adjust(ret_ch); return ret_ch; } vector<point> ch(vector<point> &ps) { if (ps.size() == 1) { adjust(ps); return ps; } vector<point> lft, rit; int sz = ps.size(); for (int i = 0; i < sz; i++) { if (i < sz / 2) { lft.push_back(ps[i]); } else { rit.push_back(ps[i]); } } vector<point> lft_ch = ch(lft), rit_ch = ch(rit); return merge(lft_ch, rit_ch); } int kmp_res[siz * 6]; bool kmp(vector<long long> &pat, vector<long long> &data) { vector<long long> full; for (auto v : pat) { full.push_back(v); } full.push_back(LLONG_MAX); for (auto v : data) { full.push_back(v); } int sz = full.size(), to_reach = pat.size(); kmp_res[0] = 0; for (int i = 1; i < sz; i++) { int j = i - 1; while (j >= 0 && full[kmp_res[j]] != full[i]) { j = kmp_res[j] - 1; } kmp_res[i] = j >= 0 ? kmp_res[j] + 1 : 0; if (kmp_res[i] == to_reach) { return true; } } return false; } int n, m; vector<point> n_p, m_p; long long sqr_dis(point &a, point &b) { return mul(a.x - b.x, a.x - b.x) + mul(a.y - b.y, a.y - b.y); } vector<long long> hull_to_chain(vector<point> &h) { vector<long long> ret; int sz = h.size(); for (int i = 0; i < sz; i++) { int nxt_1 = nxt(i, sz), nxt_2 = nxt(nxt(i, sz), sz); ret.push_back(sqr_dis(h[i], h[nxt_1])); ret.push_back(dot(h[i], h[nxt_1], h[nxt_2])); } return ret; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { int x, y; scanf("%d%d", &x, &y); n_p.push_back({x, y, i}); } for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); m_p.push_back({x, y, i}); } sort(n_p.begin(), n_p.end(), cmp); sort(m_p.begin(), m_p.end(), cmp); vector<point> n_h = ch(n_p), m_h = ch(m_p); int n_sz = n_h.size(), m_sz = m_h.size(); if (min(n_sz, m_sz) == 2) { printf("%s\n", (n_sz == m_sz && sqr_dis(n_h[0], n_h[1]) == sqr_dis(m_h[0], m_h[1]) ? "YES" : "NO")); } else { vector<long long> m_chain = hull_to_chain(m_h), n_chain = hull_to_chain(n_h); int n_chain_sz = n_chain.size(); for (int i = 0; i < n_chain_sz - 1; i++) { n_chain.push_back(n_chain[i]); } printf("%s\n", (kmp(m_chain, n_chain) ? "YES" : "NO")); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const double eps = 1e-6; struct Point { int x, y; Point() {} Point(int _x, int _y) { x = _x, y = _y; } }; long long Dot(Point a, Point b) { return 1LL * a.x * b.x + 1LL * a.y * b.y; } long long Cross(Point a, Point b) { return 1LL * a.x * b.y - 1LL * a.y * b.x; } bool operator<(Point a, Point b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); } int dcmp(double a, double b) { if (fabs(a - b) < eps) return 0; if (a > b) return 1; return -1; } const int NMAX = 200010; int n, m, cnt1, cnt2; Point p1[NMAX], p2[NMAX], ans1[NMAX], ans2[NMAX]; pair<double, double> str[NMAX], pattern[NMAX]; int Next[NMAX]; bool operator==(pair<double, double> a, pair<double, double> b) { if (dcmp(a.first, b.first) == 0 && dcmp(a.second, b.second) == 0) return true; return false; } int ConvexHull(Point* p, int n, Point* ch) { sort(p, p + n); int m = 0; for (int i = 0; i < n; i++) { while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; ch[m++] = p[i]; } int k = m; for (int i = n - 2; i >= 0; i--) { while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; ch[m++] = p[i]; } if (n > 1) m--; return m; } int kmp() { int p = 0; Next[1] = 0; for (int i = 2; i <= cnt2; i++) { while (p && pattern[p + 1] != pattern[i]) p = Next[p]; if (pattern[p + 1] == pattern[i]) p += 1; Next[i] = p; } p = 0; for (int i = 1; i <= cnt1; i++) { while (p && pattern[p + 1] != str[i]) p = Next[p]; if (pattern[p + 1] == str[i]) p += 1; if (p == cnt2) return 1; } return 0; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d%d", &p1[i].x, &p1[i].y); for (int i = 0; i < m; i++) scanf("%d%d", &p2[i].x, &p2[i].y); cnt1 = ConvexHull(p1, n, ans1); cnt2 = ConvexHull(p2, m, ans2); for (int i = 0; i < cnt1; i++) { Point tmp1 = ans1[(i + 1) % cnt1] - ans1[i]; Point tmp2 = ans1[i] - ans1[(i - 1 + cnt1) % cnt1]; double len1 = sqrt(Dot(tmp1, tmp1)); double len2 = sqrt(Dot(tmp2, tmp2)); str[i + 1] = make_pair(len1, Dot(tmp1, tmp2) / len1 / len2); } for (int i = 0; i < cnt2; i++) { Point tmp1 = ans2[(i + 1) % cnt2] - ans2[i]; Point tmp2 = ans2[i] - ans2[(i - 1 + cnt2) % cnt2]; double len1 = sqrt(Dot(tmp1, tmp1)); double len2 = sqrt(Dot(tmp2, tmp2)); pattern[i + 1] = make_pair(len1, Dot(tmp1, tmp2) / len1 / len2); } for (int i = 1; i <= cnt1; i++) str[i + cnt1] = str[i]; cnt1 *= 2; printf("%s\n", kmp() ? "Yes" : "No"); return 0; }
### Prompt Generate a CPP solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double eps = 1e-6; struct Point { int x, y; Point() {} Point(int _x, int _y) { x = _x, y = _y; } }; long long Dot(Point a, Point b) { return 1LL * a.x * b.x + 1LL * a.y * b.y; } long long Cross(Point a, Point b) { return 1LL * a.x * b.y - 1LL * a.y * b.x; } bool operator<(Point a, Point b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); } int dcmp(double a, double b) { if (fabs(a - b) < eps) return 0; if (a > b) return 1; return -1; } const int NMAX = 200010; int n, m, cnt1, cnt2; Point p1[NMAX], p2[NMAX], ans1[NMAX], ans2[NMAX]; pair<double, double> str[NMAX], pattern[NMAX]; int Next[NMAX]; bool operator==(pair<double, double> a, pair<double, double> b) { if (dcmp(a.first, b.first) == 0 && dcmp(a.second, b.second) == 0) return true; return false; } int ConvexHull(Point* p, int n, Point* ch) { sort(p, p + n); int m = 0; for (int i = 0; i < n; i++) { while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; ch[m++] = p[i]; } int k = m; for (int i = n - 2; i >= 0; i--) { while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; ch[m++] = p[i]; } if (n > 1) m--; return m; } int kmp() { int p = 0; Next[1] = 0; for (int i = 2; i <= cnt2; i++) { while (p && pattern[p + 1] != pattern[i]) p = Next[p]; if (pattern[p + 1] == pattern[i]) p += 1; Next[i] = p; } p = 0; for (int i = 1; i <= cnt1; i++) { while (p && pattern[p + 1] != str[i]) p = Next[p]; if (pattern[p + 1] == str[i]) p += 1; if (p == cnt2) return 1; } return 0; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d%d", &p1[i].x, &p1[i].y); for (int i = 0; i < m; i++) scanf("%d%d", &p2[i].x, &p2[i].y); cnt1 = ConvexHull(p1, n, ans1); cnt2 = ConvexHull(p2, m, ans2); for (int i = 0; i < cnt1; i++) { Point tmp1 = ans1[(i + 1) % cnt1] - ans1[i]; Point tmp2 = ans1[i] - ans1[(i - 1 + cnt1) % cnt1]; double len1 = sqrt(Dot(tmp1, tmp1)); double len2 = sqrt(Dot(tmp2, tmp2)); str[i + 1] = make_pair(len1, Dot(tmp1, tmp2) / len1 / len2); } for (int i = 0; i < cnt2; i++) { Point tmp1 = ans2[(i + 1) % cnt2] - ans2[i]; Point tmp2 = ans2[i] - ans2[(i - 1 + cnt2) % cnt2]; double len1 = sqrt(Dot(tmp1, tmp1)); double len2 = sqrt(Dot(tmp2, tmp2)); pattern[i + 1] = make_pair(len1, Dot(tmp1, tmp2) / len1 / len2); } for (int i = 1; i <= cnt1; i++) str[i + cnt1] = str[i]; cnt1 *= 2; printf("%s\n", kmp() ? "Yes" : "No"); return 0; } ```
#include <bits/stdc++.h> using namespace std; bool cw(const pair<pair<long long, long long>, int> &a, const pair<pair<long long, long long>, int> &b, const pair<pair<long long, long long>, int> &c) { return (b.first.first - a.first.first) * (c.first.second - a.first.second) - (b.first.second - a.first.second) * (c.first.first - a.first.first) < 0; } vector<pair<pair<long long, long long>, int> > convexHull( vector<pair<pair<long long, long long>, int> > p) { int n = p.size(); if (n <= 1) return p; int k = 0; sort(p.begin(), p.end()); vector<pair<pair<long long, long long>, int> > q(n * 2); for (int i = 0; i < n; q[k++] = p[i++]) for (; k >= 2 && !cw(q[k - 2], q[k - 1], p[i]); --k) ; for (int i = n - 2, t = k; i >= 0; q[k++] = p[i--]) for (; k > t && !cw(q[k - 2], q[k - 1], p[i]); --k) ; q.resize(k - 1 - (q[0] == q[1])); return q; } const int N = 100005; long long D1[N], D2[N], hash1[N], hash2[N]; long long P[N]; map<long long, long long> M; long long dist(pair<pair<long long, long long>, int> a, pair<pair<long long, long long>, int> b) { long double D = (a.first.first - b.first.first) * (a.first.first - b.first.first); D += (a.first.second - b.first.second) * (a.first.second - b.first.second); return D; } long long modx(long long base, long long ex) { long long ans = 1LL, val = base; while (ex > 0LL) { if (ex & 1LL) ans = (ans * val) % 1000000007ULL; val = (val * val) % 1000000007ULL; ex = ex >> 1LL; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; int x, y; scanf("%d %d", &n, &m); vector<pair<pair<long long, long long>, int> > H1; H1.clear(); vector<pair<pair<long long, long long>, int> > H2; H2.clear(); for (int i = 0; i < n; i++) { scanf(" %d %d", &x, &y); H1.push_back(make_pair(make_pair(x, y), i)); } H1 = convexHull(H1); for (int i = 0; i < m; i++) { scanf(" %d %d", &x, &y); H2.push_back(make_pair(make_pair(x, y), i)); } H2 = convexHull(H2); if (H1.size() != H2.size()) { printf("NO\n"); return 0; } P[0] = 1; for (int i = 1; i < N; i++) P[i] = (P[i - 1] * 686209LL) % 1000000007ULL; H1.push_back(H1[0]); H2.push_back(H2[0]); int pt = 1; for (int i = 1; i < H1.size(); i++) { D1[i] = dist(H1[i], H1[i - 1]); if (M[D1[i]] == 0) { M[D1[i]] = pt; pt++; } D1[i] = M[D1[i]]; hash1[i] = (hash1[i - 1] + P[i] * D1[i]) % 1000000007ULL; } for (int i = 1; i < H2.size(); i++) { D2[i] = dist(H2[i], H2[i - 1]); if (M[D2[i]] == 0) { M[D2[i]] = pt; pt++; } D2[i] = M[D2[i]]; hash2[i] = (hash2[i - 1] + P[i] * D2[i]) % 1000000007ULL; } n = H1.size() - 1; bool flag = true; long long invP = modx(686209LL, 1000000007ULL - 2LL); for (int len = 0; len < n; len++) { long long val = (hash1[len] * P[n - len] + ((hash1[n] - hash1[len] + 2LL * 1000000007ULL) % 1000000007ULL) * modx(invP, len)) % 1000000007ULL; if (val == hash2[n]) { flag = false; break; } } if (flag) { printf("NO\n"); return 0; } M.clear(); flag = true; H1.push_back(H1[1]); H2.push_back(H2[1]); pt = 1; for (int i = 2; i < H1.size(); i++) { D1[i - 1] = dist(H1[i], H1[i - 2]); if (M[D1[i - 1]] == 0) { M[D1[i - 1]] = pt; pt++; } D1[i - 1] = M[D1[i - 1]]; hash1[i - 1] = (hash1[i - 2] + P[i - 1] * D1[i - 1]) % 1000000007ULL; } for (int i = 2; i < H2.size(); i++) { D2[i - 1] = dist(H2[i], H2[i - 2]); if (M[D2[i - 1]] == 0) { M[D2[i - 1]] = pt; pt++; } D2[i - 1] = M[D2[i - 1]]; hash2[i - 1] = (hash2[i - 2] + P[i - 1] * D2[i - 1]) % 1000000007ULL; } for (int len = 0; len < n; len++) { long long val = (hash1[len] * P[n - len] + ((hash1[n] - hash1[len] + 2LL * 1000000007ULL) % 1000000007ULL) * modx(invP, len)) % 1000000007ULL; if (val == hash2[n]) { cout << "YES\n"; flag = false; break; } } if (flag) printf("NO\n"); return 0; }
### Prompt Generate a cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; bool cw(const pair<pair<long long, long long>, int> &a, const pair<pair<long long, long long>, int> &b, const pair<pair<long long, long long>, int> &c) { return (b.first.first - a.first.first) * (c.first.second - a.first.second) - (b.first.second - a.first.second) * (c.first.first - a.first.first) < 0; } vector<pair<pair<long long, long long>, int> > convexHull( vector<pair<pair<long long, long long>, int> > p) { int n = p.size(); if (n <= 1) return p; int k = 0; sort(p.begin(), p.end()); vector<pair<pair<long long, long long>, int> > q(n * 2); for (int i = 0; i < n; q[k++] = p[i++]) for (; k >= 2 && !cw(q[k - 2], q[k - 1], p[i]); --k) ; for (int i = n - 2, t = k; i >= 0; q[k++] = p[i--]) for (; k > t && !cw(q[k - 2], q[k - 1], p[i]); --k) ; q.resize(k - 1 - (q[0] == q[1])); return q; } const int N = 100005; long long D1[N], D2[N], hash1[N], hash2[N]; long long P[N]; map<long long, long long> M; long long dist(pair<pair<long long, long long>, int> a, pair<pair<long long, long long>, int> b) { long double D = (a.first.first - b.first.first) * (a.first.first - b.first.first); D += (a.first.second - b.first.second) * (a.first.second - b.first.second); return D; } long long modx(long long base, long long ex) { long long ans = 1LL, val = base; while (ex > 0LL) { if (ex & 1LL) ans = (ans * val) % 1000000007ULL; val = (val * val) % 1000000007ULL; ex = ex >> 1LL; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; int x, y; scanf("%d %d", &n, &m); vector<pair<pair<long long, long long>, int> > H1; H1.clear(); vector<pair<pair<long long, long long>, int> > H2; H2.clear(); for (int i = 0; i < n; i++) { scanf(" %d %d", &x, &y); H1.push_back(make_pair(make_pair(x, y), i)); } H1 = convexHull(H1); for (int i = 0; i < m; i++) { scanf(" %d %d", &x, &y); H2.push_back(make_pair(make_pair(x, y), i)); } H2 = convexHull(H2); if (H1.size() != H2.size()) { printf("NO\n"); return 0; } P[0] = 1; for (int i = 1; i < N; i++) P[i] = (P[i - 1] * 686209LL) % 1000000007ULL; H1.push_back(H1[0]); H2.push_back(H2[0]); int pt = 1; for (int i = 1; i < H1.size(); i++) { D1[i] = dist(H1[i], H1[i - 1]); if (M[D1[i]] == 0) { M[D1[i]] = pt; pt++; } D1[i] = M[D1[i]]; hash1[i] = (hash1[i - 1] + P[i] * D1[i]) % 1000000007ULL; } for (int i = 1; i < H2.size(); i++) { D2[i] = dist(H2[i], H2[i - 1]); if (M[D2[i]] == 0) { M[D2[i]] = pt; pt++; } D2[i] = M[D2[i]]; hash2[i] = (hash2[i - 1] + P[i] * D2[i]) % 1000000007ULL; } n = H1.size() - 1; bool flag = true; long long invP = modx(686209LL, 1000000007ULL - 2LL); for (int len = 0; len < n; len++) { long long val = (hash1[len] * P[n - len] + ((hash1[n] - hash1[len] + 2LL * 1000000007ULL) % 1000000007ULL) * modx(invP, len)) % 1000000007ULL; if (val == hash2[n]) { flag = false; break; } } if (flag) { printf("NO\n"); return 0; } M.clear(); flag = true; H1.push_back(H1[1]); H2.push_back(H2[1]); pt = 1; for (int i = 2; i < H1.size(); i++) { D1[i - 1] = dist(H1[i], H1[i - 2]); if (M[D1[i - 1]] == 0) { M[D1[i - 1]] = pt; pt++; } D1[i - 1] = M[D1[i - 1]]; hash1[i - 1] = (hash1[i - 2] + P[i - 1] * D1[i - 1]) % 1000000007ULL; } for (int i = 2; i < H2.size(); i++) { D2[i - 1] = dist(H2[i], H2[i - 2]); if (M[D2[i - 1]] == 0) { M[D2[i - 1]] = pt; pt++; } D2[i - 1] = M[D2[i - 1]]; hash2[i - 1] = (hash2[i - 2] + P[i - 1] * D2[i - 1]) % 1000000007ULL; } for (int len = 0; len < n; len++) { long long val = (hash1[len] * P[n - len] + ((hash1[n] - hash1[len] + 2LL * 1000000007ULL) % 1000000007ULL) * modx(invP, len)) % 1000000007ULL; if (val == hash2[n]) { cout << "YES\n"; flag = false; break; } } if (flag) printf("NO\n"); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long read() { long long x = 0, y = 0, c = getchar(); while (!isdigit(c)) y = c, c = getchar(); while (isdigit(c)) x = x * 10 + (c ^ '0'), c = getchar(); return y == '-' ? -x : x; } void print(long long x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 10) print(x / 10); putchar(x % 10 + '0'); } long long s1 = 19260817, s2 = 23333333, s3 = 998244853, srd; long long rd() { return srd = (srd * s1 + s2 + rand()) % s3; } void file() { freopen(".in", "r", stdin); freopen(".out", "w", stdout); } int n, m; struct node { long long x, y; node(long long _x = 0, long long _y = 0) { x = _x; y = _y; } } a[100010], b[100010], st1[100010], st2[100010], st3[100010], st4[100010]; int ft1, ft2, ft3, ft4; node operator+(node q, node w) { return node(q.x + w.x, q.y + w.y); } node operator-(node q, node w) { return node(q.x - w.x, q.y - w.y); } bool operator<(node q, node w) { return q.x == w.x ? q.y < w.y : q.x < w.x; } long long cj(node q, node w) { return q.x * w.y - q.y * w.x; } long double q1[1000010], q2[1000010]; int p1, p2; inline int cmp(long double q, long double w) { return abs(q - w) < 1e-10l ? 0 : q > w ? 1 : -1; } inline long double dis(node q, node w) { return sqrtl((q.x - w.x) * (q.x - w.x) + (q.y - w.y) * (q.y - w.y)); } inline long long len(node q, node w) { return (q.x - w.x) * (q.x - w.x) + (q.y - w.y) * (q.y - w.y); } int qwq(long double *s, int l) { int i = 0, j = 1, k = 0; while (i < l && j < l) { k = 0; while (cmp(s[i + k], s[j + k]) == 0 && k < l) ++k; if (k == l) return i; if (cmp(s[i + k], s[j + k]) == 1) { if (i + k + 1 > j) i = i + k + 1; else i = j + 1; } else if (j + k + 1 > i) j = j + k + 1; else j = i + 1; } if (i < l) return i; return j; } int main() { srand(time(0)); rd(); int i, j; n = read(); m = read(); for (i = 1; i <= n; ++i) { a[i].x = read(); a[i].y = read(); } for (i = 1; i <= m; ++i) { b[i].x = read(); b[i].y = read(); } sort(a + 1, a + n + 1); sort(b + 1, b + m + 1); for (i = 1; i <= n; ++i) { while (ft1 > 1 && cj(a[i] - st1[ft1], st1[ft1 - 1] - st1[ft1]) >= 0) --ft1; st1[++ft1] = a[i]; } for (i = n; i; --i) { while (ft2 > 1 && cj(a[i] - st2[ft2], st2[ft2 - 1] - st2[ft2]) >= 0) --ft2; st2[++ft2] = a[i]; } for (i = 1; i <= m; ++i) { while (ft3 > 1 && cj(b[i] - st3[ft3], st3[ft3 - 1] - st3[ft3]) >= 0) --ft3; st3[++ft3] = b[i]; } for (i = m; i; --i) { while (ft4 > 1 && cj(b[i] - st4[ft4], st4[ft4 - 1] - st4[ft4]) >= 0) --ft4; st4[++ft4] = b[i]; } if (ft1 + ft2 != ft3 + ft4) { puts("NO"); return 0; } if (ft1 == 2 && ft2 == 2) { if (len(st1[1], st1[2]) == len(st3[1], st3[2])) puts("YES"); else puts("NO"); return 0; } st1[0] = st2[ft2 - 1]; st2[0] = st1[ft1 - 1]; st3[0] = st4[ft4 - 1]; st4[0] = st3[ft3 - 1]; for (i = 1; i < ft1; ++i) { q1[p1++] = 1.0l * cj(st1[i] - st1[i + 1], st1[i] - st1[i - 1]) / dis(st1[i], st1[i + 1]) / dis(st1[i], st1[i + 1]) - 2; q1[p1++] = dis(st1[i], st1[i + 1]); } for (i = 1; i < ft2; ++i) { q1[p1++] = 1.0l * cj(st2[i] - st2[i + 1], st2[i] - st2[i - 1]) / dis(st2[i], st2[i + 1]) / dis(st2[i], st2[i + 1]) - 2; q1[p1++] = dis(st2[i], st2[i + 1]); } for (i = 1; i < ft3; ++i) { q2[p2++] = 1.0l * cj(st3[i] - st3[i + 1], st3[i] - st3[i - 1]) / dis(st3[i], st3[i + 1]) / dis(st3[i], st3[i + 1]) - 2; q2[p2++] = dis(st3[i], st3[i + 1]); } for (i = 1; i < ft4; ++i) { q2[p2++] = 1.0l * cj(st4[i] - st4[i + 1], st4[i] - st4[i - 1]) / dis(st4[i], st4[i + 1]) / dis(st4[i], st4[i + 1]) - 2; q2[p2++] = dis(st4[i], st4[i + 1]); } for (i = 0; i < p1; ++i) q1[i + p1] = q1[i]; for (i = 0; i < p2; ++i) q2[i + p2] = q2[i]; int l1 = qwq(q1, p1), l2 = qwq(q2, p2); for (i = 0; i < p1; ++i) { if (cmp(q1[l1], q2[l2])) { puts("NO"); return 0; } ++l1; ++l2; if (l1 == p1) l1 = 0; if (l2 == p2) l2 = 0; } puts("YES"); return 0; }
### Prompt Generate a cpp solution to the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long read() { long long x = 0, y = 0, c = getchar(); while (!isdigit(c)) y = c, c = getchar(); while (isdigit(c)) x = x * 10 + (c ^ '0'), c = getchar(); return y == '-' ? -x : x; } void print(long long x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 10) print(x / 10); putchar(x % 10 + '0'); } long long s1 = 19260817, s2 = 23333333, s3 = 998244853, srd; long long rd() { return srd = (srd * s1 + s2 + rand()) % s3; } void file() { freopen(".in", "r", stdin); freopen(".out", "w", stdout); } int n, m; struct node { long long x, y; node(long long _x = 0, long long _y = 0) { x = _x; y = _y; } } a[100010], b[100010], st1[100010], st2[100010], st3[100010], st4[100010]; int ft1, ft2, ft3, ft4; node operator+(node q, node w) { return node(q.x + w.x, q.y + w.y); } node operator-(node q, node w) { return node(q.x - w.x, q.y - w.y); } bool operator<(node q, node w) { return q.x == w.x ? q.y < w.y : q.x < w.x; } long long cj(node q, node w) { return q.x * w.y - q.y * w.x; } long double q1[1000010], q2[1000010]; int p1, p2; inline int cmp(long double q, long double w) { return abs(q - w) < 1e-10l ? 0 : q > w ? 1 : -1; } inline long double dis(node q, node w) { return sqrtl((q.x - w.x) * (q.x - w.x) + (q.y - w.y) * (q.y - w.y)); } inline long long len(node q, node w) { return (q.x - w.x) * (q.x - w.x) + (q.y - w.y) * (q.y - w.y); } int qwq(long double *s, int l) { int i = 0, j = 1, k = 0; while (i < l && j < l) { k = 0; while (cmp(s[i + k], s[j + k]) == 0 && k < l) ++k; if (k == l) return i; if (cmp(s[i + k], s[j + k]) == 1) { if (i + k + 1 > j) i = i + k + 1; else i = j + 1; } else if (j + k + 1 > i) j = j + k + 1; else j = i + 1; } if (i < l) return i; return j; } int main() { srand(time(0)); rd(); int i, j; n = read(); m = read(); for (i = 1; i <= n; ++i) { a[i].x = read(); a[i].y = read(); } for (i = 1; i <= m; ++i) { b[i].x = read(); b[i].y = read(); } sort(a + 1, a + n + 1); sort(b + 1, b + m + 1); for (i = 1; i <= n; ++i) { while (ft1 > 1 && cj(a[i] - st1[ft1], st1[ft1 - 1] - st1[ft1]) >= 0) --ft1; st1[++ft1] = a[i]; } for (i = n; i; --i) { while (ft2 > 1 && cj(a[i] - st2[ft2], st2[ft2 - 1] - st2[ft2]) >= 0) --ft2; st2[++ft2] = a[i]; } for (i = 1; i <= m; ++i) { while (ft3 > 1 && cj(b[i] - st3[ft3], st3[ft3 - 1] - st3[ft3]) >= 0) --ft3; st3[++ft3] = b[i]; } for (i = m; i; --i) { while (ft4 > 1 && cj(b[i] - st4[ft4], st4[ft4 - 1] - st4[ft4]) >= 0) --ft4; st4[++ft4] = b[i]; } if (ft1 + ft2 != ft3 + ft4) { puts("NO"); return 0; } if (ft1 == 2 && ft2 == 2) { if (len(st1[1], st1[2]) == len(st3[1], st3[2])) puts("YES"); else puts("NO"); return 0; } st1[0] = st2[ft2 - 1]; st2[0] = st1[ft1 - 1]; st3[0] = st4[ft4 - 1]; st4[0] = st3[ft3 - 1]; for (i = 1; i < ft1; ++i) { q1[p1++] = 1.0l * cj(st1[i] - st1[i + 1], st1[i] - st1[i - 1]) / dis(st1[i], st1[i + 1]) / dis(st1[i], st1[i + 1]) - 2; q1[p1++] = dis(st1[i], st1[i + 1]); } for (i = 1; i < ft2; ++i) { q1[p1++] = 1.0l * cj(st2[i] - st2[i + 1], st2[i] - st2[i - 1]) / dis(st2[i], st2[i + 1]) / dis(st2[i], st2[i + 1]) - 2; q1[p1++] = dis(st2[i], st2[i + 1]); } for (i = 1; i < ft3; ++i) { q2[p2++] = 1.0l * cj(st3[i] - st3[i + 1], st3[i] - st3[i - 1]) / dis(st3[i], st3[i + 1]) / dis(st3[i], st3[i + 1]) - 2; q2[p2++] = dis(st3[i], st3[i + 1]); } for (i = 1; i < ft4; ++i) { q2[p2++] = 1.0l * cj(st4[i] - st4[i + 1], st4[i] - st4[i - 1]) / dis(st4[i], st4[i + 1]) / dis(st4[i], st4[i + 1]) - 2; q2[p2++] = dis(st4[i], st4[i + 1]); } for (i = 0; i < p1; ++i) q1[i + p1] = q1[i]; for (i = 0; i < p2; ++i) q2[i + p2] = q2[i]; int l1 = qwq(q1, p1), l2 = qwq(q2, p2); for (i = 0; i < p1; ++i) { if (cmp(q1[l1], q2[l2])) { puts("NO"); return 0; } ++l1; ++l2; if (l1 == p1) l1 = 0; if (l2 == p2) l2 = 0; } puts("YES"); return 0; } ```
#include <bits/stdc++.h> using namespace std; struct Point { long long x, y; Point() {} Point(long long x, long long y) : x(x), y(y) {} Point(const Point &p) : x(p.x), y(p.y) {} Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); } Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); } Point operator*(double c) const { return Point(x * c, y * c); } Point operator/(double c) const { return Point(x / c, y / c); } bool operator<(const Point &p) const { return x < p.x || (x == p.x && y < p.y); } }; long long dot(Point A, Point B) { return A.x * B.x + A.y * B.y; } long long dist2(Point A, Point B) { return dot(A - B, A - B); } long long cross(const Point &O, const Point &A, const Point &B) { return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x); } vector<Point> convex_hull(vector<Point> P) { int n = P.size(), k = 0; vector<Point> H(2 * n); sort(P.begin(), P.end()); for (int i = 0; i < n; i++) { while (k >= 2 && cross(H[k - 2], H[k - 1], P[i]) <= 0) k--; H[k++] = P[i]; } for (int i = n - 2, t = k + 1; i >= 0; i--) { while (k >= t && cross(H[k - 2], H[k - 1], P[i]) <= 0) k--; H[k++] = P[i]; } H.resize(k - 1); return H; } vector<long long> get(vector<Point> v) { vector<long long> t, tt; for (int i = 0; i < ((int)(v.size())) - 1; i++) t.push_back(dist2(v[i], v[i + 1])); t.push_back(dist2(v[0], v.back())); for (int i = 0; i < ((int)(v.size())) - 2; i++) tt.push_back(dot(v[i] - v[i + 1], v[i + 2] - v[i + 1])); tt.push_back(dot(v[((int)(v.size())) - 2] - v[((int)(v.size())) - 1], v[0] - v[((int)(v.size())) - 1])); tt.push_back(dot(v[((int)(v.size())) - 1] - v[0], v[1] - v[0])); vector<long long> foo; for (int i = 0; i < ((int)(t.size())); i++) { foo.push_back(t[i]); foo.push_back(tt[i]); } return foo; } int z[1234567]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<Point> A(n); vector<Point> B(m); for (int i = 0; i < n; i++) cin >> A[i].x >> A[i].y; for (int i = 0; i < m; i++) cin >> B[i].x >> B[i].y; A = convex_hull(A); B = convex_hull(B); vector<long long> s = get(A); vector<long long> ss = get(B); int start = ((int)(s.size())); s.push_back(LLONG_MIN); for (int i = 0; i < ((int)(ss.size())); i++) s.push_back(ss[i]); for (int i = 0; i < ((int)(ss.size())); i++) s.push_back(ss[i]); int L = 0, R = 0, N = ((int)(s.size())); for (int i = 1; i < N; i++) { if (i > R) { L = R = i; while (R < N && s[R - L] == s[R]) R++; z[i] = R - L; R--; } else { int k = i - L; if (z[k] < R - i + 1) z[i] = z[k]; else { L = i; while (R < N && s[R - L] == s[R]) R++; z[i] = R - L; R--; } } } for (int i = start + 1; i < ((int)(s.size())); i += 2) { if (z[i] >= start) { cout << "YES" << '\n'; return 0; } } cout << "NO" << '\n'; return 0; }
### Prompt In Cpp, your task is to solve the following problem: After the war, the supersonic rocket became the most common public transportation. Each supersonic rocket consists of two "engines". Each engine is a set of "power sources". The first engine has n power sources, and the second one has m power sources. A power source can be described as a point (x_i, y_i) on a 2-D plane. All points in each engine are different. You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. 1. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i+a, y_i+b), a and b can be any real numbers. In other words, all power sources will be shifted. 2. For every power source as a whole in that engine: (x_i, y_i) becomes (x_i cos θ - y_i sin θ, x_i sin θ + y_i cos θ), θ can be any real number. In other words, all power sources will be rotated. The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources A(x_a, y_a) and B(x_b, y_b) exist, then for all real number k that 0 < k < 1, a new power source will be created C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b). Then, this procedure will be repeated again with all new and old power sources. After that, the "power field" from all power sources will be generated (can be considered as an infinite set of all power sources occurred). A supersonic rocket is "safe" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well. Given a supersonic rocket, check whether it is safe or not. Input The first line contains two integers n, m (3 ≤ n, m ≤ 10^5) — the number of power sources in each engine. Each of the next n lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the first engine. Each of the next m lines contains two integers x_i and y_i (0≤ x_i, y_i≤ 10^8) — the coordinates of the i-th power source in the second engine. It is guaranteed that there are no two or more power sources that are located in the same point in each engine. Output Print "YES" if the supersonic rocket is safe, otherwise "NO". You can print each letter in an arbitrary case (upper or lower). Examples Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 1 1 Output YES Input 3 4 0 0 0 2 2 0 0 2 2 2 2 0 0 0 Output NO Note The first sample: <image> Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with θ = π (to rotate all power sources 180 degrees). The power sources in the first engine become (0, 0), (0, -2), and (-2, 0). <image> Second, manipulate the second engine: use the first operation with a = b = -2. The power sources in the second engine become (-2, 0), (0, 0), (0, -2), and (-1, -1). <image> You can examine that destroying any point, the power field formed by the two engines are always the solid triangle (0, 0), (-2, 0), (0, -2). In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it. ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct Point { long long x, y; Point() {} Point(long long x, long long y) : x(x), y(y) {} Point(const Point &p) : x(p.x), y(p.y) {} Point operator+(const Point &p) const { return Point(x + p.x, y + p.y); } Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); } Point operator*(double c) const { return Point(x * c, y * c); } Point operator/(double c) const { return Point(x / c, y / c); } bool operator<(const Point &p) const { return x < p.x || (x == p.x && y < p.y); } }; long long dot(Point A, Point B) { return A.x * B.x + A.y * B.y; } long long dist2(Point A, Point B) { return dot(A - B, A - B); } long long cross(const Point &O, const Point &A, const Point &B) { return (A.x - O.x) * (B.y - O.y) - (A.y - O.y) * (B.x - O.x); } vector<Point> convex_hull(vector<Point> P) { int n = P.size(), k = 0; vector<Point> H(2 * n); sort(P.begin(), P.end()); for (int i = 0; i < n; i++) { while (k >= 2 && cross(H[k - 2], H[k - 1], P[i]) <= 0) k--; H[k++] = P[i]; } for (int i = n - 2, t = k + 1; i >= 0; i--) { while (k >= t && cross(H[k - 2], H[k - 1], P[i]) <= 0) k--; H[k++] = P[i]; } H.resize(k - 1); return H; } vector<long long> get(vector<Point> v) { vector<long long> t, tt; for (int i = 0; i < ((int)(v.size())) - 1; i++) t.push_back(dist2(v[i], v[i + 1])); t.push_back(dist2(v[0], v.back())); for (int i = 0; i < ((int)(v.size())) - 2; i++) tt.push_back(dot(v[i] - v[i + 1], v[i + 2] - v[i + 1])); tt.push_back(dot(v[((int)(v.size())) - 2] - v[((int)(v.size())) - 1], v[0] - v[((int)(v.size())) - 1])); tt.push_back(dot(v[((int)(v.size())) - 1] - v[0], v[1] - v[0])); vector<long long> foo; for (int i = 0; i < ((int)(t.size())); i++) { foo.push_back(t[i]); foo.push_back(tt[i]); } return foo; } int z[1234567]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<Point> A(n); vector<Point> B(m); for (int i = 0; i < n; i++) cin >> A[i].x >> A[i].y; for (int i = 0; i < m; i++) cin >> B[i].x >> B[i].y; A = convex_hull(A); B = convex_hull(B); vector<long long> s = get(A); vector<long long> ss = get(B); int start = ((int)(s.size())); s.push_back(LLONG_MIN); for (int i = 0; i < ((int)(ss.size())); i++) s.push_back(ss[i]); for (int i = 0; i < ((int)(ss.size())); i++) s.push_back(ss[i]); int L = 0, R = 0, N = ((int)(s.size())); for (int i = 1; i < N; i++) { if (i > R) { L = R = i; while (R < N && s[R - L] == s[R]) R++; z[i] = R - L; R--; } else { int k = i - L; if (z[k] < R - i + 1) z[i] = z[k]; else { L = i; while (R < N && s[R - L] == s[R]) R++; z[i] = R - L; R--; } } } for (int i = start + 1; i < ((int)(s.size())); i += 2) { if (z[i] >= start) { cout << "YES" << '\n'; return 0; } } cout << "NO" << '\n'; return 0; } ```
#include <bits/stdc++.h> using namespace std; void re(long long &x) { x = 0; int b = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') b = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } if (b == -1) x *= -1; } long long a, b, x, y, tot; long long gcd(long long x, long long y) { if (y == 0) { return x; } gcd(y, x % y); } int main() { re(a), re(b), re(x), re(y); tot = gcd(x, y); x = x / tot; y = y / tot; printf("%I64d", min(a / x, b / y)); return 0; }
### Prompt In cpp, your task is to solve the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; void re(long long &x) { x = 0; int b = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') b = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } if (b == -1) x *= -1; } long long a, b, x, y, tot; long long gcd(long long x, long long y) { if (y == 0) { return x; } gcd(y, x % y); } int main() { re(a), re(b), re(x), re(y); tot = gcd(x, y); x = x / tot; y = y / tot; printf("%I64d", min(a / x, b / y)); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long a, b, x, y; long long gcd(long long p, long long q) { if (q == 0) return p; else return gcd(q, p % q); } int main() { cin >> a >> b >> x >> y; long long p = gcd(x, y); x /= p; y /= p; long long u = a / x, v = b / y; cout << min(u, v); return 0; }
### Prompt Please formulate a CPP solution to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long a, b, x, y; long long gcd(long long p, long long q) { if (q == 0) return p; else return gcd(q, p % q); } int main() { cin >> a >> b >> x >> y; long long p = gcd(x, y); x /= p; y /= p; long long u = a / x, v = b / y; cout << min(u, v); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (a == 0 && b != 0) return b; if (a != 0 && b == 0) return a; if (a == b) return a; if (a > b) return gcd(a % b, b); return gcd(a, b % a); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int a, b, x, y; cin >> a >> b >> x >> y; long long int g = gcd(x, y); x = x / g; y = y / g; if (x > a || y > b) cout << "0" << endl; else { cout << min((a / x), (b / y)) << endl; } }
### Prompt Your task is to create a Cpp solution to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (a == 0 && b != 0) return b; if (a != 0 && b == 0) return a; if (a == b) return a; if (a > b) return gcd(a % b, b); return gcd(a, b % a); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int a, b, x, y; cin >> a >> b >> x >> y; long long int g = gcd(x, y); x = x / g; y = y / g; if (x > a || y > b) cout << "0" << endl; else { cout << min((a / x), (b / y)) << endl; } } ```
#include <bits/stdc++.h> using namespace std; long long GCD(long long m, long long n); int main() { long long i, j, k, m, n, val, t = 0, cnt = 0, test; cin >> n >> m >> j >> k; val = GCD(j, k); j /= val; k /= val; cout << min(n / j, m / k) << endl; return 0; } long long GCD(long long m, long long n) { long long divisor, divident, rem = 1; divident = (m > n ? m : n); divisor = (m > n ? n : m); while (rem != 0) { rem = divident % divisor; divident = divisor; divisor = rem; } return divident; }
### Prompt Please provide a cpp coded solution to the problem described below: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long GCD(long long m, long long n); int main() { long long i, j, k, m, n, val, t = 0, cnt = 0, test; cin >> n >> m >> j >> k; val = GCD(j, k); j /= val; k /= val; cout << min(n / j, m / k) << endl; return 0; } long long GCD(long long m, long long n) { long long divisor, divident, rem = 1; divident = (m > n ? m : n); divisor = (m > n ? n : m); while (rem != 0) { rem = divident % divisor; divident = divisor; divisor = rem; } return divident; } ```
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { long long int i, j, k, n, ans = 0, t, a, b, x, y; cin >> a >> b >> x >> y; j = gcd(x, y); x = x / j; y = y / j; cout << min(a / x, b / y); }
### Prompt Generate a Cpp solution to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { long long int i, j, k, n, ans = 0, t, a, b, x, y; cin >> a >> b >> x >> y; j = gcd(x, y); x = x / j; y = y / j; cout << min(a / x, b / y); } ```
#include <bits/stdc++.h> using namespace std; long long x, y, a, b; long long gcd(long long m, long long n) { if (n == 0) return m; return gcd(n, m % n); } int main(void) { cin >> a >> b >> x >> y; long long gxy = gcd(x, y); x /= gxy; y /= gxy; long long rt = min(a / x, b / y); if (rt < 0) rt = 0; cout << rt << endl; }
### Prompt Your task is to create a cpp solution to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long x, y, a, b; long long gcd(long long m, long long n) { if (n == 0) return m; return gcd(n, m % n); } int main(void) { cin >> a >> b >> x >> y; long long gxy = gcd(x, y); x /= gxy; y /= gxy; long long rt = min(a / x, b / y); if (rt < 0) rt = 0; cout << rt << endl; } ```
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int main() { long long a, b, x, y, d; cin >> a >> b >> x >> y; d = gcd(x, y); x /= d; y /= d; cout << min(a / x, b / y); }
### Prompt Create a solution in Cpp for the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int main() { long long a, b, x, y, d; cin >> a >> b >> x >> y; d = gcd(x, y); x /= d; y /= d; cout << min(a / x, b / y); } ```
#include <bits/stdc++.h> using namespace std; long long a, b, x, y; long long gcd(long long x, long long y) { return (min(x, y) == 0 ? max(x, y) : gcd(min(x, y), max(x, y) % min(x, y))); } int main() { cin >> a >> b >> x >> y; long long k = gcd(x, y); x = x / k; y = y / k; long long u = a / x; long long v = b / y; long long res = min(u, v); cout << res; }
### Prompt Please provide a CPP coded solution to the problem described below: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long a, b, x, y; long long gcd(long long x, long long y) { return (min(x, y) == 0 ? max(x, y) : gcd(min(x, y), max(x, y) % min(x, y))); } int main() { cin >> a >> b >> x >> y; long long k = gcd(x, y); x = x / k; y = y / k; long long u = a / x; long long v = b / y; long long res = min(u, v); cout << res; } ```
#include <bits/stdc++.h> using namespace std; long long a, b, x, y; long long cmmdc(long long a, long long b) { long long c; while (b) { c = a % b; a = b; b = c; } return a; } int main() { cin >> a >> b >> x >> y; long long gcd = cmmdc(x, y); x /= gcd; y /= gcd; cout << min(a / x, b / y); return 0; }
### Prompt Generate a CPP solution to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long a, b, x, y; long long cmmdc(long long a, long long b) { long long c; while (b) { c = a % b; a = b; b = c; } return a; } int main() { cin >> a >> b >> x >> y; long long gcd = cmmdc(x, y); x /= gcd; y /= gcd; cout << min(a / x, b / y); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int LIM = 1e5; const int INF = 1e9; const long long INF64 = 1e18; const int MOD = INF + 7; const double EPS = 1e-9; long long gcd(long long a, long long b) { while (a) { b %= a; swap(a, b); } return b; } int main() { ios_base::sync_with_stdio(false); long long a, b, x_, y_; cin >> a >> b >> x_ >> y_; long long gs = gcd(x_, y_); x_ /= gs; y_ /= gs; cout << min(a / x_, b / y_); return 0; }
### Prompt Please create a solution in cpp to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int LIM = 1e5; const int INF = 1e9; const long long INF64 = 1e18; const int MOD = INF + 7; const double EPS = 1e-9; long long gcd(long long a, long long b) { while (a) { b %= a; swap(a, b); } return b; } int main() { ios_base::sync_with_stdio(false); long long a, b, x_, y_; cin >> a >> b >> x_ >> y_; long long gs = gcd(x_, y_); x_ /= gs; y_ /= gs; cout << min(a / x_, b / y_); return 0; } ```
#include <bits/stdc++.h> using namespace std; struct compare { bool operator()(const int64_t& a, const int64_t& b) { return a > b; } }; bool customS(pair<int, int> a, pair<int, int> b) { if (a.first == b.first) return a.second > b.second; return a.first < b.first; } bool customS2(pair<int, int> a, pair<int, int> b) { if ((a.first + a.second) == (b.first + b.second)) return a.second < b.second; return (a.first + a.second) < (b.first + b.second); } class CompareDist { public: bool operator()(pair<int64_t, int64_t> a, pair<int64_t, int64_t> b) { return a.first < b.first; } }; int64_t gcd(int64_t a, int64_t b) { if (b == 0) return a; return gcd(b, a % b); } double gcdfloat(double a, double b) { if (a < b) return gcd(b, a); if (fabs(b) < 0.001) return a; else return (gcd(b, a - floor(a / b) * b)); } int64_t LCA(int64_t a, int64_t b) { while (a != b) { if (b > a) { swap(a, b); } a /= 2; } return a; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int64_t i, j, a, b, d, n, m, k, sum = 0, x, y, flag = 0, ans = 0, zero = 0; string s, newS; cin >> a >> b >> x >> y; int64_t gcdd = gcd(x, y); x /= gcdd; y /= gcdd; double numerator = a / x, denominator = b / y; cout << (int64_t)floor(min(numerator, denominator)) << "\n"; return 0; }
### Prompt Your task is to create a CPP solution to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; struct compare { bool operator()(const int64_t& a, const int64_t& b) { return a > b; } }; bool customS(pair<int, int> a, pair<int, int> b) { if (a.first == b.first) return a.second > b.second; return a.first < b.first; } bool customS2(pair<int, int> a, pair<int, int> b) { if ((a.first + a.second) == (b.first + b.second)) return a.second < b.second; return (a.first + a.second) < (b.first + b.second); } class CompareDist { public: bool operator()(pair<int64_t, int64_t> a, pair<int64_t, int64_t> b) { return a.first < b.first; } }; int64_t gcd(int64_t a, int64_t b) { if (b == 0) return a; return gcd(b, a % b); } double gcdfloat(double a, double b) { if (a < b) return gcd(b, a); if (fabs(b) < 0.001) return a; else return (gcd(b, a - floor(a / b) * b)); } int64_t LCA(int64_t a, int64_t b) { while (a != b) { if (b > a) { swap(a, b); } a /= 2; } return a; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int64_t i, j, a, b, d, n, m, k, sum = 0, x, y, flag = 0, ans = 0, zero = 0; string s, newS; cin >> a >> b >> x >> y; int64_t gcdd = gcd(x, y); x /= gcdd; y /= gcdd; double numerator = a / x, denominator = b / y; cout << (int64_t)floor(min(numerator, denominator)) << "\n"; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int main() { long long a, b, x, y; cin >> a >> b >> x >> y; cout << min(a / (x / gcd(x, y)), b / (y / gcd(x, y))); return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int main() { long long a, b, x, y; cin >> a >> b >> x >> y; cout << min(a / (x / gcd(x, y)), b / (y / gcd(x, y))); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long linf = 1e18; const int N = 100000 + 1; const double eps = 1e-5; const int mo = 1e9 + 7; long long a, b, x, y; long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); scanf("%lld %lld %lld %lld", &a, &b, &x, &y); long long d = gcd(x, y); x /= d, y /= d; long long ans = a / x; ans = min(ans, b / y); printf("%lld\n", ans); return 0; }
### Prompt Your challenge is to write a cpp solution to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long linf = 1e18; const int N = 100000 + 1; const double eps = 1e-5; const int mo = 1e9 + 7; long long a, b, x, y; long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); scanf("%lld %lld %lld %lld", &a, &b, &x, &y); long long d = gcd(x, y); x /= d, y /= d; long long ans = a / x; ans = min(ans, b / y); printf("%lld\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long a, b, x, y; long long gcd; long long GCD(long long a, long long b) { if (b == 0) return a; else return GCD(b, a % b); } int main() { cin >> a >> b >> x >> y; gcd = GCD(x, y); x = x / gcd; y = y / gcd; cout << min(a / x, b / y) << endl; return 0; }
### Prompt Please provide a Cpp coded solution to the problem described below: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long a, b, x, y; long long gcd; long long GCD(long long a, long long b) { if (b == 0) return a; else return GCD(b, a % b); } int main() { cin >> a >> b >> x >> y; gcd = GCD(x, y); x = x / gcd; y = y / gcd; cout << min(a / x, b / y) << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } int main() { long long a, b, x, y; cin >> a >> b >> x >> y; long long kk = gcd(x, y); x = x / kk; y = y / kk; long long ans = min(a / x, b / y); cout << ans << endl; return 0; }
### Prompt Develop a solution in Cpp to the problem described below: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } int main() { long long a, b, x, y; cin >> a >> b >> x >> y; long long kk = gcd(x, y); x = x / kk; y = y / kk; long long ans = min(a / x, b / y); cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long n, m; int gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); long long a, b = 0, c = 0, t, lj = 0, k = 0, r = 0, l = 0, sum = 0, rr, mn = LLONG_MAX, mx = LLONG_MIN, w, y = 0, x = 0, d, neg = 0, pos = 0, ans = 0; string s5 = "", s2 = "", s3 = "", s1 = "", s = ""; map<char, long long> mp; cin >> a >> b >> x >> y; c = gcd(x, y); while (c != 1) { x /= c; y /= c; c = gcd(x, y); } cout << min(b / y, a / x); }
### Prompt Please provide a Cpp coded solution to the problem described below: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long n, m; int gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); long long a, b = 0, c = 0, t, lj = 0, k = 0, r = 0, l = 0, sum = 0, rr, mn = LLONG_MAX, mx = LLONG_MIN, w, y = 0, x = 0, d, neg = 0, pos = 0, ans = 0; string s5 = "", s2 = "", s3 = "", s1 = "", s = ""; map<char, long long> mp; cin >> a >> b >> x >> y; c = gcd(x, y); while (c != 1) { x /= c; y /= c; c = gcd(x, y); } cout << min(b / y, a / x); } ```
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { long long int a, b, x, y, n1, n2, m; cin >> a >> b >> x >> y; long long int g = gcd(x, y); if (g == 0) return 0; x = x / g; y = y / g; n1 = a / x; n2 = b / y; m = min(n1, n2); cout << m; }
### Prompt Please create a solution in Cpp to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { long long int a, b, x, y, n1, n2, m; cin >> a >> b >> x >> y; long long int g = gcd(x, y); if (g == 0) return 0; x = x / g; y = y / g; n1 = a / x; n2 = b / y; m = min(n1, n2); cout << m; } ```
#include <bits/stdc++.h> using namespace std; long long x, y, w, h; inline long long read() { long long s = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { s = s * 10 + c - '0'; c = getchar(); } return f * s; } inline void write(long long x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); } long long gcd(long long x, long long y) { return (x % y == 0) ? y : gcd(y, x % y); } int main() { x = read(); y = read(); w = read(); h = read(); long long g = gcd(w, h); w /= g; h /= g; cout << min(x / w, y / h) << endl; return 0; }
### Prompt Please create a solution in cpp to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long x, y, w, h; inline long long read() { long long s = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { s = s * 10 + c - '0'; c = getchar(); } return f * s; } inline void write(long long x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); } long long gcd(long long x, long long y) { return (x % y == 0) ? y : gcd(y, x % y); } int main() { x = read(); y = read(); w = read(); h = read(); long long g = gcd(w, h); w /= g; h /= g; cout << min(x / w, y / h) << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long mod = (long long)1e9 + 7; long long gcd(long long x, long long y) { if (y == 0) return x; if (x > y) swap(y, x); y %= x; return gcd(x, y); } int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long a, b, x, y; cin >> a >> b >> x >> y; long long k = gcd(x, y); x /= k; y /= k; cout << min((a / x), (b / y)); return 0; }
### Prompt Create a solution in cpp for the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long mod = (long long)1e9 + 7; long long gcd(long long x, long long y) { if (y == 0) return x; if (x > y) swap(y, x); y %= x; return gcd(x, y); } int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long a, b, x, y; cin >> a >> b >> x >> y; long long k = gcd(x, y); x /= k; y /= k; cout << min((a / x), (b / y)); return 0; } ```
#include <bits/stdc++.h> using namespace std; const double PI = 3.141592654; long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } long long fib(int n) { return (((1 / sqrt(5)) * (pow((1 + sqrt(5)) / 2, n)) - ((1 / sqrt(5)) * (pow((1 - sqrt(5)) / 2, n))))); } void fast() { ios_base::sync_with_stdio(0); cin.tie(NULL), cout.tie(NULL); } int main() { fast(); long long a, b, x, y; cin >> a >> b >> x >> y; long long n = gcd(x, y); x /= n, y /= n; long long c = a / x, cc = b / y; cout << min(c, cc); return 0; }
### Prompt Your challenge is to write a CPP solution to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const double PI = 3.141592654; long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } long long fib(int n) { return (((1 / sqrt(5)) * (pow((1 + sqrt(5)) / 2, n)) - ((1 / sqrt(5)) * (pow((1 - sqrt(5)) / 2, n))))); } void fast() { ios_base::sync_with_stdio(0); cin.tie(NULL), cout.tie(NULL); } int main() { fast(); long long a, b, x, y; cin >> a >> b >> x >> y; long long n = gcd(x, y); x /= n, y /= n; long long c = a / x, cc = b / y; cout << min(c, cc); return 0; } ```
#include <bits/stdc++.h> using namespace std; const long long inf = 1e9 + 7; const long long N = 3e5 + 5; long long n, k, id = 0; long long A[N]; long long in[N], f[N]; inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } void solve() { long long i, j, a, b, first, second; cin >> a >> b >> first >> second; long long c = gcd(first, second); first /= c; second /= c; cout << min(a / first, b / second); } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long i, t = 1, j, first; while (t--) solve(); }
### Prompt Develop a solution in Cpp to the problem described below: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const long long inf = 1e9 + 7; const long long N = 3e5 + 5; long long n, k, id = 0; long long A[N]; long long in[N], f[N]; inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } void solve() { long long i, j, a, b, first, second; cin >> a >> b >> first >> second; long long c = gcd(first, second); first /= c; second /= c; cout << min(a / first, b / second); } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long i, t = 1, j, first; while (t--) solve(); } ```
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; t = 1; while (t--) { long long a, b, x, y; cin >> a >> b >> x >> y; long long q = 0; q = gcd(x, y); x = x / q; y = y / q; long long ans = 0; ans = min(a / x, b / y); cout << ans << endl; } return 0; }
### Prompt Develop a solution in cpp to the problem described below: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; t = 1; while (t--) { long long a, b, x, y; cin >> a >> b >> x >> y; long long q = 0; q = gcd(x, y); x = x / q; y = y / q; long long ans = 0; ans = min(a / x, b / y); cout << ans << endl; } return 0; } ```
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { char ch; bool flag = false; for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') flag = true; for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; x = flag ? -x : x; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int main() { long long a, b, x, y; cin >> a >> b >> x >> y; long long ans = 0; long long c = x / gcd(x, y); long long d = y / gcd(x, y); if (a < c || b < d) { printf("0\n"); return 0; } ans = min(a / c, b / d); cout << ans << endl; return 0; }
### Prompt Your challenge is to write a Cpp solution to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; inline void read(int &x) { char ch; bool flag = false; for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') flag = true; for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; x = flag ? -x : x; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int main() { long long a, b, x, y; cin >> a >> b >> x >> y; long long ans = 0; long long c = x / gcd(x, y); long long d = y / gcd(x, y); if (a < c || b < d) { printf("0\n"); return 0; } ans = min(a / c, b / d); cout << ans << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long hcf(long long a, long long b) { return b == 0 ? a : hcf(b, a % b); } int main() { long long a, b, x, y; cin >> a >> b >> x >> y; long long k = hcf(x, y); x = x / k; y = y / k; cout << min(a / x, b / y); }
### Prompt Construct a cpp code solution to the problem outlined: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long hcf(long long a, long long b) { return b == 0 ? a : hcf(b, a % b); } int main() { long long a, b, x, y; cin >> a >> b >> x >> y; long long k = hcf(x, y); x = x / k; y = y / k; cout << min(a / x, b / y); } ```
#include <bits/stdc++.h> using namespace std; long long int a, b, x, y, w, h; long long int gcd(long long int a, long long int b) { if (!b) return a; else return gcd(b, a % b); } int main() { ios::sync_with_stdio(false); while (cin >> a >> b >> x >> y) { long long int temp = gcd(x, y); long long int x1 = x / temp; long long int y1 = y / temp; long long int ans = min(a / x1, b / y1); cout << ans << endl; } }
### Prompt Create a solution in CPP for the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int a, b, x, y, w, h; long long int gcd(long long int a, long long int b) { if (!b) return a; else return gcd(b, a % b); } int main() { ios::sync_with_stdio(false); while (cin >> a >> b >> x >> y) { long long int temp = gcd(x, y); long long int x1 = x / temp; long long int y1 = y / temp; long long int ans = min(a / x1, b / y1); cout << ans << endl; } } ```
#include <bits/stdc++.h> using namespace std; int n; const int maxn = 1e5 + 1; const int mod = 1e9 + 7; void init() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } long long gcd(long long a0, long long b0) { while (a0 > 0 && b0 > 0) { if (a0 > b0) a0 %= b0; else b0 %= a0; } return a0 + b0; } int main() { init(); long long a, b, x, y; cin >> a >> b >> x >> y; long long g = gcd(x, y); x /= g; y /= g; cout << min(a / x, b / y) << endl; return 0; }
### Prompt Please create a solution in cpp to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; int n; const int maxn = 1e5 + 1; const int mod = 1e9 + 7; void init() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } long long gcd(long long a0, long long b0) { while (a0 > 0 && b0 > 0) { if (a0 > b0) a0 %= b0; else b0 %= a0; } return a0 + b0; } int main() { init(); long long a, b, x, y; cin >> a >> b >> x >> y; long long g = gcd(x, y); x /= g; y /= g; cout << min(a / x, b / y) << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long a, b, x, y; long long gcd(long long c, long long d) { if (d == 0) return c; else return gcd(d, c % d); } int main() { scanf("%lld %lld %lld %lld", &a, &b, &x, &y); long long k = gcd(x, y); x /= k; y /= k; long long ans = min(a / x, b / y); printf("%lld", ans); return 0; }
### Prompt Please formulate a cpp solution to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long a, b, x, y; long long gcd(long long c, long long d) { if (d == 0) return c; else return gcd(d, c % d); } int main() { scanf("%lld %lld %lld %lld", &a, &b, &x, &y); long long k = gcd(x, y); x /= k; y /= k; long long ans = min(a / x, b / y); printf("%lld", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int gsd(long long int n1, long long int n2) { int k; if (n1 == 1 || n2 == 1) return 1; if (n1 % n2 == 0) return n2; if (n2 % n1 == 0) return n1; while (n1 != n2) { if (n1 > n2) n1 -= (n1 / n2) * n2; else n2 -= (n2 / n1) * n1; if (n1 % n2 == 0) return n2; if (n2 % n1 == 0) return n1; if (n1 == 1 || n2 == 1) return 1; } return n1; } int main() { long long int i, j, k, m, n, a, b, x, y, t, w; cin >> a >> b >> x >> y; k = gsd(x, y); t = x / k; w = y / k; cout << min(a / t, b / w) << endl; return 0; }
### Prompt Generate a Cpp solution to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int gsd(long long int n1, long long int n2) { int k; if (n1 == 1 || n2 == 1) return 1; if (n1 % n2 == 0) return n2; if (n2 % n1 == 0) return n1; while (n1 != n2) { if (n1 > n2) n1 -= (n1 / n2) * n2; else n2 -= (n2 / n1) * n1; if (n1 % n2 == 0) return n2; if (n2 % n1 == 0) return n1; if (n1 == 1 || n2 == 1) return 1; } return n1; } int main() { long long int i, j, k, m, n, a, b, x, y, t, w; cin >> a >> b >> x >> y; k = gsd(x, y); t = x / k; w = y / k; cout << min(a / t, b / w) << endl; return 0; } ```
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a % b) return gcd(b, a % b); else return b; } int main() { long long a, b, c, d; cin >> a >> b >> c >> d; long long g = gcd(c, d); c /= g; d /= g; long long a1 = a / c, a2 = b / d; cout << (a1 < a2 ? a1 : a2); return 0; }
### Prompt Create a solution in cpp for the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a % b) return gcd(b, a % b); else return b; } int main() { long long a, b, c, d; cin >> a >> b >> c >> d; long long g = gcd(c, d); c /= g; d /= g; long long a1 = a / c, a2 = b / d; cout << (a1 < a2 ? a1 : a2); return 0; } ```
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int a, b, x, y, g, ans; cin >> a >> b >> x >> y; g = gcd(x, y); ans = min(a / (x / g), b / (y / g)); cout << ans << "\n"; }
### Prompt Please create a solution in CPP to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int a, b, x, y, g, ans; cin >> a >> b >> x >> y; g = gcd(x, y); ans = min(a / (x / g), b / (y / g)); cout << ans << "\n"; } ```
#include <bits/stdc++.h> using namespace std; long long int power(long long int a, long long int b, long long int m) { if (b == 0) return (1); long long int sol = power(a, b / 2, m); sol = (sol * sol) % m; if (b % 2 == 1) sol = (sol * a) % m; return (sol); } long long int hcf(long long int a, long long int b) { if (a % b == 0) return b; else return hcf(b, a % b); } void solve() { long long int a, b, x, y, x1, y1; cin >> a >> b >> x >> y; x1 = x / hcf(x, y); y1 = y / hcf(x, y); long long int w = a / x1; long long int l = b / y1; long long int sol = min(w, l); cout << sol; } int main() { int t = 1; while (t--) { solve(); } return 0; }
### Prompt In CPP, your task is to solve the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; long long int power(long long int a, long long int b, long long int m) { if (b == 0) return (1); long long int sol = power(a, b / 2, m); sol = (sol * sol) % m; if (b % 2 == 1) sol = (sol * a) % m; return (sol); } long long int hcf(long long int a, long long int b) { if (a % b == 0) return b; else return hcf(b, a % b); } void solve() { long long int a, b, x, y, x1, y1; cin >> a >> b >> x >> y; x1 = x / hcf(x, y); y1 = y / hcf(x, y); long long int w = a / x1; long long int l = b / y1; long long int sol = min(w, l); cout << sol; } int main() { int t = 1; while (t--) { solve(); } return 0; } ```
#include <bits/stdc++.h> using namespace std; const int mod = 256; const int maxn = 1e4 + 7; const int M = 3e5; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int main() { long long a, b, x, y; scanf("%lld%lld%lld%lld", &a, &b, &x, &y); long long g = gcd(x, y); x /= g; y /= g; long long i, j; if (x) i = (long long)floor(1.0 * a / x); if (y) j = (long long)floor(1.0 * b / y); long long ans = (long long)min(i, j); printf("%lld\n", ans); return 0; }
### Prompt Generate a Cpp solution to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int mod = 256; const int maxn = 1e4 + 7; const int M = 3e5; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int main() { long long a, b, x, y; scanf("%lld%lld%lld%lld", &a, &b, &x, &y); long long g = gcd(x, y); x /= g; y /= g; long long i, j; if (x) i = (long long)floor(1.0 * a / x); if (y) j = (long long)floor(1.0 * b / y); long long ans = (long long)min(i, j); printf("%lld\n", ans); return 0; } ```
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; vector<long long> vec; long long ans, sum1, sum2, sum; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { long long n, m, x, y, g, x1, y1; scanf("%lld%lld%lld%lld", &n, &m, &x, &y); g = gcd(x, y); x /= g, y /= g; sum1 = n / x; sum2 = m / y; ans = min(sum1, sum2); printf("%lld\n", ans); return 0; }
### Prompt Please formulate a CPP solution to the following problem: Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: w/h = x/y. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers w and h, beforehand, such that (w ≤ a), (h ≤ b) and (w/h = x/y). In other words, Monocarp wants to determine the number of TV sets having aspect ratio x/y, screen width not exceeding a, and screen height not exceeding b. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers a, b, x, y (1 ≤ a, b, x, y ≤ 10^{18}) — the constraints on the screen width and height, and on the aspect ratio. Output Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 17 15 5 3 Output 3 Input 14 16 7 22 Output 0 Input 4 2 6 4 Output 1 Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Output 1000000063 Note In the first example, there are 3 possible variants: (5, 3), (10, 6), (15, 9). In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: (3, 2). ### Response ```cpp #include <bits/stdc++.h> using namespace std; const int maxn = 200005; vector<long long> vec; long long ans, sum1, sum2, sum; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { long long n, m, x, y, g, x1, y1; scanf("%lld%lld%lld%lld", &n, &m, &x, &y); g = gcd(x, y); x /= g, y /= g; sum1 = n / x; sum2 = m / y; ans = min(sum1, sum2); printf("%lld\n", ans); return 0; } ```