题目 › fcs-48

FrontierCS #48

FrontierCS 算法赛道 v1.1 · 2026-05 · L5 开放式 · C++17 · 时限 1 秒 · 0–100 连续分

查看原题跳到结果

题面

来源: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;
}