题面
来源:USACO 官网。模型收到的是同一份题面的文字。
You have probably heard of the game "Rock, Paper, Scissors". The cows like to play a similar game they call "Hoof, Paper, Scissors".
The rules of "Hoof, Paper, Scissors" are simple. Two cows play against each other. They both count to three and then each simultaneously makes a gesture that represents either a hoof, a piece of paper, or a pair of scissors. Hoof beats scissors (since a hoof can smash a pair of scissors), scissors beats paper (since scissors can cut paper), and paper beats hoof (since the hoof can get a papercut). For example, if the first cow makes a "hoof" gesture and the second a "paper" gesture, then the second cow wins. Of course, it is also possible to tie, if both cows make the same gesture.
Now there are $N$ ($3\le N\le 2\cdot 10^5$) cows who want to play hoof paper scissors, and they each independently have a strategy of drawing from some fixed distribution. In particular, the $i$th cow's strategy is to play hoof, paper, or scissors with probabilities $\left(\frac{h_i}{h_i+p_i+s_i}, \frac{p_i}{h_i+p_i+s_i}, \frac{s_i}{h_i+p_i+s_i} \right)$, respectively.
How many distinct triples of cows (A,B,C) are there such that A beats B on average, B beats C on average, and C beats A on average? We consider two triples the same if one equals the other up to a cyclic shift.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains $T$ ($1\le T\le 5\cdot 10^4$), the number of independent tests. Each test is specified in the following format:The first line contains $N$.
The next $N$ lines each contain three non-negative integers $h_i$, $p_i$, $s_i$ ($0\le h_i,p_i,s_i\le 10^9, h_i+p_i+s_i>0$).
It is guaranteed that the sum of $N$ over all tests does not exceed
OUTPUT FORMAT (print output to the terminal / stdout):
Output the number of triples.Note: The large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).
SAMPLE INPUT:
2 4 1 0 0 1 0 0 0 1 0 0 0 1 10 20410069 21445597 257862632 114108992 287498302 113278897 607994331 143503714 631122722 337497016 270153603 320256324 633717786 631078144 493265815 202783212 612643590 560838949 713379081 42803063 58996167 293262767 470686180 220651551 656404313 408797935 345461691 959196297 827681918 591519393
SAMPLE OUTPUT:
2 32
For the first test, there are two triples: $(1, 3, 4)$ and $(2, 3, 4)$.
SCORING:
- Inputs 2-3: $N\le 10$
- Inputs 4-9: $N \le 7500$, the sum of $N$ over all tests does not exceed $10^4$
- Inputs 10-21: No additional constraints.
Problem credits: Richard Qi
结果
Kimi K3.1 ProKimi
2/3
解出的采样 ↑
- 85.7采样 1 · 超时
- 100采样 2 · 解出
- 100采样 3 · 解出
- 18.0k平均输出 token(含思考) ↓
- 26.4输出速度 tokens/s ↑
- 568 秒延迟 ↓
- 2.30×CPU 时间 / 官方解 ↓
- 更差 1、同阶 2时间复杂度(相对官方解)
Kimi K3.1 Pro(high)Kimi high
1/3
解出的采样 ↑
- 85.7采样 1 · 超时
- 100采样 2 · 解出
- 4.8采样 3 · 答案错误
- 13.7k平均输出 token(含思考) ↓
- 25.8输出速度 tokens/s ↑
- 540 秒延迟 ↓
- 2.81×CPU 时间 / 官方解 ↓
- 更差 1、同阶 1时间复杂度(相对官方解)
DeepSeek Flash V4.1-1017DeepSeek
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 32.2k平均输出 token(含思考) ↓
- 134.6输出速度 tokens/s ↑
- 248 秒延迟 ↓
- 1.24×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
MiniMax M3.1 FlashMiniMax
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 37.9k平均输出 token(含思考) ↓
- 88.2输出速度 tokens/s ↑
- 422 秒延迟 ↓
- 1.06×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
#include <bits/stdc++.h> using namespace std; using ll = long long; using i128 = __int128_t; struct Vec { ll x, y; }; static inline i128 cross(const Vec &a, const Vec &b) { return (i128)a.x * b.y - (i128)a.y * b.x; } static inline i128 dot(const Vec &a, const Vec &b) { return (i128)a.x * b.x + (i128)a.y * b.y; } static inline int half(const Vec &a) { return (a.y > 0 || (a.y == 0 && a.x > 0)) ? 0 : 1; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<Vec> v; v.reserve(N); for (int i = 0; i < N; i++) { ll h, p, s; cin >> h >> p >> s; Vec a{p - s, h - s}; if (a.x != 0 || a.y != 0) v.push_back(a); } int n = (int)v.size(); if (n < 3) { cout << 0 << '\n'; continue; } sort(v.begin(), v.end(), [](const Vec &a, const Vec &b) { if (half(a) != half(b)) return half(a) < half(b); return cross(a, b) > 0; }); vector<Vec> doubled(2 * n); for (int i = 0; i < 2 * n; i++) doubled[i] = v[i % n]; // q[i]: first direction at least pi radians after v[i]. // r[i]: first direction strictly more than pi radians after v[i]. vector<int> q(n), r(n); int ptr = 1; for (int i = 0; i < n; i++) { ptr = max(ptr, i + 1); int lim = i + n; while (ptr < lim) { i128 cr = cross(v[i], doubled[ptr]); if (cr > 0 || (cr == 0 && dot(v[i], doubled[ptr]) > 0)) { ptr++; } else { break; } } q[i] = ptr; int z = ptr; while (z < lim && cross(v[i], doubled[z]) == 0) z++; r[i] = z; } vector<int> sameEnd(n); for (int l = 0; l < n; ) { int rr = l + 1; while (rr < n && cross(v[l], v[rr]) == 0) rr++; for (int i = l; i < rr; i++) sameEnd[i] = rr; l = rr; } vector<int> R(n); vector<ll> pref(n + 1, 0); for (int i = 0; i < n; i++) { R[i] = min(q[i], n); pref[i + 1] = pref[i] + R[i]; } ll answer = 0; for (int i = 0; i < n; i++) { int L = min(r[i], n); if (L >= n) continue; int lo = sameEnd[i]; int hi = min(q[i], n); if (lo >= hi) continue; int start = lower_bound(R.begin() + lo, R.begin() + hi, L + 1) - R.begin(); if (start < hi) { answer += pref[hi] - pref[start] - (ll)(hi - start) * L; } } cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Point { ll x, y; }; static inline ll cross(const Point& a, const Point& b) { return a.x * b.y - a.y * b.x; } static inline ll dot(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y; } static inline int halfPlane(const Point& p) { return (p.y > 0 || (p.y == 0 && p.x >= 0)) ? 0 : 1; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<Point> points; points.reserve(N); map<pair<ll, ll>, ll> rayCount; for (int i = 0; i < N; i++) { ll h, p, s; cin >> h >> p >> s; Point v{h - s, p - s}; if (v.x == 0 && v.y == 0) continue; points.push_back(v); ll g = std::gcd(v.x < 0 ? -v.x : v.x, v.y < 0 ? -v.y : v.y); rayCount[{v.x / g, v.y / g}]++; } int M = (int)points.size(); if (M < 3) { cout << 0 << '\n'; continue; } sort(points.begin(), points.end(), [](const Point& a, const Point& b) { int ha = halfPlane(a), hb = halfPlane(b); if (ha != hb) return ha < hb; return cross(a, b) > 0; }); ll total = (ll)M * (M - 1) * (M - 2) / 6; // Count triples having a circular gap strictly greater than 180 degrees. ll bad = 0; int r = 0; for (int i = 0; i < M; i++) { if (r < i + 1) r = i + 1; while (r < i + M) { const Point& a = points[i]; const Point& b = points[r % M]; ll cr = cross(a, b); bool inside; if (cr > 0) { inside = true; // Angular difference is in (0, 180). } else if (cr == 0 && dot(a, b) > 0 && r < M) { inside = true; // Same direction, angular difference 0. } else { inside = false; } if (!inside) break; r++; } ll c = r - i - 1; bad += c * (c - 1) / 2; } // Count triples containing an antipodal pair; these have a gap of // exactly 180 degrees and were not counted above. __int128 antipodal = 0; for (const auto& entry : rayCount) { auto key = entry.first; pair<ll, ll> opposite{-key.first, -key.second}; if (!(key < opposite)) continue; auto it = rayCount.find(opposite); if (it == rayCount.end()) continue; ll a = entry.second; ll b = it->second; ll outside = M - a - b; antipodal += (__int128)a * b * outside; antipodal += (__int128)a * b * (a + b - 2) / 2; } ll answer = total - bad - (ll)antipodal; cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using i128 = __int128_t; struct Point { ll x, y; }; struct Ray { Point p; ll weight; }; static inline i128 cross(const Point& a, const Point& b) { return (i128)a.x * b.y - (i128)a.y * b.x; } static inline int halfPlane(const Point& a) { return (a.y > 0 || (a.y == 0 && a.x > 0)) ? 0 : 1; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<Point> points; points.reserve(N); for (int i = 0; i < N; i++) { ll h, p, s; cin >> h >> p >> s; // A beats B exactly when cross(v_A, v_B) < 0, // where v = (p - h, s - h). Point v{p - h, s - h}; if (v.x != 0 || v.y != 0) { points.push_back(v); } } const ll total = (ll)points.size(); if (total < 3) { cout << 0 << '\n'; continue; } sort(points.begin(), points.end(), [](const Point& a, const Point& b) { int ha = halfPlane(a), hb = halfPlane(b); if (ha != hb) return ha < hb; return cross(a, b) > 0; }); // Compress points having the same direction into weighted rays. vector<Ray> rays; for (size_t i = 0; i < points.size();) { size_t j = i + 1; int hi = halfPlane(points[i]); while (j < points.size() && halfPlane(points[j]) == hi && cross(points[i], points[j]) == 0) { ++j; } rays.push_back({points[i], (ll)(j - i)}); i = j; } const int M = (int)rays.size(); // Cows tied with a given cow are precisely those on the same line // through the origin (either the same or the opposite ray). map<pair<ll, ll>, int> lineIds; vector<int> rayLine(M); vector<ll> lineWeight; for (int i = 0; i < M; i++) { ll x = rays[i].p.x; ll y = rays[i].p.y; ll ax = x < 0 ? -x : x; ll ay = y < 0 ? -y : y; ll g = std::gcd(ax, ay); x /= g; y /= g; if (x < 0 || (x == 0 && y < 0)) { x = -x; y = -y; } pair<ll, ll> key{x, y}; auto it = lineIds.find(key); int id; if (it == lineIds.end()) { id = (int)lineWeight.size(); lineIds[key] = id; lineWeight.push_back(0); } else { id = it->second; } rayLine[i] = id; lineWeight[id] += rays[i].weight; } // Number of triples containing no tied pair: the three cows must // come from three distinct lines. i128 sum1 = total, sum2 = 0, sum3 = 0; for (ll w : lineWeight) { sum2 += (i128)w * w; sum3 += (i128)w * w * w; } i128 answer = (sum1 * sum1 * sum1 - 3 * sum1 * sum2 + 2 * sum3) / 6; // in[i] = number of cows beating a cow on ray i. // These are the rays strictly inside the next CCW semicircle. const int D = 2 * M; vector<Point> dirs(D); vector<ll> pref(D + 1, 0); for (int i = 0; i < D; i++) { dirs[i] = rays[i % M].p; pref[i + 1] = pref[i] + rays[i % M].weight; } vector<ll> in(M, 0); int j = 0; for (int i = 0; i < M; i++) { if (j < i + 1) j = i + 1; int limit = i + M; while (j < limit && cross(dirs[i], dirs[j]) > 0) { ++j; } in[i] = pref[j] - pref[i + 1]; } // Subtract transitive non-tied triples. The last term removes // pairs of cows on the same ray that were counted as a pair // both beaten by another cow. for (int i = 0; i < M; i++) { ll w = rays[i].weight; ll out = total - lineWeight[rayLine[i]] - in[i]; answer -= (i128)w * out * (out - 1) / 2; answer += (i128)w * (w - 1) / 2 * in[i]; } cout << (ll)answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using i128 = __int128_t; struct Vec { ll x, y; }; static inline i128 cross(const Vec &a, const Vec &b) { return (i128)a.x * b.y - (i128)a.y * b.x; } static inline i128 dot(const Vec &a, const Vec &b) { return (i128)a.x * b.x + (i128)a.y * b.y; } static inline int half(const Vec &a) { return (a.y > 0 || (a.y == 0 && a.x > 0)) ? 0 : 1; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<Vec> v; v.reserve(N); for (int i = 0; i < N; i++) { ll h, p, s; cin >> h >> p >> s; Vec a{p - s, h - s}; if (a.x != 0 || a.y != 0) v.push_back(a); } int n = (int)v.size(); if (n < 3) { cout << 0 << '\n'; continue; } sort(v.begin(), v.end(), [](const Vec &a, const Vec &b) { if (half(a) != half(b)) return half(a) < half(b); return cross(a, b) > 0; }); vector<Vec> doubled(2 * n); for (int i = 0; i < 2 * n; i++) doubled[i] = v[i % n]; // q[i]: first direction at least pi radians after v[i]. // r[i]: first direction strictly more than pi radians after v[i]. vector<int> q(n), r(n); int ptr = 1; for (int i = 0; i < n; i++) { ptr = max(ptr, i + 1); int lim = i + n; while (ptr < lim) { i128 cr = cross(v[i], doubled[ptr]); if (cr > 0 || (cr == 0 && dot(v[i], doubled[ptr]) > 0)) { ptr++; } else { break; } } q[i] = ptr; int z = ptr; while (z < lim && cross(v[i], doubled[z]) == 0) z++; r[i] = z; } vector<int> sameEnd(n); for (int l = 0; l < n; ) { int rr = l + 1; while (rr < n && cross(v[l], v[rr]) == 0) rr++; for (int i = l; i < rr; i++) sameEnd[i] = rr; l = rr; } vector<int> R(n); vector<ll> pref(n + 1, 0); for (int i = 0; i < n; i++) { R[i] = min(q[i], n); pref[i + 1] = pref[i] + R[i]; } ll answer = 0; for (int i = 0; i < n; i++) { int L = min(r[i], n); if (L >= n) continue; int lo = sameEnd[i]; int hi = min(q[i], n); if (lo >= hi) continue; int start = lower_bound(R.begin() + lo, R.begin() + hi, L + 1) - R.begin(); if (start < hi) { answer += pref[hi] - pref[start] - (ll)(hi - start) * L; } } cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Point { ll x, y; }; static inline ll cross(const Point& a, const Point& b) { return a.x * b.y - a.y * b.x; } static inline ll dot(const Point& a, const Point& b) { return a.x * b.x + a.y * b.y; } static inline int halfPlane(const Point& p) { return (p.y > 0 || (p.y == 0 && p.x >= 0)) ? 0 : 1; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<Point> points; points.reserve(N); map<pair<ll, ll>, ll> rayCount; for (int i = 0; i < N; i++) { ll h, p, s; cin >> h >> p >> s; Point v{h - s, p - s}; if (v.x == 0 && v.y == 0) continue; points.push_back(v); ll g = std::gcd(v.x < 0 ? -v.x : v.x, v.y < 0 ? -v.y : v.y); rayCount[{v.x / g, v.y / g}]++; } int M = (int)points.size(); if (M < 3) { cout << 0 << '\n'; continue; } sort(points.begin(), points.end(), [](const Point& a, const Point& b) { int ha = halfPlane(a), hb = halfPlane(b); if (ha != hb) return ha < hb; return cross(a, b) > 0; }); ll total = (ll)M * (M - 1) * (M - 2) / 6; // Count triples having a circular gap strictly greater than 180 degrees. ll bad = 0; int r = 0; for (int i = 0; i < M; i++) { if (r < i + 1) r = i + 1; while (r < i + M) { const Point& a = points[i]; const Point& b = points[r % M]; ll cr = cross(a, b); bool inside; if (cr > 0) { inside = true; // Angular difference is in (0, 180). } else if (cr == 0 && dot(a, b) > 0 && r < M) { inside = true; // Same direction, angular difference 0. } else { inside = false; } if (!inside) break; r++; } ll c = r - i - 1; bad += c * (c - 1) / 2; } // Count triples containing an antipodal pair; these have a gap of // exactly 180 degrees and were not counted above. __int128 antipodal = 0; for (const auto& entry : rayCount) { auto key = entry.first; pair<ll, ll> opposite{-key.first, -key.second}; if (!(key < opposite)) continue; auto it = rayCount.find(opposite); if (it == rayCount.end()) continue; ll a = entry.second; ll b = it->second; ll outside = M - a - b; antipodal += (__int128)a * b * outside; antipodal += (__int128)a * b * (a + b - 2) / 2; } ll answer = total - bad - (ll)antipodal; cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; using i128 = __int128_t; struct Vec { int64 x, y; }; static inline i128 cross(const Vec& a, const Vec& b) { return (i128)a.x * b.y - (i128)a.y * b.x; } static inline int halfPlane(const Vec& a) { return (a.y > 0 || (a.y == 0 && a.x > 0)) ? 0 : 1; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<Vec> base; base.reserve(N); for (int i = 0; i < N; ++i) { int64 h, p, s; cin >> h >> p >> s; // The pairwise comparison sign is the sign of the cross product // of (h-s, p-s), up to a fixed reversal. Uniform strategies map // to the zero vector and cannot belong to a cycle. Vec v{h - s, p - s}; if (v.x != 0 || v.y != 0) base.push_back(v); } int m = (int)base.size(); if (m < 3) { cout << 0 << '\n'; continue; } sort(base.begin(), base.end(), [](const Vec& a, const Vec& b) { int ha = halfPlane(a), hb = halfPlane(b); if (ha != hb) return ha < hb; return cross(a, b) > 0; }); vector<Vec> a = base; a.insert(a.end(), base.begin(), base.end()); int sz = 2 * m; // One past the end of the run of vectors having the same direction. vector<int> runEnd(sz); runEnd[sz - 1] = sz; for (int i = sz - 2; i >= 0; --i) { if (cross(a[i], a[i + 1]) == 0) runEnd[i] = runEnd[i + 1]; else runEnd[i] = i + 1; } // up[j]: first direction at least pi radians after direction j. vector<int> up(sz); int k = 0; for (int j = 0; j < sz; ++j) { k = max(k, runEnd[j]); while (k < sz && cross(a[j], a[k]) > 0) ++k; up[j] = k; } vector<int64> pref(sz + 1, 0); for (int i = 0; i < sz; ++i) pref[i + 1] = pref[i] + up[i]; int64 counted = 0; for (int i = 0; i < m; ++i) { int lo = runEnd[i]; // Skip directions equal to direction i. int hi = up[i]; // Directions strictly between 0 and pi after i. if (lo >= hi) continue; // First direction strictly more than pi after direction i. int left = up[i]; if (left < sz && cross(a[i], a[left]) == 0) left = runEnd[left]; // For each j in [lo,hi), valid k form [left, up[j]). // up[j] is nondecreasing, so sum all positive interval lengths. int first = upper_bound(up.begin() + lo, up.begin() + hi, left) - up.begin(); int64 cnt = hi - first; if (cnt > 0) { counted += (pref[hi] - pref[first]) - (int64)left * cnt; } } // Every cyclic triple was counted once from each of its three cows. cout << counted / 3 << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Vec { long long x, y; }; static inline __int128 cross(const Vec& a, const Vec& b) { return (__int128)a.x * b.y - (__int128)a.y * b.x; } static inline __int128 dot(const Vec& a, const Vec& b) { return (__int128)a.x * b.x + (__int128)a.y * b.y; } static inline int half(const Vec& v) { return (v.y > 0 || (v.y == 0 && v.x >= 0)) ? 0 : 1; } bool cmpAngle(const Vec& a, const Vec& b) { int ha = half(a), hb = half(b); if (ha != hb) return ha < hb; __int128 c = cross(a, b); if (c != 0) return c > 0; __int128 la = (__int128)a.x * a.x + (__int128)a.y * a.y; __int128 lb = (__int128)b.x * b.x + (__int128)b.y * b.y; return la < lb; } void printInt128(__int128 x) { if (x == 0) { cout << 0 << '\n'; return; } if (x < 0) { cout << '-'; x = -x; } string s; while (x > 0) { s.push_back(char('0' + x % 10)); x /= 10; } reverse(s.begin(), s.end()); cout << s << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<Vec> pts; pts.reserve(N); for (int i = 0; i < N; ++i) { long long h, p, s; cin >> h >> p >> s; // Direction relative to the uniform distribution (1/3, 1/3, 1/3) long long x = 2 * h - p - s; long long y = 2 * p - h - s; if (x == 0 && y == 0) continue; // uniform strategy ties with everyone pts.push_back({x, y}); } if (pts.size() < 3) { cout << 0 << '\n'; continue; } sort(pts.begin(), pts.end(), cmpAngle); vector<Vec> dirs; vector<long long> w; dirs.reserve(pts.size()); w.reserve(pts.size()); for (const Vec& v : pts) { if (!dirs.empty() && cross(dirs.back(), v) == 0 && dot(dirs.back(), v) > 0) { ++w.back(); } else { dirs.push_back(v); w.push_back(1); } } int M = (int)dirs.size(); if (M < 3) { cout << 0 << '\n'; continue; } // e3 = sum over triples of distinct directions w[i] * w[j] * w[k] __int128 e1 = 0, e2 = 0, e3 = 0; for (long long wi : w) { e3 += e2 * wi; e2 += e1 * wi; e1 += wi; } vector<long long> pref(2 * M + 1, 0), pref2(2 * M + 1, 0); for (int i = 0; i < 2 * M; ++i) { long long wi = w[i % M]; pref[i + 1] = pref[i] + wi; pref2[i + 1] = pref2[i] + wi * wi; } __int128 invalid = 0; int r = 0; for (int i = 0; i < M; ++i) { if (r < i) r = i; while (r + 1 < i + M) { Vec a = dirs[i]; Vec b = dirs[(r + 1) % M]; if (cross(a, b) >= 0) { ++r; } else { break; } } int L = i + 1; int R = r; if (L <= R) { long long sumW = pref[R + 1] - pref[L]; long long sumW2 = pref2[R + 1] - pref2[L]; __int128 pairs = ((__int128)sumW * sumW - sumW2) / 2; invalid += pairs * w[i]; } } __int128 answer = e3 - invalid; printInt128(answer); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; struct Point { ll x, y; }; static inline __int128 cross(const Point& a, const Point& b) { return (__int128)a.x * b.y - (__int128)a.y * b.x; } static inline int half(const Point& p) { if (p.y > 0 || (p.y == 0 && p.x > 0)) return 0; return 1; } struct PairHash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15ULL; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL; x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL; return x ^ (x >> 31); } size_t operator()(const pair<ll, ll>& p) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); uint64_t a = splitmix64((uint64_t)p.first + FIXED_RANDOM); uint64_t b = splitmix64((uint64_t)p.second + FIXED_RANDOM); return a ^ (b << 1); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<Point> pts; pts.reserve(N); for (int i = 0; i < N; i++) { ll h, p, s; cin >> h >> p >> s; ll sum = h + p + s; // Centered vector (up to a factor of 3), projected to 2D. ll x = 3 * h - sum; ll y = 3 * p - sum; // Equal distribution gives the zero vector and can never be in a cycle. if (x != 0 || y != 0) { pts.push_back({x, y}); } } int M = (int)pts.size(); if (M < 3) { cout << 0 << '\n'; continue; } // Count triples containing an antipodal pair. These are exactly the // boundary cases where the maximum circular gap is exactly pi. unordered_map<pair<ll, ll>, ll, PairHash> mp; mp.reserve(M * 2 + 10); for (const Point& pt : pts) { ll x = pt.x; ll y = pt.y; ll ax = (x >= 0 ? x : -x); ll ay = (y >= 0 ? y : -y); ll g = std::gcd(ax, ay); ll nx = x / g; ll ny = y / g; mp[{nx, ny}]++; } ll boundary = 0; for (auto& entry : mp) { pair<ll, ll> key = entry.first; ll a = entry.second; pair<ll, ll> opp = {-key.first, -key.second}; if (key < opp) { auto it = mp.find(opp); if (it != mp.end()) { ll b = it->second; ll other = (ll)M - a - b; boundary += a * b * other; boundary += (a * (a - 1) / 2) * b; boundary += a * (b * (b - 1) / 2); } } } // Sort vectors by polar angle. sort(pts.begin(), pts.end(), [](const Point& a, const Point& b) { int ha = half(a); int hb = half(b); if (ha != hb) return ha < hb; __int128 cr = cross(a, b); if (cr != 0) return cr > 0; __int128 la = (__int128)a.x * a.x + (__int128)a.y * a.y; __int128 lb = (__int128)b.x * b.x + (__int128)b.y * b.y; return la < lb; }); // Count bad triples whose points fit inside an open semicircle. ll bad = 0; int r = 1; for (int i = 0; i < M; i++) { if (r < i + 1) r = i + 1; while (r < i + M) { const Point& a = pts[i]; const Point& b = pts[r % M]; __int128 cr = cross(a, b); bool inside; if (cr > 0) { inside = true; } else if (cr < 0) { inside = false; } else { __int128 dot = (__int128)a.x * b.x + (__int128)a.y * b.y; if (dot < 0) { // Opposite directions: angle difference is pi. inside = false; } else { // Same direction. Include duplicates only within the // current full turn, not the wrapped copy. inside = (r < M); } } if (!inside) break; ++r; } ll k = (ll)r - i - 1; if (k >= 2) { bad += k * (k - 1) / 2; } } ll total = (ll)M * (M - 1) * (M - 2) / 6; ll answer = total - bad - boundary; cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using i128 = __int128_t; struct Point { long long x, y; }; i128 cross(const Point& a, const Point& b) { return (i128)a.x * b.y - (i128)a.y * b.x; } i128 dot(const Point& a, const Point& b) { return (i128)a.x * b.x + (i128)a.y * b.y; } int half(const Point& p) { return (p.y > 0 || (p.y == 0 && p.x > 0)) ? 0 : 1; } struct Cmp { bool operator()(const Point& a, const Point& b) const { int ha = half(a), hb = half(b); if (ha != hb) return ha < hb; i128 cr = cross(a, b); if (cr != 0) return cr > 0; if (a.x != b.x) return a.x < b.x; return a.y < b.y; } }; bool sameRay(const Point& a, const Point& b) { if (cross(a, b) != 0) return false; return dot(a, b) > 0; } void print128(i128 x) { if (x == 0) { cout << 0; return; } if (x < 0) { cout << '-'; x = -x; } string s; while (x > 0) { s.push_back(char('0' + (int)(x % 10))); x /= 10; } reverse(s.begin(), s.end()); cout << s; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<Point> pts; pts.reserve(N); for (int i = 0; i < N; i++) { long long h, p, s; cin >> h >> p >> s; long long total = h + p + s; // Coordinates of the point relative to the center (1/3, 1/3, 1/3), // projected onto the (h, p) plane. long long x = 3 * h - total; long long y = 3 * p - total; if (x != 0 || y != 0) { pts.push_back({x, y}); } } if (pts.size() < 3) { cout << 0 << '\n'; continue; } sort(pts.begin(), pts.end(), Cmp()); vector<long long> vx, vy, w; for (const Point& p : pts) { if (vx.empty() || !sameRay({vx.back(), vy.back()}, p)) { vx.push_back(p.x); vy.push_back(p.y); w.push_back(1); } else { w.back()++; } } int M = (int)vx.size(); if (M < 3) { cout << 0 << '\n'; continue; } // Number of triples using three distinct directions: // sum_{i<j<k} w[i] * w[j] * w[k] i128 e1 = 0, e2 = 0, e3 = 0; long long W = 0; for (long long ww : w) { e3 += e2 * ww; e2 += e1 * ww; e1 += ww; W += ww; } vector<long long> prefW(2 * M + 1, 0); vector<long long> prefW2(2 * M + 1, 0); for (int i = 0; i < 2 * M; i++) { long long ww = w[i % M]; prefW[i + 1] = prefW[i] + ww; prefW2[i + 1] = prefW2[i] + ww * ww; } i128 bad = 0; i128 degenerate = 0; int r = 1; for (int i = 0; i < M; i++) { if (r < i + 1) r = i + 1; Point a{vx[i], vy[i]}; while (r < i + M) { Point b{vx[r % M], vy[r % M]}; if (cross(a, b) > 0) { r++; } else { break; } } i128 sumW = (i128)prefW[r] - prefW[i + 1]; i128 sumW2 = (i128)prefW2[r] - prefW2[i + 1]; i128 pairs = (sumW * sumW - sumW2) / 2; bad += (i128)w[i] * pairs; if (r < i + M) { Point b{vx[r % M], vy[r % M]}; if (cross(a, b) == 0 && dot(a, b) < 0) { int j = r % M; if (i < j) { degenerate += (i128)w[i] * w[j] * (W - w[i] - w[j]); } } } } i128 answer = e3 - bad - degenerate; print128(answer); cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; using i128 = __int128_t; struct Vec { int64 x, y; }; static inline i128 cross(const Vec& a, const Vec& b) { return (i128)a.x * b.y - (i128)a.y * b.x; } static inline i128 dot(const Vec& a, const Vec& b) { return (i128)a.x * b.x + (i128)a.y * b.y; } // Half 0 contains angles [0, pi), half 1 contains [pi, 2*pi). static inline int half(const Vec& a) { return (a.y > 0 || (a.y == 0 && a.x > 0)) ? 0 : 1; } struct Group { Vec v; int64 cnt; // Number of cows with this direction int64 u; // Number of cows with angle < angle(v) - pi int64 l; // Number of cows with angle <= angle(v) - pi }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<Vec> cows; cows.reserve(N); for (int i = 0; i < N; ++i) { int64 h, p, s; cin >> h >> p >> s; Vec v{h - p, p - s}; // A zero vector ties with every other cow, so it cannot // participate in a valid triple. if (v.x != 0 || v.y != 0) { cows.push_back(v); } } if (cows.size() < 3) { cout << 0 << '\n'; continue; } // Exact polar-angle sorting, without floating point. sort(cows.begin(), cows.end(), [](const Vec& a, const Vec& b) { int ha = half(a); int hb = half(b); if (ha != hb) return ha < hb; return cross(a, b) > 0; }); // Group cows pointing in the same direction. vector<Group> groups; groups.reserve(cows.size()); for (const Vec& v : cows) { if (!groups.empty() && cross(groups.back().v, v) == 0 && dot(groups.back().v, v) > 0) { ++groups.back().cnt; } else { groups.push_back({v, 1, 0, 0}); } } int M = (int)groups.size(); vector<int64> prefixCount(M + 1, 0); for (int i = 0; i < M; ++i) { prefixCount[i + 1] = prefixCount[i] + groups[i].cnt; } int firstHalfCount = 0; while (firstHalfCount < M && half(groups[firstHalfCount].v) == 0) { ++firstHalfCount; } // Compute U and L for every direction. for (Group& g : groups) { if (half(g.v) == 0) { // angle(g.v) - pi is negative. g.u = g.l = 0; continue; } // Let phi = angle(g.v) - pi, which lies in [0, pi). // For a first-half direction p: // cross(g.v, p) > 0 iff angle(p) < phi, // cross(g.v, p) = 0 iff angle(p) = phi. int lo = 0, hi = firstHalfCount; while (lo < hi) { int mid = (lo + hi) / 2; if (cross(g.v, groups[mid].v) > 0) { lo = mid + 1; } else { hi = mid; } } int pos = lo; g.u = prefixCount[pos]; g.l = g.u; // Include the antipodal direction only in the inclusive count. if (pos < firstHalfCount && cross(g.v, groups[pos].v) == 0) { g.l += groups[pos].cnt; } } vector<int64> prefixMass(M + 1, 0); vector<int64> prefixWeightedU(M + 1, 0); for (int i = 0; i < M; ++i) { prefixMass[i + 1] = prefixMass[i] + groups[i].cnt; prefixWeightedU[i + 1] = prefixWeightedU[i] + groups[i].cnt * groups[i].u; } int64 answer = 0; int right = -1; for (int j = 0; j < M; ++j) { right = max(right, j); // For j < k, cross(v_j, v_k) > 0 exactly when // angle(k) - angle(j) < pi. while (right + 1 < M && cross(groups[j].v, groups[right + 1].v) > 0) { ++right; } if (right <= j) continue; int64 mass = prefixMass[right + 1] - prefixMass[j + 1]; int64 weightedU = prefixWeightedU[right + 1] - prefixWeightedU[j + 1]; answer += groups[j].cnt * (weightedU - groups[j].l * mass); } cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; using i128 = __int128_t; struct Vec { int64 x, y; }; static i128 cross(const Vec& a, const Vec& b) { return (i128)a.x * b.y - (i128)a.y * b.x; } static i128 dot(const Vec& a, const Vec& b) { return (i128)a.x * b.x + (i128)a.y * b.y; } // Half 0: angles in [0, pi), half 1: angles in [pi, 2*pi). static int half(const Vec& a) { return (a.y > 0 || (a.y == 0 && a.x > 0)) ? 0 : 1; } static bool polarLess(const Vec& a, const Vec& b) { int ha = half(a), hb = half(b); if (ha != hb) return ha < hb; return cross(a, b) > 0; } static int64 choose2(int64 x) { if (x < 2) return 0; return x * (x - 1) / 2; } static int64 choose3(int64 x) { if (x < 3) return 0; return x * (x - 1) * (x - 2) / 6; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<Vec> vectors; vectors.reserve(N); for (int i = 0; i < N; ++i) { int64 h, p, s; cin >> h >> p >> s; int64 H = h + p + s; int64 x = 3 * h - H; int64 y = 3 * p - H; // The zero vector represents the uniform strategy, which ties // against every strategy and therefore cannot be in a cycle. if (x != 0 || y != 0) { vectors.push_back({x, y}); } } int64 n = (int64)vectors.size(); if (n < 3) { cout << 0 << '\n'; continue; } sort(vectors.begin(), vectors.end(), polarLess); // Group cows whose strategy vectors point in the same direction. vector<Vec> direction; vector<int64> count; for (const Vec& v : vectors) { if (!direction.empty() && cross(direction.back(), v) == 0 && dot(direction.back(), v) > 0) { ++count.back(); } else { direction.push_back(v); count.push_back(1); } } int m = (int)direction.size(); // A valid cycle needs three distinct, non-opposite directions. if (m < 3) { cout << 0 << '\n'; continue; } // first[i] is the first direction whose counterclockwise angular // distance from direction i is at least pi. vector<int> first(m); int r = 1; for (int i = 0; i < m; ++i) { r = max(r, i + 1); while (r < i + m && cross(direction[i], direction[r % m]) > 0) { ++r; } first[i] = r; } // Invalid triples containing two cows with the same direction. int64 sameDirection = 0; for (int64 c : count) { sameDirection += choose3(c); sameDirection += choose2(c) * (n - c); } // Invalid triples containing a pair of opposite directions. // Every opposite pair is found from both endpoints, so divide by 2. int64 oppositeTwice = 0; for (int i = 0; i < m; ++i) { if (first[i] < i + m) { int j = first[i] % m; if (cross(direction[i], direction[j]) == 0 && dot(direction[i], direction[j]) < 0) { oppositeTwice += count[i] * count[j] * (n - count[i] - count[j]); } } } int64 opposite = oppositeTwice / 2; // Prefix sums over two copies of the circular order. vector<int64> prefixCount(2 * m + 1, 0); vector<int64> prefixSamePairs(2 * m + 1, 0); for (int i = 0; i < 2 * m; ++i) { int64 c = count[i % m]; prefixCount[i + 1] = prefixCount[i] + c; prefixSamePairs[i + 1] = prefixSamePairs[i] + choose2(c); } // Invalid triples whose three distinct directions fit in an open // semicircle. int64 openSemicircle = 0; for (int i = 0; i < m; ++i) { int64 M = prefixCount[first[i]] - prefixCount[i + 1]; int64 Q = prefixSamePairs[first[i]] - prefixSamePairs[i + 1]; // Choose one cow from group i and two cows from different // groups in the following open half-circle. openSemicircle += count[i] * (choose2(M) - Q); } int64 answer = choose3(n) - sameDirection - opposite - openSemicircle; cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; using i128 = __int128_t; struct PairHash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15ULL; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL; x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL; return x ^ (x >> 31); } size_t operator()(const pair<int64, int64>& p) const { static const uint64_t seed = chrono::steady_clock::now().time_since_epoch().count(); uint64_t a = splitmix64(static_cast<uint64_t>(p.first) + seed); uint64_t b = splitmix64(static_cast<uint64_t>(p.second) + seed + 0x9e3779b97f4a7c15ULL); return a ^ (b + 0x9e3779b97f4a7c15ULL + (a << 6) + (a >> 2)); } }; struct Bucket { int64 x, y; int64 weight; }; struct Direction { int64 x, y; int64 weight; }; static int halfPlane(const Direction& d) { return (d.y > 0 || (d.y == 0 && d.x > 0)) ? 0 : 1; } static i128 cross(const Direction& a, const Direction& b) { return (i128)a.x * b.y - (i128)a.y * b.x; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; unordered_map<pair<int64, int64>, Bucket, PairHash> buckets; buckets.max_load_factor(0.7f); buckets.reserve(N); int64 active = 0; for (int i = 0; i < N; ++i) { int64 h, p, s; cin >> h >> p >> s; int64 x = 2 * h - p - s; int64 y = p - s; // The completely mixed strategy ties against every strategy. if (x == 0 && y == 0) continue; // Normalize so that equal rays have the same key. int64 g = std::gcd(std::llabs(x), std::llabs(y)); x /= g; y /= g; pair<int64, int64> key{x, y}; auto it = buckets.find(key); if (it == buckets.end()) { buckets.emplace(key, Bucket{x, y, 1}); } else { ++it->second.weight; } ++active; } int64 seen = 0; int64 pairs = 0; int64 allTriples = 0; int64 boundaryTriples = 0; vector<Direction> directions; directions.reserve(buckets.size()); for (const auto& entry : buckets) { const auto& key = entry.first; const Bucket& b = entry.second; // Number of choices using three distinct direction groups. allTriples += b.weight * pairs; pairs += b.weight * seen; seen += b.weight; directions.push_back({b.x, b.y, b.weight}); // Exactly opposite rays tie. A triangle containing such a pair // has the origin on its boundary, rather than strictly inside. pair<int64, int64> opposite{-b.x, -b.y}; if (key < opposite) { auto jt = buckets.find(opposite); if (jt != buckets.end()) { int64 a = b.weight; int64 c = jt->second.weight; boundaryTriples += a * c * (active - a - c); } } } sort(directions.begin(), directions.end(), [](const Direction& a, const Direction& b) { int ha = halfPlane(a); int hb = halfPlane(b); if (ha != hb) return ha < hb; return cross(a, b) > 0; }); int M = (int)directions.size(); // Duplicate the circular ordering. vector<Direction> circular(2 * M); for (int i = 0; i < 2 * M; ++i) { circular[i] = directions[i % M]; } vector<int64> prefixWeight(2 * M + 1, 0); vector<int64> prefixSquare(2 * M + 1, 0); for (int i = 0; i < 2 * M; ++i) { int64 w = circular[i].weight; prefixWeight[i + 1] = prefixWeight[i] + w; prefixSquare[i + 1] = prefixSquare[i] + w * w; } // Count triples lying in an open semicircle. Each such triple has // a unique direction immediately following its unique gap > 180°. int64 outsideTriples = 0; int r = 1; for (int i = 0; i < M; ++i) { r = max(r, i + 1); while (r < i + M && cross(circular[i], circular[r]) > 0) { ++r; } int64 sum = prefixWeight[r] - prefixWeight[i + 1]; int64 sumSquares = prefixSquare[r] - prefixSquare[i + 1]; int64 twoDistinctGroups = (sum * sum - sumSquares) / 2; outsideTriples += circular[i].weight * twoDistinctGroups; } int64 answer = allTriples - outsideTriples - boundaryTriples; cout << answer << '\n'; } return 0; }