题面
来源:FrontierCS 官方 statement.txt,与模型收到的题面相同。
Sphere Packing in a Cube (Optimization)
You are given an integer n. Place n pairwise-disjoint congruent solid spheres inside the unit cube [0, 1]^3 so that the common radius is as large as possible. You do not need to output the radius explicitly; the checker infers the largest valid common radius from your centers.
Input
The input consists of a single line with one integer:
n (2 ≤ n ≤ 10^6 in principle; in the official tests n ≤ 4096.)
Output
Output exactly n lines. Each line must contain three real numbers x y z (in any standard real-number format parseable by C/C++, e.g., 0.0, 1, 1e-3), giving the coordinates of a sphere center. All coordinates must satisfy 0 ≤ x, y, z ≤ 1. Whitespace is free; no additional text is allowed. Trailing whitespace is ignored.
Feasibility & how the checker interprets your output
Given your centers C = {c_i}, the checker computes the largest common radius that makes the spheres non-overlapping and contained in the cube, completely ignoring any radius you might have assumed. Formally, your geometric radius is
r(C) = min( ½ * min_{i≠j} ||c_i - c_j||_2 , min_i dist(c_i, ∂[0,1]^3) ),
where dist(c_i, ∂[0,1]^3) is the minimum distance from c_i to any cube face. If any coordinate lies outside [0,1] (up to an absolute tolerance of 1e−12), the output is rejected.
## Scoring
Let x be rho, the geometric sphere radius. B and R are the baseline and comparison radii stored with the test. If R<=B+1e-15, use the separate rule: 60 if rho+1e-18>=R, otherwise 0. These tolerances are absolute. The physical reference (the 60-point level) is a=R. Define the lower-band continuation:
L(x) = 60*max(0,(x-B)/(R-B)).
For positive x and positive physical reference a, define the improvement factor r=x/a. The physical reference has r=1 and earns 60 points. For a valid output on this branch, the points P are:
P = L(x), if L(x)<=60;
P = min(100,60+ln(r)), otherwise.
Here ln is the natural logarithm. In the upper branch before the 100-point cap, r=exp(P-60): each additional point requires an e-fold improvement in the physical objective (multiplication by e). A zero maximization objective is not a full-score exception. All feasibility, correctness, and hard resource-limit checks remain required; invalid output receives 0.
Across test files, use the arithmetic mean of the file scores.
Validation & precision
• The checker reads doubles and computes in IEEE-754 double precision. It accepts coordinates on the faces of the cube.
• The checker rejects outputs that do not contain exactly n triples, that contain non-finite numbers, or that place any center outside [0,1].
• Distances are computed exactly as written above; there is no relative/absolute tolerance applied to r(C) beyond floating-point rounding.
• The checker runs in O(n^2) on your centers (only 8.4 million pairs at n=4096), so keep n modest if you test locally.
Example
Input
5
Valid Output (one of many)
0.1 0.1 0.1
0.9 0.1 0.1
0.1 0.9 0.1
0.1 0.1 0.9
0.9 0.9 0.9
(Your solution is free to produce any arrangement.)
Notes
• You only need to output centers. The checker automatically determines the maximum common radius supported by your centers.
• Greedy / local-improvement heuristics, lattice-based constructions, simulated annealing, or nonlinear optimization often yield good packings.
• Touching spheres and touching the cube faces are allowed; overlaps are not.
结果
Kimi K3.1 ProKimi
max
22.8
平均得分 ↑
- 22.8采样 1 · 部分分
- 40.4k平均输出 token(含思考) ↓
- 32.5输出速度 tokens/s ↑
- 1243 秒延迟 ↓
Kimi K3.1 Pro(high)Kimi high
high
25.3
平均得分 ↑
- 25.3采样 1 · 部分分
- 19.7k平均输出 token(含思考) ↓
- 29.1输出速度 tokens/s ↑
- 681 秒延迟 ↓
DeepSeek Flash V4.1-1017DeepSeek
auto(high)
2.2
平均得分 ↑
- 2.2采样 1 · 部分分
- 43.4k平均输出 token(含思考) ↓
- 195.4输出速度 tokens/s ↑
- 223 秒延迟 ↓
MiniMax M3.1 FlashMiniMax
auto(max)
0.0
平均得分 ↑
- 0.0采样 1 · 编译错误
- 67.8k平均输出 token(含思考) ↓
- 152.1输出速度 tokens/s ↑
- 446 秒延迟 ↓
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
Kimi K3.1 Pro · 采样 1 · 735 行
#include <bits/stdc++.h> using namespace std; struct Vec3 { double x = 0, y = 0, z = 0; }; static inline Vec3 operator+(const Vec3& a, const Vec3& b) { return {a.x + b.x, a.y + b.y, a.z + b.z}; } static inline Vec3 operator-(const Vec3& a, const Vec3& b) { return {a.x - b.x, a.y - b.y, a.z - b.z}; } static inline Vec3 operator*(const Vec3& a, double s) { return {a.x * s, a.y * s, a.z * s}; } static inline double dot(const Vec3& a, const Vec3& b) { return a.x * b.x + a.y * b.y + a.z * b.z; } static inline Vec3 cross(const Vec3& a, const Vec3& b) { return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; } static inline double norm2(const Vec3& a) { return dot(a, a); } struct Builder { int n; vector<Vec3> best; vector<Vec3> bestRaw; double bestR = 0.0; explicit Builder(int n_) : n(n_) {} double anisotropic(const vector<Vec3>& p, vector<Vec3>& out) const { Vec3 lo{1e100, 1e100, 1e100}, hi{-1e100, -1e100, -1e100}; for (const auto& a : p) { lo.x = min(lo.x, a.x); lo.y = min(lo.y, a.y); lo.z = min(lo.z, a.z); hi.x = max(hi.x, a.x); hi.y = max(hi.y, a.y); hi.z = max(hi.z, a.z); } Vec3 span{hi.x - lo.x, hi.y - lo.y, hi.z - lo.z}; vector<Vec3> q(n); for (int i = 0; i < n; ++i) { q[i].x = span.x > 1e-15 ? (p[i].x - lo.x) / span.x : 0.5; q[i].y = span.y > 1e-15 ? (p[i].y - lo.y) / span.y : 0.5; q[i].z = span.z > 1e-15 ? (p[i].z - lo.z) / span.z : 0.5; } double q2 = 1e100; for (int i = 0; i < n; ++i) { for (int j = 0; j < i; ++j) { q2 = min(q2, norm2(q[i] - q[j])); } } if (!(q2 > 1e-30) || !isfinite(q2)) return 0.0; double qm = sqrt(q2); double r = qm / (2.0 * (1.0 + qm)); double mul = 1.0 - 2.0 * r; out.resize(n); for (int i = 0; i < n; ++i) { out[i] = {r + mul * q[i].x, r + mul * q[i].y, r + mul * q[i].z}; } return r; } void consider(const vector<Vec3>& p, double latticeDistance) { if ((int)p.size() != n) return; if (n <= 700) { vector<Vec3> c; double r = anisotropic(p, c); if (r > bestR) { bestR = r; best = std::move(c); } return; } Vec3 lo{1e100, 1e100, 1e100}, hi{-1e100, -1e100, -1e100}; for (const auto& a : p) { lo.x = min(lo.x, a.x); lo.y = min(lo.y, a.y); lo.z = min(lo.z, a.z); hi.x = max(hi.x, a.x); hi.y = max(hi.y, a.y); hi.z = max(hi.z, a.z); } double S = max({hi.x - lo.x, hi.y - lo.y, hi.z - lo.z}); if (!(S > 1e-15)) return; double scale = 1.0 / (S + latticeDistance); double r = 0.5 * latticeDistance * scale; if (r <= bestR * (1.0 + 1e-13)) return; Vec3 mid{(lo.x + hi.x) * 0.5, (lo.y + hi.y) * 0.5, (lo.z + hi.z) * 0.5}; vector<Vec3> c(n); for (int i = 0; i < n; ++i) { c[i] = {0.5 + (p[i].x - mid.x) * scale, 0.5 + (p[i].y - mid.y) * scale, 0.5 + (p[i].z - mid.z) * scale}; } bestR = r; best = std::move(c); bestRaw = p; } struct Item { Vec3 p; double k1, k2; int id; }; void processBase(const vector<Vec3>& base, double d, const vector<Vec3>& offsets) { if ((int)base.size() < n) return; vector<Item> a(base.size()); for (const Vec3& off : offsets) { for (int i = 0; i < (int)base.size(); ++i) { double dx = base[i].x - off.x; double dy = base[i].y - off.y; double dz = base[i].z - off.z; a[i].p = base[i]; a[i].k1 = max({fabs(dx), fabs(dy), fabs(dz)}); a[i].k2 = dx * dx + dy * dy + dz * dz; a[i].id = i; } auto cmp = [](const Item& u, const Item& v) { if (u.k1 != v.k1) return u.k1 < v.k1; if (u.k2 != v.k2) return u.k2 < v.k2; return u.id < v.id; }; if ((int)a.size() > n) { nth_element(a.begin(), a.begin() + n, a.end(), cmp); a.resize(n); } vector<Vec3> raw; raw.reserve(n); for (const auto& it : a) raw.push_back(it.p); consider(raw, d); } } static vector<Vec3> centralSubset(const vector<Vec3>& input, int wanted, const Vec3& center) { if ((int)input.size() <= wanted) return input; vector<Item> a(input.size()); for (int i = 0; i < (int)input.size(); ++i) { a[i].p = input[i]; a[i].k1 = norm2(input[i] - center); a[i].k2 = 0; a[i].id = i; } auto cmp = [](const Item& u, const Item& v) { if (u.k1 != v.k1) return u.k1 < v.k1; return u.id < v.id; }; nth_element(a.begin(), a.begin() + wanted, a.end(), cmp); a.resize(wanted); vector<Vec3> ans; ans.reserve(wanted); for (const auto& it : a) ans.push_back(it.p); return ans; } void finishLargeCandidate() { if (n > 700 && !bestRaw.empty()) { vector<Vec3> c; double r = anisotropic(bestRaw, c); if (r > bestR) { bestR = r; best = std::move(c); } } } }; struct Optimizer { int n; vector<Vec3>& best; double& bestR; mt19937_64& rng; chrono::steady_clock::time_point deadline; struct Eval { double energy = 0, maxOverlap = 0, actual = -1, force2 = 0; }; struct Result { vector<Vec3> x; bool feasible = false; }; Optimizer(int n_, vector<Vec3>& b, double& r, mt19937_64& g, chrono::steady_clock::time_point dl) : n(n_), best(b), bestR(r), rng(g), deadline(dl) {} Eval evaluate(const vector<Vec3>& x, double R, vector<Vec3>& f) { const double D = 2.0 * R; const double D2 = D * D; f.assign(n, Vec3{}); Eval e; double wall = 1e100; double minOverlappingDistance = D; bool pairOverlap = false; auto doWall = [&](double v, double& force) { wall = min(wall, v); wall = min(wall, 1.0 - v); if (v < R) { double o = R - v; e.energy += 0.5 * o * o; e.maxOverlap = max(e.maxOverlap, o); force += o; } else if (v > 1.0 - R) { double o = v - (1.0 - R); e.energy += 0.5 * o * o; e.maxOverlap = max(e.maxOverlap, o); force -= o; } }; for (int i = 0; i < n; ++i) { doWall(x[i].x, f[i].x); doWall(x[i].y, f[i].y); doWall(x[i].z, f[i].z); } auto doPair = [&](int i, int j) { Vec3 delta = x[i] - x[j]; double d2 = norm2(delta); if (d2 >= D2) return; double d = sqrt(max(0.0, d2)); double ux, uy, uz; if (d > 1e-14) { ux = delta.x / d; uy = delta.y / d; uz = delta.z / d; } else { d = 0; if ((i + j) & 1) { ux = 1; uy = 0; uz = 0; } else { ux = 0; uy = 1; uz = 0; } } double o = D - d; e.energy += 0.5 * o * o; e.maxOverlap = max(e.maxOverlap, o); pairOverlap = true; minOverlappingDistance = min(minOverlappingDistance, d); f[i].x += o * ux; f[i].y += o * uy; f[i].z += o * uz; f[j].x -= o * ux; f[j].y -= o * uy; f[j].z -= o * uz; }; bool useGrid = n > 256; if (useGrid) { int g = (int)floor(1.0 / D); g = max(g, 1); long long cells = 1LL * g * g * g; if (cells > 1000000) { useGrid = false; } else { vector<int> head((size_t)cells, -1), nxt(n, -1); auto coordinateCell = [&](double v) { int c = (int)floor(v * g); return max(0, min(g - 1, c)); }; auto cellId = [&](int a, int b, int c) { return (a * g + b) * g + c; }; for (int i = 0; i < n; ++i) { int id = cellId(coordinateCell(x[i].x), coordinateCell(x[i].y), coordinateCell(x[i].z)); nxt[i] = head[id]; head[id] = i; } for (int i = 0; i < n; ++i) { int cx = coordinateCell(x[i].x); int cy = coordinateCell(x[i].y); int cz = coordinateCell(x[i].z); for (int dx = -1; dx <= 1; ++dx) { int ax = cx + dx; if (ax < 0 || ax >= g) continue; for (int dy = -1; dy <= 1; ++dy) { int ay = cy + dy; if (ay < 0 || ay >= g) continue; for (int dz = -1; dz <= 1; ++dz) { int az = cz + dz; if (az < 0 || az >= g) continue; int j = head[cellId(ax, ay, az)]; while (j != -1) { if (j < i) doPair(i, j); j = nxt[j]; } } } } } } } if (!useGrid) { for (int i = 0; i < n; ++i) { for (int j = 0; j < i; ++j) doPair(i, j); } } for (const auto& a : f) e.force2 += norm2(a); if (wall >= 0.0) { if (pairOverlap) e.actual = min(wall, 0.5 * minOverlappingDistance); else e.actual = min(wall, R); if (e.actual > bestR + 1e-12 && e.actual > 0.0) { bestR = e.actual; best = x; } } return e; } Result relax(vector<Vec3> x, double R, int maxIterations) { vector<Vec3> velocity(n, Vec3{}), force; double dt = 0.75; double alpha = 0.1; int positiveSteps = 0; const double D = 2.0 * R; for (int it = 0; it < maxIterations; ++it) { if ((it & 15) == 0 && chrono::steady_clock::now() >= deadline) break; Eval e = evaluate(x, R, force); if (e.maxOverlap < 1e-10) return {x, true}; if (it > 30 && e.force2 < 1e-25) break; if (dt < 1e-10) break; double power = 0; for (int i = 0; i < n; ++i) { velocity[i].x += dt * force[i].x; velocity[i].y += dt * force[i].y; velocity[i].z += dt * force[i].z; power += dot(velocity[i], force[i]); } if (power > 0.0) { ++positiveSteps; double vv = 0, ff = 0; for (int i = 0; i < n; ++i) { vv += norm2(velocity[i]); ff += norm2(force[i]); } if (vv > 1e-30 && ff > 1e-30) { double factor = sqrt(vv / ff); for (int i = 0; i < n; ++i) { velocity[i].x = (1.0 - alpha) * velocity[i].x + alpha * factor * force[i].x; velocity[i].y = (1.0 - alpha) * velocity[i].y + alpha * factor * force[i].y; velocity[i].z = (1.0 - alpha) * velocity[i].z + alpha * factor * force[i].z; } } if (positiveSteps > 5) { dt = min(dt * 1.1, 6.0); alpha *= 0.99; } } else { positiveSteps = 0; dt *= 0.5; alpha = 0.1; fill(velocity.begin(), velocity.end(), Vec3{}); } double maxMove = 0; for (const auto& v : velocity) { maxMove = max(maxMove, fabs(dt * v.x)); maxMove = max(maxMove, fabs(dt * v.y)); maxMove = max(maxMove, fabs(dt * v.z)); } double moveScale = 1.0; if (maxMove > 0.2 * D && maxMove > 0) { moveScale = 0.2 * D / maxMove; } for (int i = 0; i < n; ++i) { x[i].x += dt * moveScale * velocity[i].x; x[i].y += dt * moveScale * velocity[i].y; x[i].z += dt * moveScale * velocity[i].z; x[i].x = max(0.0, min(1.0, x[i].x)); x[i].y = max(0.0, min(1.0, x[i].y)); x[i].z = max(0.0, min(1.0, x[i].z)); } } return {std::move(x), false}; } void attempt(vector<Vec3> start, double startR) { Result first = relax(std::move(start), startR, 350); if (!first.feasible) return; vector<Vec3> current = std::move(first.x); double currentR = startR; for (int step = 0; step < 90; ++step) { if (chrono::steady_clock::now() >= deadline) return; double increment; if (currentR < 0.90 * bestR) increment = currentR * 0.02; else increment = bestR * 0.002; increment = max(increment, 1e-7); double target = currentR + increment; if (target >= 0.5) return; Result next = relax(current, target, 240); if (!next.feasible) return; current = std::move(next.x); currentR = target; } } bool randomSequential(double R, vector<Vec3>& out) { out.clear(); out.reserve(n); uniform_real_distribution<double> U(R, 1.0 - R); const double need2 = 4.0 * R * R; for (int i = 0; i < n; ++i) { bool placed = false; for (int tries = 0; tries < 3000; ++tries) { Vec3 p{U(rng), U(rng), U(rng)}; bool ok = true; for (const auto& q : out) { if (norm2(p - q) < need2) { ok = false; break; } } if (ok) { out.push_back(p); placed = true; break; } } if (!placed) return false; } return true; } void run() { int attempts; if (n <= 16) attempts = 80; else if (n <= 64) attempts = 48; else if (n <= 128) attempts = 20; else if (n <= 256) attempts = 8; else if (n <= 512) attempts = 3; else attempts = 1; uniform_real_distribution<double> U(-1.0, 1.0); for (int a = 0; a < attempts; ++a) { if (chrono::steady_clock::now() >= deadline) break; if (a == 0) { attempt(best, bestR); continue; } if (a % 4 == 3 && n <= 128) { vector<Vec3> randomStart; double r0 = bestR * 0.65; if (randomSequential(r0, randomStart)) { attempt(std::move(randomStart), r0); continue; } } vector<Vec3> start = best; double amp = bestR * (0.03 + 0.025 * (a % 8)); if (a % 9 == 8) amp *= 1.8; for (auto& p : start) { p.x = max(0.0, min(1.0, p.x + amp * U(rng))); p.y = max(0.0, min(1.0, p.y + amp * U(rng))); p.z = max(0.0, min(1.0, p.z + amp * U(rng))); } double startR = bestR * (0.96 - 0.005 * (a % 5)); attempt(std::move(start), startR); } if (chrono::steady_clock::now() < deadline) { relax(best, min(0.499999, bestR * 1.0003), 600); } } }; static long long fccBlockCount(int m) { long long E = m / 2 + 1; long long O = m + 1 - E; return E * E * E + 3LL * E * O * O; } static long long bccBlockCount(int m) { long long E = m / 2 + 1; long long O = m + 1 - E; return E * E * E + O * O * O; } static void largeFallback(int n) { int k = max(1, (int)ceil(cbrt((double)n) - 1e-12)); while (1LL * k * k * k < n) ++k; int mf = 0; while (fccBlockCount(mf) < n) ++mf; int mb = 0; while (bccBlockCount(mb) < n) ++mb; const double sq2 = sqrt(2.0), sq3 = sqrt(3.0); double rs = 1.0 / (2.0 * k); double rf = sq2 / (2.0 * (mf + sq2)); double rb = sq3 / (2.0 * (mb + sq3)); cout << setprecision(17); if (rs >= rf && rs >= rb) { int printed = 0; for (int i = 0; i < k && printed < n; ++i) { for (int j = 0; j < k && printed < n; ++j) { for (int l = 0; l < k && printed < n; ++l) { cout << (i + 0.5) / k << ' ' << (j + 0.5) / k << ' ' << (l + 0.5) / k << '\n'; ++printed; } } } } else if (rf >= rb) { double step = 1.0 / (mf + sq2); int printed = 0; for (int i = 0; i <= mf && printed < n; ++i) { for (int j = 0; j <= mf && printed < n; ++j) { for (int l = 0; l <= mf && printed < n; ++l) { if (((i + j + l) & 1) != 0) continue; cout << 0.5 + (i - mf * 0.5) * step << ' ' << 0.5 + (j - mf * 0.5) * step << ' ' << 0.5 + (l - mf * 0.5) * step << '\n'; ++printed; } } } } else { double step = 1.0 / (mb + sq3); int printed = 0; for (int i = 0; i <= mb && printed < n; ++i) { for (int j = 0; j <= mb && printed < n; ++j) { for (int l = 0; l <= mb && printed < n; ++l) { int pi = (i % 2 + 2) % 2; int pj = (j % 2 + 2) % 2; int pl = (l % 2 + 2) % 2; if (!(pi == pj && pj == pl)) continue; cout << 0.5 + (i - mb * 0.5) * step << ' ' << 0.5 + (j - mb * 0.5) * step << ' ' << 0.5 + (l - mb * 0.5) * step << '\n'; ++printed; } } } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; if (!(cin >> n)) return 0; if (n == 1) { cout << "0.5 0.5 0.5\n"; return 0; } if (n > 5000) { largeFallback(n); return 0; } Builder builder(n); if (n <= 8) { for (int mask = 0; mask < 256; ++mask) { if (__builtin_popcount((unsigned)mask) != n) continue; vector<Vec3> p; for (int b = 0; b < 8; ++b) { if (mask & (1 << b)) { p.push_back({(double)(b & 1), (double)((b >> 1) & 1), (double)((b >> 2) & 1)}); } } builder.consider(p, 1.0); } } const double sq2 = sqrt(2.0), sq3 = sqrt(3.0); { int k = 1; while (k * k * k < n) ++k; vector<Vec3> p; for (int i = 0; i < k; ++i) for (int j = 0; j < k; ++j) for (int l = 0; l < k; ++l) p.push_back({(double)i, (double)j, (double)l}); p = Builder::centralSubset(p, n, {(k - 1) * 0.5, (k - 1) * 0.5, (k - 1) * 0.5}); builder.consider(p, 1.0); } { int m = 0; while (fccBlockCount(m) < n) ++m; vector<Vec3> p; for (int i = 0; i <= m; ++i) for (int j = 0; j <= m; ++j) for (int l = 0; l <= m; ++l) if (((i + j + l) & 1) == 0) p.push_back({i / sq2, j / sq2, l / sq2}); Vec3 mid{m / (2.0 * sq2), m / (2.0 * sq2), m / (2.0 * sq2)}; p = Builder::centralSubset(p, n, mid); builder.consider(p, 1.0); } { int m = 0; while (bccBlockCount(m) < n) ++m; vector<Vec3> p; for (int i = 0; i <= m; ++i) for (int j = 0; j <= m; ++j) for (int l = 0; l <= m; ++l) { int pi = (i % 2 + 2) % 2; int pj = (j % 2 + 2) % 2; int pl = (l % 2 + 2) % 2; if (pi == pj && pj == pl) p.push_back({i / sq3, j / sq3, l / sq3}); } Vec3 mid{m / (2.0 * sq3), m / (2.0 * sq3), m / (2.0 * sq3)}; p = Builder::centralSubset(p, n, mid); builder.consider(p, 1.0); } int M = (int)ceil(cbrt((double)n)) + 3; vector<Vec3> sc, fcc, bcc, hcp, abc; sc.reserve((2 * M + 1) * (2 * M + 1) * (2 * M + 1)); fcc.reserve(sc.capacity() / 2 + 1); bcc.reserve(sc.capacity() / 3 + 1); for (int i = -M; i <= M; ++i) { for (int j = -M; j <= M; ++j) { for (int k = -M; k <= M; ++k) { sc.push_back({(double)i, (double)j, (double)k}); if (((i + j + k) & 1) == 0) { fcc.push_back({i / sq2, j / sq2, k / sq2}); } int pi = (i % 2 + 2) % 2; int pj = (j % 2 + 2) % 2; int pk = (k % 2 + 2) % 2; if (pi == pj && pj == pk) { bcc.push_back({i / sq3, j / sq3, k / sq3}); } } } } const double sy = sq3 / 2.0; const double h = sqrt(2.0 / 3.0); const Vec3 shiftsABC[3] = { {0.0, 0.0, 0.0}, {0.5, sq3 / 6.0, 0.0}, {0.5, -sq3 / 6.0, 0.0} }; for (int k = -M; k <= M; ++k) { int km = (k % 3 + 3) % 3; int kh = (k % 2 + 2) % 2; Vec3 shH = kh == 0 ? Vec3{0, 0, 0} : Vec3{0.5, sq3 / 6.0, 0}; Vec3 shA = shiftsABC[km]; for (int i = -M; i <= M; ++i) { for (int j = -M; j <= M; ++j) { double bx = i + 0.5 * j; double by = sy * j; hcp.push_back({bx + shH.x, by + shH.y, h * k}); abc.push_back({bx + shA.x, by + shA.y, h * k}); } } } vector<Vec3> offsets = { {0.0, 0.0, 0.0}, {0.5, 0.5, 0.5}, {0.3535533905932738, 0.3535533905932738, 0.3535533905932738}, {0.23, 0.37, 0.41}, {-0.31, 0.17, 0.29} }; builder.processBase(sc, 1.0, offsets); builder.processBase(fcc, 1.0, offsets); builder.processBase(bcc, 1.0, offsets); builder.processBase(hcp, 1.0, offsets); builder.processBase(abc, 1.0, offsets); if (n <= 1024) { auto rotated = [&](double ax, double ay, double az, double angle) { Vec3 axis{ax, ay, az}; double len = sqrt(norm2(axis)); axis = axis * (1.0 / len); double c = cos(angle), s = sin(angle); vector<Vec3> out; out.reserve(fcc.size()); for (const auto& p : fcc) { Vec3 q = p * c + cross(axis, p) * s + axis * (dot(axis, p) * (1.0 - c)); out.push_back(q); } return out; }; vector<Vec3> r1 = rotated(0, 0, 1, acos(-1.0) / 4.0); vector<Vec3> r2 = rotated(1, 2, 3, 0.73); vector<Vec3> smallOffsets = {offsets[0], offsets[1], offsets[3]}; builder.processBase(r1, 1.0, smallOffsets); builder.processBase(r2, 1.0, smallOffsets); } builder.finishLargeCandidate(); if (builder.best.empty()) { largeFallback(n); return 0; } bool doOptimize = n <= 768 && n > 4 && n != 8; if (doOptimize) { mt19937_64 rng(0x9e3779b97f4a7c15ULL ^ (unsigned long long)n); double seconds = n <= 64 ? 2.0 : (n <= 256 ? 1.6 : 0.8); auto deadline = chrono::steady_clock::now() + chrono::milliseconds((int)(seconds * 1000)); Optimizer optimizer(n, builder.best, builder.bestR, rng, deadline); optimizer.run(); } cout << setprecision(17); for (auto p : builder.best) { p.x = max(0.0, min(1.0, p.x)); p.y = max(0.0, min(1.0, p.y)); p.z = max(0.0, min(1.0, p.z)); cout << p.x << ' ' << p.y << ' ' << p.z << '\n'; } return 0; }
Kimi K3.1 Pro(high) · 采样 1 · 395 行
#include <bits/stdc++.h> using namespace std; struct Point { double x, y, z; }; static inline double dist2(const Point& a, const Point& b) { double x = a.x - b.x, y = a.y - b.y, z = a.z - b.z; return x*x + y*y + z*z; } struct Solver { int n; vector<Point> bestQ; double bestD = -1.0; mt19937_64 rng{123456789ULL}; chrono::steady_clock::time_point started = chrono::steady_clock::now(); double elapsed() const { return chrono::duration<double>(chrono::steady_clock::now() - started).count(); } double minimumDistance(const vector<Point>& q) const { double md = 1e100; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { md = min(md, dist2(q[i], q[j])); } } return sqrt(max(0.0, md)); } void normalize(vector<Point>& q) const { double mn[3] = {1e100, 1e100, 1e100}; double mx[3] = {-1e100, -1e100, -1e100}; for (const auto& p : q) { mn[0] = min(mn[0], p.x); mx[0] = max(mx[0], p.x); mn[1] = min(mn[1], p.y); mx[1] = max(mx[1], p.y); mn[2] = min(mn[2], p.z); mx[2] = max(mx[2], p.z); } for (auto& p : q) { double v[3] = {p.x, p.y, p.z}; for (int k = 0; k < 3; ++k) { if (mx[k] - mn[k] < 1e-14) v[k] = 0.5; else v[k] = (v[k] - mn[k]) / (mx[k] - mn[k]); v[k] = max(0.0, min(1.0, v[k])); } p = {v[0], v[1], v[2]}; } } void addCandidate(const vector<Point>& q) { if ((int)q.size() != n) return; double d = minimumDistance(q); if (d > bestD) { bestD = d; bestQ = q; } } struct Spec { int a, b, c; long long count; double d; }; static long long latticeCount(int type, int a, int b, int c) { long long nx = a + 1LL, ny = b + 1LL, nz = c + 1LL; if (type == 0) return nx * ny * nz; if (type == 1) return (nx * ny * nz + 1) / 2; long long e = (nx + 1) / 2 * ((ny + 1) / 2) * ((nz + 1) / 2); long long o = (nx / 2) * (ny / 2) * (nz / 2); return e + o; } static double theoreticalDistance(int type, int a, int b, int c) { double x = 1.0 / a, y = 1.0 / b, z = 1.0 / c; if (type == 0) return min(x, min(y, z)); if (type == 1) { return min(sqrt(x*x + y*y), min(sqrt(x*x + z*z), sqrt(y*y + z*z))); } return min(sqrt(x*x + y*y + z*z), min(2*x, min(2*y, 2*z))); } vector<Spec> specifications(int type) const { double multiplier = type == 0 ? 1.0 : (type == 1 ? 2.0 : 4.0); int root = (int)round(cbrt(multiplier * (double)n)); int lo = max(1, root - 8), hi = root + 10; vector<Spec> v; for (int a = lo; a <= hi; ++a) { for (int b = a; b <= hi; ++b) { for (int c = b; c <= hi; ++c) { long long cnt = latticeCount(type, a, b, c); if (cnt >= n) { v.push_back({a, b, c, cnt, theoreticalDistance(type, a, b, c)}); } } } } sort(v.begin(), v.end(), [](const Spec& a, const Spec& b) { return a.d > b.d; }); return v; } vector<Point> makeLattice(const Spec& s, int type, int variant) const { vector<Point> raw; raw.reserve((size_t)s.count); for (int x = 0; x <= s.a; ++x) { for (int y = 0; y <= s.b; ++y) { for (int z = 0; z <= s.c; ++z) { bool ok; if (type == 0) ok = true; else if (type == 1) ok = ((x + y + z) & 1) == 0; else ok = ((x & 1) == (y & 1)) && ((x & 1) == (z & 1)); if (ok) raw.push_back({(double)x, (double)y, (double)z}); } } } if ((int)raw.size() < n) return {}; if ((int)raw.size() > n) { vector<pair<double,int>> order; order.reserve(raw.size()); for (int i = 0; i < (int)raw.size(); ++i) { double nx = (raw[i].x - 0.5*s.a) / (0.5*s.a); double ny = (raw[i].y - 0.5*s.b) / (0.5*s.b); double nz = (raw[i].z - 0.5*s.c) / (0.5*s.c); nx = fabs(nx); ny = fabs(ny); nz = fabs(nz); double score; if (variant == 1) score = nx*nx + ny*ny + nz*nz; else if (variant == 2) score = max(nx, max(ny, nz)); else { double xx = nx*nx, yy = ny*ny, zz = nz*nz; xx *= xx; yy *= yy; zz *= zz; score = xx*xx + yy*yy + zz*zz; } uint64_t h = (uint64_t)(i + 1) * 11995408973635179863ULL; score += 1e-11 * (double)(h >> 11) / (double)(1ULL << 53); order.push_back({score, i}); } sort(order.begin(), order.end()); vector<Point> selected; selected.reserve(n); for (int i = 0; i < n; ++i) selected.push_back(raw[order[i].second]); raw.swap(selected); } normalize(raw); return raw; } void addHCP() { if (n < 24 || n > 5000) return; int m = (int)ceil(cbrt((double)n) * 1.35) + 5; vector<Point> raw; const double sy = sqrt(3.0) / 2.0; const double sz = sqrt(2.0 / 3.0); for (int k = -m; k <= m; ++k) { double ox = 0.0, oy = 0.0; if (abs(k) & 1) ox = 0.5, oy = sqrt(3.0) / 6.0; for (int j = -m; j <= m; ++j) { for (int i = -m; i <= m; ++i) { raw.push_back({i + 0.5*j + ox, sy*j + oy, sz*k}); } } } int variants = n > 2000 ? 2 : 3; for (int variant = 0; variant < variants; ++variant) { if (elapsed() > 3.6) return; double wx = 1.0, wy = 1.0, wz = 1.0; if (variant == 1) wx = 0.90, wz = 1.10; if (variant == 2) wx = 1.10, wy = 0.90; vector<pair<double,int>> order; order.reserve(raw.size()); for (int i = 0; i < (int)raw.size(); ++i) { double x = fabs(raw[i].x) * wx; double y = fabs(raw[i].y) * wy; double z = fabs(raw[i].z) * wz; double score; if (variant == 0) score = max(x, max(y, z)); else { double xx=x*x, yy=y*y, zz=z*z; xx*=xx; yy*=yy; zz*=zz; score = xx*xx + yy*yy + zz*zz; } order.push_back({score, i}); } sort(order.begin(), order.end()); vector<Point> q; q.reserve(n); for (int i = 0; i < n; ++i) q.push_back(raw[order[i].second]); normalize(q); addCandidate(q); } } struct PairInfo { int i, j; double d, ux, uy, uz; }; void optimizeRun(vector<Point> q, int iterations, double alpha0, double alpha1, const vector<pair<int,int>>& pairs) { int m = pairs.size(); vector<Point> g(n); vector<double> mx(n*3, 0.0), vx(n*3, 0.0); vector<PairInfo> info(m); uniform_real_distribution<double> U(0.0, 1.0); for (int it = 0; it < iterations; ++it) { if ((it & 15) == 0 && elapsed() > 4.85) return; double dmin = 1e100; for (int k = 0; k < m; ++k) { int i = pairs[k].first, j = pairs[k].second; double dx = q[i].x - q[j].x; double dy = q[i].y - q[j].y; double dz = q[i].z - q[j].z; double d = sqrt(dx*dx + dy*dy + dz*dz); if (d < 1e-13) { dx = U(rng) - 0.5; dy = U(rng) - 0.5; dz = U(rng) - 0.5; d = sqrt(dx*dx + dy*dy + dz*dz); } dmin = min(dmin, d); info[k] = {i, j, d, dx/d, dy/d, dz/d}; } if (dmin > bestD) { bestD = dmin; bestQ = q; } for (int i = 0; i < n; ++i) g[i] = {0.0, 0.0, 0.0}; double f = iterations <= 1 ? 1.0 : (double)it / (iterations - 1); double alpha = exp(log(alpha0) + f * log(alpha1 / alpha0)); double sum = 0.0; vector<double> weights(m); for (int k = 0; k < m; ++k) { double w = exp(-alpha * (info[k].d - dmin)); weights[k] = w; sum += w; } for (int k = 0; k < m; ++k) { double w = weights[k] / sum; auto& e = info[k]; g[e.i].x += w*e.ux; g[e.i].y += w*e.uy; g[e.i].z += w*e.uz; g[e.j].x -= w*e.ux; g[e.j].y -= w*e.uy; g[e.j].z -= w*e.uz; } double lr = 0.018 * (1.0 - f) + 0.00018; double b1 = 0.9, b2 = 0.999; double cb1 = 1.0 - pow(b1, it + 1); double cb2 = 1.0 - pow(b2, it + 1); for (int i = 0; i < n; ++i) { double* p[3] = {&q[i].x, &q[i].y, &q[i].z}; double gg[3] = {g[i].x, g[i].y, g[i].z}; for (int k = 0; k < 3; ++k) { int id = 3*i + k; mx[id] = b1*mx[id] + (1-b1)*gg[k]; vx[id] = b2*vx[id] + (1-b2)*gg[k]*gg[k]; double step = lr * (mx[id]/cb1) / (sqrt(vx[id]/cb2) + 1e-8); double nv = *p[k] + step; if (nv < 0.0) nv = 0.0, mx[id] *= 0.5; if (nv > 1.0) nv = 1.0, mx[id] *= 0.5; *p[k] = nv; } } } } void optimize() { if (n > 300 || bestQ.empty()) return; vector<pair<int,int>> pairs; for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) pairs.push_back({i, j}); int runs, iterations; if (n <= 12) runs = 16, iterations = 900; else if (n <= 40) runs = 10, iterations = 700; else if (n <= 90) runs = 6, iterations = 550; else if (n <= 160) runs = 3, iterations = 450; else runs = 1, iterations = 400; uniform_real_distribution<double> U(0.0, 1.0); for (int run = 0; run < runs; ++run) { if (elapsed() > 4.75) break; vector<Point> q; if (run == 0) { q = bestQ; } else if (run & 1) { q.resize(n); for (auto& p : q) p = {U(rng), U(rng), U(rng)}; } else { q = bestQ; double amplitude = (run % 4 == 0) ? 0.04 : 0.12; for (auto& p : q) { p.x = max(0.0, min(1.0, p.x + amplitude*(2*U(rng)-1))); p.y = max(0.0, min(1.0, p.y + amplitude*(2*U(rng)-1))); p.z = max(0.0, min(1.0, p.z + amplitude*(2*U(rng)-1))); } } optimizeRun(q, iterations, run == 0 ? 18.0 : 5.0, 5000.0, pairs); } } void solve() { if (n > 6000) { vector<Spec> all; for (int type = 0; type < 3; ++type) { auto v = specifications(type); if (!v.empty()) { Spec s = v[0]; vector<Point> q = makeLattice(s, type, 0); if ((int)q.size() == n && s.d > bestD) { bestD = s.d; bestQ = move(q); } } } if (bestQ.empty()) { int m = (int)ceil(cbrt((double)n)); for (int x = 0; x < m && (int)bestQ.size() < n; ++x) for (int y = 0; y < m && (int)bestQ.size() < n; ++y) for (int z = 0; z < m && (int)bestQ.size() < n; ++z) bestQ.push_back({(double)x/(m-1), (double)y/(m-1), (double)z/(m-1)}); bestD = 1.0 / max(1, m-1); } return; } int m = (int)ceil(cbrt((double)n)); vector<Point> initial; for (int x = 0; x < m && (int)initial.size() < n; ++x) for (int y = 0; y < m && (int)initial.size() < n; ++y) for (int z = 0; z < m && (int)initial.size() < n; ++z) initial.push_back({(double)x/(m-1), (double)y/(m-1), (double)z/(m-1)}); addCandidate(initial); if (n <= 8) { for (int mask = 0; mask < 256; ++mask) { if (__builtin_popcount((unsigned)mask) != n) continue; vector<Point> q; for (int k = 0; k < 8; ++k) if (mask & (1 << k)) { q.push_back({(double)(k&1), (double)((k>>1)&1), (double)((k>>2)&1)}); } addCandidate(q); } } int small = n <= 2000; int limits[3] = {small ? 8 : 4, small ? 16 : 10, small ? 10 : 5}; for (int type = 1; type >= 0; --type) { auto specs = specifications(type); int generated = 0; for (int i = 0; i < (int)specs.size() && generated < limits[type]; ++i) { if (elapsed() > 3.45) break; if (specs[i].count == n && specs[i].d <= bestD) continue; vector<Point> q = makeLattice(specs[i], type, 0); if (!q.empty()) addCandidate(q); ++generated; if (i < 3 && specs[i].count > n && elapsed() <= 3.45) { q = makeLattice(specs[i], type, 1); if (!q.empty()) addCandidate(q); } } } addHCP(); optimize(); } void output() const { double d = max(bestD, 1e-12); double r = d / (2.0 * (1.0 + d)); double scale = 1.0 / (1.0 + d); cout << setprecision(17); for (const auto& p : bestQ) { cout << r + scale*p.x << ' ' << r + scale*p.y << ' ' << r + scale*p.z << '\n'; } } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; if (!(cin >> n)) return 0; Solver solver; solver.n = n; solver.solve(); solver.output(); return 0; }
DeepSeek Flash V4.1-1017 · 采样 1 · 337 行
#include <bits/stdc++.h> using namespace std; struct Vec3 { double x, y, z; }; double computeMinD(const vector<Vec3>& p) { int n = p.size(); double minD = 1e100; for (int i = 0; i < n; ++i) { double b = min({p[i].x, 1.0 - p[i].x, p[i].y, 1.0 - p[i].y, p[i].z, 1.0 - p[i].z}); if (2.0 * b < minD) minD = 2.0 * b; } double min_d2 = 1e100; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { double dx = p[i].x - p[j].x; double dy = p[i].y - p[j].y; double dz = p[i].z - p[j].z; double d2 = dx*dx + dy*dy + dz*dz; if (d2 < min_d2) min_d2 = d2; } } if (n >= 2) { double d = sqrt(min_d2); if (d < minD) minD = d; } return minD; } double computeRadius(const vector<Vec3>& p) { int n = p.size(); double r = 1e100; for (int i = 0; i < n; ++i) { double b = min({p[i].x, 1.0 - p[i].x, p[i].y, 1.0 - p[i].y, p[i].z, 1.0 - p[i].z}); if (b < r) r = b; } for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { double dx = p[i].x - p[j].x; double dy = p[i].y - p[j].y; double dz = p[i].z - p[j].z; double d2 = dx*dx + dy*dy + dz*dz; double d = sqrt(d2); if (d * 0.5 < r) r = d * 0.5; } } return r; } bool relax(vector<Vec3>& p, double D, int maxIter, double tol) { int n = p.size(); if (n <= 1) return true; double r = D * 0.5; double D2 = D * D; double cellSize = max(D, 0.01); int cellsPerDim = max(1, (int)floor(1.0 / cellSize) + 1); int cellCount = cellsPerDim * cellsPerDim * cellsPerDim; vector<Vec3> delta(n); vector<int> cellStart(cellCount + 1); vector<int> cellPoints(n); vector<int> cellOfPoint(n); vector<int> next(cellStart.size()); for (int iter = 0; iter < maxIter; ++iter) { fill(cellStart.begin(), cellStart.end(), 0); for (int i = 0; i < n; ++i) { int cx = (int)(p[i].x / cellSize); if (cx < 0) cx = 0; if (cx >= cellsPerDim) cx = cellsPerDim - 1; int cy = (int)(p[i].y / cellSize); if (cy < 0) cy = 0; if (cy >= cellsPerDim) cy = cellsPerDim - 1; int cz = (int)(p[i].z / cellSize); if (cz < 0) cz = 0; if (cz >= cellsPerDim) cz = cellsPerDim - 1; int idx = (cx * cellsPerDim + cy) * cellsPerDim + cz; cellOfPoint[i] = idx; cellStart[idx + 1]++; } for (int c = 0; c < cellCount; ++c) { cellStart[c + 1] += cellStart[c]; } for (int c = 0; c <= cellCount; ++c) next[c] = cellStart[c]; for (int i = 0; i < n; ++i) { int idx = cellOfPoint[i]; cellPoints[next[idx]++] = i; } for (int i = 0; i < n; ++i) delta[i] = {0, 0, 0}; double maxViol = 0.0; for (int i = 0; i < n; ++i) { double xi = p[i].x, yi = p[i].y, zi = p[i].z; double v; if (xi < r) { v = r - xi; delta[i].x += v; if (v > maxViol) maxViol = v; } else if (xi > 1.0 - r) { v = xi - (1.0 - r); delta[i].x -= v; if (v > maxViol) maxViol = v; } if (yi < r) { v = r - yi; delta[i].y += v; if (v > maxViol) maxViol = v; } else if (yi > 1.0 - r) { v = yi - (1.0 - r); delta[i].y -= v; if (v > maxViol) maxViol = v; } if (zi < r) { v = r - zi; delta[i].z += v; if (v > maxViol) maxViol = v; } else if (zi > 1.0 - r) { v = zi - (1.0 - r); delta[i].z -= v; if (v > maxViol) maxViol = v; } int cx = (int)(xi / cellSize); if (cx < 0) cx = 0; if (cx >= cellsPerDim) cx = cellsPerDim - 1; int cy = (int)(yi / cellSize); if (cy < 0) cy = 0; if (cy >= cellsPerDim) cy = cellsPerDim - 1; int cz = (int)(zi / cellSize); if (cz < 0) cz = 0; if (cz >= cellsPerDim) cz = cellsPerDim - 1; for (int dx = -1; dx <= 1; ++dx) { int nx = cx + dx; if (nx < 0 || nx >= cellsPerDim) continue; for (int dy = -1; dy <= 1; ++dy) { int ny = cy + dy; if (ny < 0 || ny >= cellsPerDim) continue; for (int dz = -1; dz <= 1; ++dz) { int nz = cz + dz; if (nz < 0 || nz >= cellsPerDim) continue; int idx = (nx * cellsPerDim + ny) * cellsPerDim + nz; int start = cellStart[idx]; int end = cellStart[idx + 1]; for (int k = start; k < end; ++k) { int j = cellPoints[k]; if (j <= i) continue; double dxp = xi - p[j].x; double dyp = yi - p[j].y; double dzp = zi - p[j].z; double d2 = dxp*dxp + dyp*dyp + dzp*dzp; if (d2 < D2) { double d = sqrt(max(d2, 1e-24)); double overlap = D - d; if (overlap > maxViol) maxViol = overlap; double inv = 0.5 * overlap / d; delta[i].x += dxp * inv; delta[i].y += dyp * inv; delta[i].z += dzp * inv; delta[j].x -= dxp * inv; delta[j].y -= dyp * inv; delta[j].z -= dzp * inv; } } } } } } if (maxViol < tol) return true; double alpha = 0.6; for (int i = 0; i < n; ++i) { p[i].x += alpha * delta[i].x; p[i].y += alpha * delta[i].y; p[i].z += alpha * delta[i].z; if (p[i].x < 0) p[i].x = 0; else if (p[i].x > 1) p[i].x = 1; if (p[i].y < 0) p[i].y = 0; else if (p[i].y > 1) p[i].y = 1; if (p[i].z < 0) p[i].z = 0; else if (p[i].z > 1) p[i].z = 1; } } return false; } vector<Vec3> optimize(const vector<Vec3>& seed, int n, double& outD) { vector<Vec3> p = seed; double D = computeMinD(p); if (D < 1e-8) D = 1e-8; vector<Vec3> bestP = p; double bestD = D; double growth = 1.02; int maxIterRelax = 20; int steps = 0; while (growth > 1.00005 && steps < 300) { double Dnew = D * growth; bool ok = relax(p, Dnew, maxIterRelax, 1e-10); if (ok) { D = Dnew; bestD = D; bestP = p; } else { p = bestP; D = bestD; growth = 1.0 + (growth - 1.0) * 0.5; } steps++; } for (int iter = 0; iter < 50; ++iter) { double Dnew = bestD * 1.00002; p = bestP; bool ok = relax(p, Dnew, 100, 1e-11); if (ok) { bestD = Dnew; bestP = p; } else { Dnew = bestD * 1.000005; p = bestP; ok = relax(p, Dnew, 100, 1e-11); if (ok) { bestD = Dnew; bestP = p; } else { break; } } } outD = computeMinD(bestP); return bestP; } vector<Vec3> generateFCCSeed(int n, int seedVal, int mode) { int maxDim = max(5, (int)ceil(pow(n, 1.0/3.0) * 3) + 5); long long bestScore = LLONG_MAX; int bestnx = 1, bestny = 1, bestnz = 1; for (int nx = 1; nx <= maxDim; ++nx) { for (int ny = 1; ny <= maxDim; ++ny) { for (int nz = 1; nz <= maxDim; ++nz) { long long count = 4LL * nx * ny * nz; if (count < n) continue; long long extra = count - n; int mn = min({nx, ny, nz}); int mx = max({nx, ny, nz}); int balance = mx - mn; long long score; if (mode == 0) { score = extra * 10 + (long long)balance * balance * 100; } else if (mode == 1) { score = extra * 1000 + (long long)balance * balance * 10; } else { score = extra * 100 + (long long)balance * balance * 50; } if (score < bestScore) { bestScore = score; bestnx = nx; bestny = ny; bestnz = nz; } } } } vector<Vec3> all; all.reserve(4LL * bestnx * bestny * bestnz); for (int i = 0; i < bestnx; ++i) { for (int j = 0; j < bestny; ++j) { for (int k = 0; k < bestnz; ++k) { for (int b = 0; b < 4; ++b) { double bx, by, bz; if (b == 0) { bx = 0; by = 0; bz = 0; } else if (b == 1) { bx = 0; by = 0.5; bz = 0.5; } else if (b == 2) { bx = 0.5; by = 0; bz = 0.5; } else { bx = 0.5; by = 0.5; bz = 0; } double x = (i + bx) / bestnx; double y = (j + by) / bestny; double z = (k + bz) / bestnz; all.push_back({x, y, z}); } } } } mt19937 rng(seedVal + n * 1234567); shuffle(all.begin(), all.end(), rng); all.resize(n); double min_d2 = 1e100; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { double dx = all[i].x - all[j].x; double dy = all[i].y - all[j].y; double dz = all[i].z - all[j].z; double d2 = dx*dx + dy*dy + dz*dz; if (d2 < min_d2) min_d2 = d2; } } double D_pair = sqrt(min_d2); double r0 = D_pair / (2.0 * (1.0 + D_pair)); double s = 1.0 - 2.0 * r0; for (auto& v : all) { v.x = r0 + s * v.x; v.y = r0 + s * v.y; v.z = r0 + s * v.z; } return all; } vector<Vec3> generateRandomSeed(int n, int seedVal) { mt19937 rng(seedVal + n * 987654321); uniform_real_distribution<double> dist(0.05, 0.95); vector<Vec3> p(n); for (int i = 0; i < n; ++i) { p[i] = {dist(rng), dist(rng), dist(rng)}; } return p; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; if (n == 2) { double r = sqrt(3.0) / (2.0 * (1.0 + sqrt(3.0))); cout << fixed << setprecision(12); cout << r << " " << r << " " << r << "\n"; cout << 1.0 - r << " " << 1.0 - r << " " << 1.0 - r << "\n"; return 0; } vector<Vec3> bestP; double bestR = -1; int runs; if (n <= 20) runs = 12; else if (n <= 100) runs = 8; else if (n <= 1000) runs = 4; else runs = 3; for (int run = 0; run < runs; ++run) { vector<Vec3> seed; if (run == 0) { seed = generateFCCSeed(n, run, 0); } else if (run == 1) { seed = generateFCCSeed(n, run, 1); } else if (n <= 100) { seed = generateRandomSeed(n, run); } else { seed = generateFCCSeed(n, run, run % 2); mt19937 rng(run + n * 123); normal_distribution<double> nd(0.0, 0.0015); for (auto& v : seed) { v.x = max(0.0, min(1.0, v.x + nd(rng))); v.y = max(0.0, min(1.0, v.y + nd(rng))); v.z = max(0.0, min(1.0, v.z + nd(rng))); } } double outD; vector<Vec3> p = optimize(seed, n, outD); double r = computeRadius(p); if (r > bestR) { bestR = r; bestP = p; } } cout << fixed << setprecision(12); for (auto& v : bestP) { cout << v.x << " " << v.y << " " << v.z << "\n"; } return 0; }
MiniMax M3.1 Flash · 采样 1 · 906 行
#include <bits/stdc++.h> using namespace std; struct Point { double x = 0.0, y = 0.0, z = 0.0; Point& operator+=(const Point& o) { x += o.x; y += o.y; z += o.z; return *this; } Point& operator-=(const Point& o) { x -= o.x; y -= o.y; z -= o.z; return *this; } Point& operator*=(double s) { x *= s; y *= s; z *= s; return *this; } }; static inline double distance2(const Point& a, const Point& b) { double dx = a.x - b.x; double dy = a.y - b.y; double dz = a.z - b.z; return dx * dx + dy * dy + dz * dz; } static inline Point clampPoint(Point p) { p.x = clamp(p.x, 0.0, 1.0); p.y = clamp(p.y, 0.0, 1.0); p.z = clamp(p.z, 0.0, 1.0); return p; } struct RNG { uint64_t state = 0x9e3779b97f4a7c15ULL; uint64_t next() { uint64_t z = (state += 0x9e3779b97f4a7c15ULL); z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL; z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL; return z ^ (z >> 31); } double uniform() { return double(next() >> 11) * (1.0 / 9007199254740992.0); } int integer(int n) { return int(next() % uint64_t(n)); } }; class SpatialGrid { const vector<Point>& points; double cellSize; int nx, ny, nz; vector<int> head, link; public: SpatialGrid(const vector<Point>& p, double h) : points(p), cellSize(h) { nx = max(1, int(ceil(1.0 / h))); ny = nx; nz = nx; size_t cells = size_t(nx) * ny * nz; head.assign(cells, -1); link.assign(points.size(), -1); for (int i = 0; i < int(points.size()); ++i) { int ix = min(nx - 1, max(0, int(points[i].x / h))); int iy = min(ny - 1, max(0, int(points[i].y / h))); int iz = min(nz - 1, max(0, int(points[i].z / h))); size_t c = (size_t(ix) * ny + iy) * nz + iz; link[i] = head[c]; head[c] = i; } } template<class F> void forEachPair(F&& f) const { double cutoff2 = cellSize * cellSize; for (int i = 0; i < int(points.size()); ++i) { int ix = min(nx - 1, max(0, int(points[i].x / cellSize))); int iy = min(ny - 1, max(0, int(points[i].y / cellSize))); int iz = min(nz - 1, max(0, int(points[i].z / cellSize))); for (int dz = -1; dz <= 1; ++dz) { int z = iz + dz; if (z < 0 || z >= nz) continue; for (int dy = -1; dy <= 1; ++dy) { int y = iy + dy; if (y < 0 || y >= ny) continue; for (int dx = -1; dx <= 1; ++dx) { int x = ix + dx; if (x < 0 || x >= nx) continue; size_t c = (size_t(x) * ny + y) * nz + z; for (int j = head[c]; j != -1; j = link[j]) { if (j <= i) continue; double d2 = distance2(points[i], points[j]); if (d2 < cutoff2) f(i, j, d2); } } } } } } }; static double minimumDistance(const vector<Point>& points) { int n = int(points.size()); if (n < 2) return 0.0; double estimate = pow(sqrt(2.0) / n, 1.0 / 3.0); double h = clamp(1.5 * estimate, 1e-7, 1.0); for (int attempt = 0; attempt < 10; ++attempt) { SpatialGrid grid(points, h); double best = numeric_limits<double>::infinity(); grid.forEachPair([&](int, int, double d2) { best = min(best, d2); }); if (isfinite(best)) return sqrt(best); h *= 2.0; } double best = numeric_limits<double>::infinity(); for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) best = min(best, distance2(points[i], points[j])); return sqrt(best); } static void normalizePoints(vector<Point>& points) { if (points.empty()) return; double mn[3] = { numeric_limits<double>::infinity(), numeric_limits<double>::infinity(), numeric_limits<double>::infinity() }; double mx[3] = { -numeric_limits<double>::infinity(), -numeric_limits<double>::infinity(), -numeric_limits<double>::infinity() }; for (const Point& p : points) { mn[0] = min(mn[0], p.x); mx[0] = max(mx[0], p.x); mn[1] = min(mn[1], p.y); mx[1] = max(mx[1], p.y); mn[2] = min(mn[2], p.z); mx[2] = max(mx[2], p.z); } for (Point& p : points) { double a = (p.x - mn[0]) / max(1e-15, mx[0] - mn[0]); double b = (p.y - mn[1]) / max(1e-15, mx[1] - mn[1]); double c = (p.z - mn[2]) / max(1e-15, mx[2] - mn[2]); p = { mx[0] > mn[0] ? a : 0.5, mx[1] > mn[1] ? b : 0.5, mx[2] > mn[2] ? c : 0.5 }; } } struct PairObservation { int i, j; double d2; }; struct SoftResult { double minimum = numeric_limits<double>::infinity(); double value = -numeric_limits<double>::infinity(); vector<PairObservation> pairs; }; static SoftResult collectSoft(const vector<Point>& points, double sigma, double cutoff) { SoftResult result; SpatialGrid grid(points, cutoff); double sum = 0.0; double minimum = numeric_limits<double>::infinity(); grid.forEachPair([&](int i, int j, double d2) { double d = sqrt(d2); result.pairs.push_back({i, j, d2}); if (sum == 0.0) { minimum = d; sum = 1.0; } else if (d < minimum) { sum = sum * exp((minimum - d) / sigma) + 1.0; minimum = d; } else { sum += exp(-(d - minimum) / sigma); } }); result.minimum = minimum; if (sum > 0.0) result.value = minimum - sigma * log(sum); return result; } static double softValue(const vector<Point>& points, double sigma, double cutoff) { SpatialGrid grid(points, cutoff); double sum = 0.0; double minimum = numeric_limits<double>::infinity(); grid.forEachPair([&](int, int, double d2) { double d = sqrt(d2); if (sum == 0.0) { minimum = d; sum = 1.0; } else if (d < minimum) { sum = sum * exp((minimum - d) / sigma) + 1.0; minimum = d; } else { sum += exp(-(d - minimum) / sigma); } }); if (sum == 0.0) return minimumDistance(points); return minimum - sigma * log(sum); } static void perturbCritical(vector<Point>& points, double amplitude, RNG& rng) { int n = int(points.size()); if (n < 2) return; double dmin = minimumDistance(points); double cutoff = dmin * 1.025 + 1e-12; SpatialGrid grid(points, cutoff); vector<Point> shift(n); grid.forEachPair([&](int i, int j, double d2) { double d = sqrt(d2); if (d > dmin * 1.02 || rng.uniform() > 0.22) return; double inv = 1.0 / max(d, 1e-15); double ux = (points[i].x - points[j].x) * inv; double uy = (points[i].y - points[j].y) * inv; double uz = (points[i].z - points[j].z) * inv; shift[i].x += amplitude * ux; shift[i].y += amplitude * uy; shift[i].z += amplitude * uz; shift[j].x -= amplitude * ux; shift[j].y -= amplitude * uy; shift[j].z -= amplitude * uz; }); for (int i = 0; i < n; ++i) { double len = sqrt(shift[i].x * shift[i].x + shift[i].y * shift[i].y + shift[i].z * shift[i].z); double cap = 3.0 * amplitude; if (len > cap) shift[i] *= cap / len; points[i] = clampPoint(points[i] + shift[i]); } } static void softOptimizeStage(vector<Point>& points, double relativeSigma, int iterations, RNG& rng) { if (iterations <= 0) return; double currentDistance = minimumDistance(points); double sigma = max(1e-12, relativeSigma * currentDistance); int stalled = 0; for (int iteration = 0; iteration < iterations; ++iteration) { double cutoff = min(4.0, currentDistance + 12.0 * sigma); SoftResult current = collectSoft(points, sigma, cutoff); if (current.pairs.empty() || !isfinite(current.minimum)) break; vector<Point> gradient(points.size()); double maxNorm = 0.0; for (const PairObservation& observation : current.pairs) { double d = sqrt(observation.d2); if (d < 1e-15) continue; double weight = exp(-(d - current.minimum) / sigma); int i = observation.i; int j = observation.j; double ux = (points[i].x - points[j].x) / d; double uy = (points[i].y - points[j].y) / d; double uz = (points[i].z - points[j].z) / d; gradient[i].x += weight * ux; gradient[i].y += weight * uy; gradient[i].z += weight * uz; gradient[j].x -= weight * ux; gradient[j].y -= weight * uy; gradient[j].z -= weight * uz; } for (const Point& g : gradient) maxNorm = max(maxNorm, sqrt(g.x * g.x + g.y * g.y + g.z * g.z)); if (maxNorm < 1e-18) { perturbCritical(points, 2e-4 * currentDistance, rng); continue; } double scale = 0.010 * currentDistance / maxNorm; bool accepted = false; const double factors[4] = {1.0, 0.5, 0.25, 0.125}; for (double factor : factors) { vector<Point> trial = points; for (int i = 0; i < int(points.size()); ++i) { trial[i].x += factor * scale * gradient[i].x; trial[i].y += factor * scale * gradient[i].y; trial[i].z += factor * scale * gradient[i].z; trial[i] = clampPoint(trial[i]); } double value = softValue(trial, sigma, cutoff); if (value > current.value + 1e-15) { points.swap(trial); current.value = value; currentDistance = minimumDistance(points); accepted = true; stalled = 0; break; } } if (!accepted) { ++stalled; if (stalled >= 12) { perturbCritical(points, 2.5e-4 * currentDistance, rng); stalled = 0; } } } } static void relaxAtTarget(vector<Point>& points, double target, int iterations) { int n = int(points.size()); double cutoff = target * (1.0 + 1e-10) + 1e-12; for (int iteration = 0; iteration < iterations; ++iteration) { SpatialGrid grid(points, cutoff); vector<Point> movement(n); bool any = false; grid.forEachPair([&](int i, int j, double d2) { double d = sqrt(d2); if (d >= target) return; any = true; double overlap = target - d; double amount = 0.30 * overlap; double inv = 1.0 / max(d, 1e-15); double ux = (points[i].x - points[j].x) * inv; double uy = (points[i].y - points[j].y) * inv; double uz = (points[i].z - points[j].z) * inv; movement[i].x += amount * ux; movement[i].y += amount * uy; movement[i].z += amount * uz; movement[j].x -= amount * ux; movement[j].y -= amount * uy; movement[j].z -= amount * uz; }); if (!any) break; double cap = 0.04 * target; for (int i = 0; i < n; ++i) { double len = sqrt(movement[i].x * movement[i].x + movement[i].y * movement[i].y + movement[i].z * movement[i].z); if (len > cap) movement[i] *= cap / len; points[i] = clampPoint(points[i] + movement[i]); } } } static vector<Point> improveWithTarget(vector<Point> points, int rounds, int iterations) { vector<Point> best = points; double bestDistance = minimumDistance(best); double epsilon = 0.004; for (int round = 0; round < rounds; ++round) { double currentDistance = minimumDistance(points); vector<Point> trial = points; relaxAtTarget(trial, currentDistance * (1.0 + epsilon), iterations); double trialDistance = minimumDistance(trial); if (trialDistance > bestDistance + 1e-13) { best.swap(trial); bestDistance = trialDistance; points = best; epsilon = min(0.010, epsilon * 1.35); } else { points = best; epsilon *= 0.45; if (epsilon < 2e-5) break; } } return best; } static vector<Point> optimizeCandidate(vector<Point> points, int totalIterations, bool fullRun, RNG& rng) { vector<Point> best = points; double bestDistance = minimumDistance(best); vector<Point> work = points; auto runStage = [&](double sigma, int iterations) { softOptimizeStage(work, sigma, iterations, rng); double d = minimumDistance(work); if (d > bestDistance) { bestDistance = d; best = work; } if (d < bestDistance * 0.995) work = best; }; if (fullRun) { int q = totalIterations / 4; runStage(0.060, max(1, int(totalIterations * 0.20))); runStage(0.022, max(1, int(totalIterations * 0.25))); runStage(0.007, max(1, int(totalIterations * 0.30))); runStage(0.002, max(1, q)); } else { runStage(0.040, max(1, int(totalIterations * 0.40))); runStage(0.010, max(1, int(totalIterations * 0.60))); } if (fullRun) { work = improveWithTarget(work, 8, 35); double d = minimumDistance(work); if (d > bestDistance) { bestDistance = d; best = work; } } return best; } static void removeRedundantPoints(vector<Point>& points, int required, RNG& rng) { int total = int(points.size()); if (total <= required) return; normalizePoints(points); double dmin = minimumDistance(points); double cutoff = 1.28 * dmin + 1e-12; SpatialGrid grid(points, cutoff); vector<int> degree(total, 0); grid.forEachPair([&](int, int, double) { ++degree[0]; }); grid.forEachPair([&](int i, int j, double) { ++degree[i]; ++degree[j]; }); vector<pair<double, int>> order; order.reserve(total); for (int i = 0; i < total; ++i) order.push_back({double(degree[i]) + 0.85 * rng.uniform(), i}); sort(order.begin(), order.end(), [](const auto& a, const auto& b) { if (a.first != b.first) return a.first > b.first; return a.second < b.second; }); int remove = total - required; vector<bool> deleted(total, false); for (int i = 0; i < remove; ++i) deleted[order[i].second] = true; vector<Point> kept; kept.reserve(required); for (int i = 0; i < total; ++i) if (!deleted[i]) kept.push_back(points[i]); points.swap(kept); normalizePoints(points); } struct FccSpec { int a, b, c, parity; int count; double theoreticalDistance; double score; }; static int parityCount(int a, int b, int c, int parity) { long long total = 1LL * a * b * c; int sign = (a & 1) * (b & 1) * (c & 1); int even = int((total + sign) / 2); return parity == 0 ? even : int(total - even); } static int cornerCount(int a, int b, int c, int parity) { int count = 0; for (int x = 0; x < 2; ++x) for (int y = 0; y < 2; ++y) for (int z = 0; z < 2; ++z) if (((x ? a - 1 : 0) + (y ? b - 1 : 0) + (z ? c - 1 : 0)) % 2 == parity) ++count; return count; } static vector<FccSpec> findFccSpecs(int n) { int base = max(2, int(ceil(cbrt(2.0 * n)))); int low = max(2, base - 6); int high = base + 6; vector<FccSpec> specs; for (int a = low; a <= high; ++a) { for (int b = low; b <= high; ++b) { for (int c = low; c <= high; ++c) { long long cells = 1LL * a * b * c; if (cells > 3LL * n) continue; double sa = 1.0 / max(1, a - 1); double sb = 1.0 / max(1, b - 1); double sc = 1.0 / max(1, c - 1); double delta = numeric_limits<double>::infinity(); if (a >= 3) delta = min(delta, 2.0 * sa); if (b >= 3) delta = min(delta, 2.0 * sb); if (c >= 3) delta = min(delta, 2.0 * sc); if (a >= 2 && b >= 2) delta = min(delta, hypot(sa, sb)); if (a >= 2 && c >= 2) delta = min(delta, hypot(sa, sc)); if (b >= 2 && c >= 2) delta = min(delta, hypot(sb, sc)); if (!isfinite(delta)) continue; for (int parity = 0; parity < 2; ++parity) { int count = parityCount(a, b, c, parity); if (count < n || count > int(1.55 * n)) continue; double holes = double(count - n) / n; int corners = cornerCount(a, b, c, parity); double score = delta * (1.0 - 0.12 * holes) + 1e-7 * corners; specs.push_back({a, b, c, parity, count, delta, score}); } } } } sort(specs.begin(), specs.end(), [](const FccSpec& x, const FccSpec& y) { return x.score > y.score; }); vector<FccSpec> unique; set<array<int, 3>> used; for (const FccSpec& s : specs) { array<int, 3> key = {s.a, s.b, s.c}; if (used.insert(key).second) unique.push_back(s); } return unique; } static vector<Point> makeFcc(const FccSpec& spec, int n, RNG& rng) { vector<Point> points; points.reserve(spec.count); for (int i = 0; i < spec.a; ++i) { for (int j = 0; j < spec.b; ++j) { for (int k = 0; k < spec.c; ++k) { if ((i + j + k) % 2 != spec.parity) continue; points.push_back({ double(i) / max(1, spec.a - 1), double(j) / max(1, spec.b - 1), double(k) / max(1, spec.c - 1) }); } } } removeRedundantPoints(points, n, rng); return points; } static Point rotatePoint(Point p, double ax, double ay, double az) { double cx = cos(ax), sx = sin(ax); double cy = cos(ay), sy = sin(ay); double cz = cos(az), sz = sin(az); double y = p.y * cx - p.z * sx; double z = p.y * sx + p.z * cx; p.y = y; p.z = z; double x = p.x * cy + p.z * sy; z = -p.x * sy + p.z * cy; p.x = x; p.z = z; x = p.x * cz - p.y * sz; y = p.x * sz + p.y * cz; p.x = x; p.y = y; return p; } static void improveOrientation(vector<Point>& points, RNG& rng) { if (points.size() > 10000) return; vector<Point> best = points; double bestDistance = minimumDistance(best); for (int trial = 0; trial < 24; ++trial) { double ax = (2.0 * rng.uniform() - 1.0) * 0.10; double ay = (2.0 * rng.uniform() - 1.0) * 0.10; double az = (2.0 * rng.uniform() - 1.0) * 0.10; vector<Point> rotated; rotated.reserve(points.size()); for (Point p : points) rotated.push_back(rotatePoint(p, ax, ay, az)); normalizePoints(rotated); double d = minimumDistance(rotated); if (d > bestDistance + 1e-13) { bestDistance = d; best.swap(rotated); } } points.swap(best); } static vector<Point> makeSimpleGrid(int a, int b, int c) { vector<Point> points; points.reserve(a * b * c); for (int i = 0; i < a; ++i) for (int j = 0; j < b; ++j) for (int k = 0; k < c; ++k) points.push_back({ double(i) / max(1, a - 1), double(j) / max(1, b - 1), double(k) / max(1, c - 1) }); normalizePoints(points); return points; } static vector<array<int, 3>> findSimpleGrids(int n) { vector<pair<double, array<int, 3>>> grids; for (int a = 2; 1LL * a * a * a <= n; ++a) { if (n % a != 0) continue; int remaining = n / a; for (int b = a; 1LL * b * b <= remaining; ++b) { if (remaining % b != 0) continue; int c = remaining / b; double score = 1.0 / max(1, a - 1); score = min(score, 1.0 / max(1, b - 1)); score = min(score, 1.0 / max(1, c - 1)); grids.push_back({score, {a, b, c}}); } } sort(grids.begin(), grids.end(), [](const auto& x, const auto& y) { return x.first > y.first; }); vector<array<int, 3>> result; for (const auto& g : grids) { if (result.size() >= 3) break; result.push_back(g.second); } return result; } static vector<Point> makeBcc(int a, int b, int c, int n, RNG& rng) { vector<Point> points; points.reserve(a * b * c + (a - 1) * (b - 1) * (c - 1)); for (int i = 0; i < a; ++i) for (int j = 0; j < b; ++j) for (int k = 0; k < c; ++k) points.push_back({ double(i) / max(1, a - 1), double(j) / max(1, b - 1), double(k) / max(1, c - 1) }); for (int i = 0; i + 1 < a; ++i) for (int j = 0; j + 1 < b; ++j) for (int k = 0; k + 1 < c; ++k) points.push_back({ (i + 0.5) / max(1, a - 1), (j + 0.5) / max(1, b - 1), (k + 0.5) / max(1, c - 1) }); removeRedundantPoints(points, n, rng); return points; } enum CandidateType { FCC_CANDIDATE, SIMPLE_CANDIDATE, BCC_CANDIDATE }; struct Candidate { vector<Point> points; double distance; int type; double holes; }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; if (n <= 9) { vector<Point> points; if (n == 2) { points = {{0, 0, 0}, {1, 1, 1}}; } else if (n == 3) { points = {{0, 0, 0}, {1, 1, 0}, {1, 0, 1}}; } else if (n == 4) { points = {{0, 0, 0}, {1, 1, 0}, {1, 0, 1}, {0, 1, 1}}; } else if (n <= 8) { vector<Point> vertices = { {0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}, {1, 0, 1}, {0, 1, 1}, {1, 1, 1} }; points.assign(vertices.begin(), vertices.begin() + n); } else { points = { {0, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {1, 1, 0}, {1, 0, 1}, {0, 1, 1}, {1, 1, 1}, {0.5, 0.5, 0.5} }; } double d = minimumDistance(points); double radius = d / (2.0 * (1.0 + d)); double scale = 1.0 / (1.0 + d); cout << setprecision(17); for (const Point& p : points) { cout << radius + scale * p.x << ' ' << radius + scale * p.y << ' ' << radius + scale * p.z << '\n'; } return 0; } RNG rng; vector<Candidate> candidates; vector<FccSpec> fccSpecs = findFccSpecs(n); int fccLimit = n > 10000 ? 2 : min<int>(8, fccSpecs.size()); for (int i = 0; i < fccLimit; ++i) { vector<Point> points = makeFcc(fccSpecs[i], n, rng); improveOrientation(points, rng); candidates.push_back({ move(points), minimumDistance(points), FCC_CANDIDATE, double(fccSpecs[i].count - n) / n }); } if (n <= 10000) { for (const array<int, 3>& dims : findSimpleGrids(n)) { vector<Point> points = makeSimpleGrid(dims[0], dims[1], dims[2]); candidates.push_back({ move(points), minimumDistance(points), SIMPLE_CANDIDATE, 0.0 }); } } if (n <= 10000) { int base = max(2, int(ceil(cbrt(0.5 * n)))); vector<pair<double, array<int, 3>>> bccSpecs; for (int a = max(2, base - 2); a <= base + 3; ++a) for (int b = max(2, base - 2); b <= base + 3; ++b) for (int c = max(2, base - 2); c <= base + 3; ++c) { int count = a * b * c + (a - 1) * (b - 1) * (c - 1); if (count < n || count > int(1.45 * n)) continue; double sa = 1.0 / max(1, a - 1); double sb = 1.0 / max(1, b - 1); double sc = 1.0 / max(1, c - 1); double delta = sqrt(0.25 * (sa * sa + sb * sb + sc * sc)); delta = min(delta, min({sa, sb, sc})); double holes = double(count - n) / n; bccSpecs.push_back({delta * (1.0 - holes), {a, b, c}}); } sort(bccSpecs.begin(), bccSpecs.end(), [](const auto& x, const auto& y) { return x.first > y.first; }); for (int i = 0; i < min<int>(2, bccSpecs.size()); ++i) { auto d = bccSpecs[i].second; vector<Point> points = makeBcc(d[0], d[1], d[2], n, rng); candidates.push_back({ move(points), minimumDistance(points), BCC_CANDIDATE, 0.0 }); } } sort(candidates.begin(), candidates.end(), [](const Candidate& a, const Candidate& b) { return a.distance > b.distance; }); vector<int> selected; auto addCandidate = [&](int index) { if (index < 0 || index >= int(candidates.size())) return; if (find(selected.begin(), selected.end(), index) == selected.end()) selected.push_back(index); }; addCandidate(0); int minimumHolesFcc = -1; for (int i = 0; i < int(candidates.size()); ++i) { if (candidates[i].type != FCC_CANDIDATE) continue; if (minimumHolesFcc < 0 || candidates[i].holes < candidates[minimumHolesFcc].holes) minimumHolesFcc = i; } addCandidate(minimumHolesFcc); for (int i = 0; i < int(candidates.size()) && selected.size() < 4; ++i) addCandidate(i); int shortIterations = n < 500 ? 400 : n < 5000 ? 100 : 25; vector<Candidate> improved; for (int index : selected) { vector<Point> points = optimizeCandidate( candidates[index].points, shortIterations, false, rng ); improved.push_back({ move(points), minimumDistance(points), candidates[index].type, candidates[index].holes }); } sort(improved.begin(), improved.end(), [](const Candidate& a, const Candidate& b) { return a.distance > b.distance; }); int fullIterations = n < 500 ? 1400 : n < 5000 ? 500 : 80; vector<Point> best = optimizeCandidate( improved.front().points, fullIterations, true, rng ); double bestDistance = minimumDistance(best); if (n <= 5000) { vector<Point> trial = best; perturbCritical(trial, 0.002 * bestDistance, rng); trial = optimizeCandidate(trial, max(80, fullIterations / 4), false, rng); trial = improveWithTarget(trial, 5, 25); double d = minimumDistance(trial); if (d > bestDistance) { best.swap(trial); bestDistance = d; } } double finalDistance = minimumDistance(best); double radius = finalDistance / (2.0 * (1.0 + finalDistance)); double scale = 1.0 / (1.0 + finalDistance); cout << setprecision(17); for (const Point& p : best) { cout << radius + scale * p.x << ' ' << radius + scale * p.y << ' ' << radius + scale * p.z << '\n'; } return 0; }