# [BZOJ 1013][JSOI 2008] 球形空间产生器sphere 题解（高斯消元）

## [BZOJ 1013][JSOI 2008] 球形空间产生器sphere

Input

Output

### Solution

1.考虑构造对于不同的店同样结构的方程。

sqrt ( ( ai1 - x1 ) ^ 2 + ( ai2 - x2 ) ^ 2 + ... + ( ain - xn ) ^2 ) = R;

2.考虑构造方程组

-2 ( ai1 * x1 + ai2 * x2 + ... + ain * xn )
+( ai1 ^ 2 + ai2 ^ 2 + ... + ain ^ 2 )
+( x1 ^ 2 + x2 ^ 2 + ... + xn ^ 2 )
= R ^ 2;

2 ( ai1 * x1 + ai2 * x2 + ... + ain * xn )
= ( ai1 ^ 2 + ai2 ^ 2 + ... + ain ^ 2 )
- ( a11 ^ 2 + a12 ^ 2 + ... + a1n ^ 2 )
- 2 ( a11 * x1 + a12 * x2 + ... + a1n * xn ) ;

### Code

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;

const int max_n=15;
double a[max_n][max_n+1],v[max_n],del[max_n],x;
int n,w[max_n];

inline double sqr(double x){return x*x;}

void init(){
for(int i=1;i<=n;++i)scanf("%lf",&del[i]);
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
scanf("%lf",&x);
a[i][n+1]+=sqr(x)-sqr(del[j]);
a[i][j]=2*(x-del[j]);
}
}
}

void gauss(){
double eps=1e-6;
for(int i=1;i<=n;++i){//enumerate the equation;
int p=0;          //Record the position of the largest number;
double mx=0;      //Recording the largest number;
for(int j=1;j<=n;++j)
if(fabs(a[i][j])-eps>mx){
mx=fabs(a[i][j]);p=j;//fabs() returns the absolute value of float;
}
w[i]=p;
for(int j=1;j<=n;++j)
if(i!=j){       //other equations
double t=a[j][p]/a[i][p];
for(int k=1;k<=n+1;++k)//n+1 is important
a[j][k]-=a[i][k]*t;
}
}
for(int i=1;i<=n;++i) v[w[i]]=a[i][n+1]/a[i][w[i]];
for(int i=1;i<=n;++i) printf("%.3lf ",v[i]);
}

int main(){
scanf("%d",&n);
init();
gauss();
return 0;
}
posted @ 2018-05-02 21:11  COLINGAO  阅读(...)  评论(...编辑  收藏