[BZOJ 1013] [JSOI2008] spherical space generator

[BZOJ 1013] [JSOI2008] spherical space generator

Face questions

N n + 1 is given a point on the dimensional sphere, the sphere center coordinates seek

analysis

Set sphere center coordinates \ ((x_1, x_2, \ DOTS x_n) \) , since all points on a sphere equidistant to the center of the sphere. Then

The first \ (I \) equations as \ [\ begin {equation} \ sum_ {j = 0} ^ n (a_ {i, j} -x_j) ^ 2 = C ^ 2 \ tag {i} \ end {equation } \]

Wherein \ (C \) is the distance, \ (A_ {I, J} \) is the coordinates of a point. We make the difference between adjacent two equations, erasing \ (x_j ^ 2 \) and \ (C \) . \ ((i) - (i + 1) \) to give

\[\begin{equation}\sum_{j=1}^n (a_{i,j}^2-a_{i+1,j}^2-2x_j(a_{i,j}-a_{i+1,j})) =0 \\ 2(a_{i,j}-a_{i+1,j})x_j =\sum_{j=1}^n (a_{i,j}^2-a_{i+1,j}^2)\end{equation}\]

Gaussian elimination can be direct.

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 10 
using namespace std;
int n;
double a[maxn+5][maxn+5]; 
double mat[maxn+5][maxn+5];
void gauss(int n,int m){
    for(int i=1;i<=n;i++){
        int id=i;
        for(int j=i+1;j<=n;j++){
            if(mat[j][i]>mat[id][i]) id=j;
        }
        for(int k=1;k<=m;k++) swap(mat[i][k],mat[id][k]);
        for(int j=1;j<=n;j++){
            if(j==i) continue;
            double r=mat[j][i]/mat[i][i];
            for(int k=1;k<=m;k++) mat[j][k]-=mat[i][k]*r;
        }
    }
    for(int i=1;i<=n;i++){
        mat[i][m]/=mat[i][i];
    }
} 

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n+1;i++){
        for(int j=1;j<=n;j++){
            scanf("%lf",&a[i][j]);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            mat[i][j]=2*(a[i][j]-a[i+1][j]); 
            mat[i][n+1]+=a[i][j]*a[i][j]-a[i+1][j]*a[i+1][j];
        }
    }
    gauss(n,n+1);
    for(int i=1;i<=n;i++){
        printf("%.3f ",mat[i][n+1]);
    }
} 

Guess you like

Origin www.cnblogs.com/birchtree/p/11788026.html