---
id: 1205
lang: en
translation_of: index.md
source_sha256: 4e8971b35910239fb59f0b86a11abd41bf8730406e8bb5cf8659c8881e05206c
title: "BZOJ1013: [JSOI2008] Sphere Space Generator — Solution"
description: "Subtracting consecutive sphere equations cancels the constant and leaves a linear system of n equations, solved by Gaussian elimination."
tags: ["OI","Math","Gaussian Elimination"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "上下相减 (subtract top and bottom) rendered as “subtract consecutive ones”."
---

## Solution

This problem is actually just <span class="slang" data-zh="裸题" data-gloss="luǒtí, “naked problem”. A problem that is a direct application of a standard algorithm.">a textbook problem</span> for Gaussian elimination. Following the statement, we can write equations of the following form:

$$
\sum_{i = 1}^n (x_a [i] - x_0 [i])^2 = c
$$

where $c$ is the radius of the sphere. There are $n+1$ such equations in total; we can subtract consecutive ones to cancel the constant, which builds a system of $n$ equations. The final system:

$$
\begin{cases} 2\sum_{i = 1}^n x_1[i] x_2[i] = \sum_{i = 1}^n (x_1[i])^2 - (x_2[i])^2 \\ 2\sum_{i = 1}^n x_2[i] x_3[i] = \sum_{i = 1}^n (x_2[i])^2 - (x_3[i])^2 \\ 2\sum_{i = 1}^n x_3[i] x_4[i] = \sum_{i = 1}^n (x_3[i])^2 - (x_4[i])^2 \\ \dots \end{cases}
$$

Throw it into Gaussian elimination and you’re done.

<!--more-->

## Code

```cpp
// BZ1013.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 12;
double mat[MAX_N][MAX_N], c[MAX_N], raw[MAX_N][MAX_N];
int n;

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n + 1; i++)
        for (int j = 1; j <= n; j++)
            scanf("%lf", &raw[i][j]);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            mat[i][j] = 2 * (raw[i][j] - raw[i + 1][j]),
            c[i] += raw[i][j] * raw[i][j] - raw[i + 1][j] * raw[i + 1][j];
    for (int i = 1; i <= n; i++)
        for (int j = i; j <= n; j++)
            if (fabs(mat[j][i]) > 1e-8)
            {
                for (int k = 1; k <= n; k++)
                    swap(mat[j][k], mat[i][k]);
                swap(c[i], c[j]);
            }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            if (i == j)
                continue;
            double rate = mat[j][i] / mat[i][i];
            for (int k = 1; k <= n; k++)
                mat[j][k] -= rate * mat[i][k];
            c[j] -= rate * c[i];
        }
    for (int i = 1; i <= n; i++)
        printf("%.3lf ", c[i] / mat[i][i]);
    return 0;
}
```
