Olympiad in Informatics

BZOJ1013: [JSOI2008] Sphere Space Generator — Solution

Solution

This problem is actually just a textbook problem 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.

Code

// 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;
}