Description
有一个球形空间产生器能够在n维空间中产生一个坚硬的球体。现在,你被困在了这个n维球体中,你只知道球面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧毁这个球形空间产生器。
Input
第一行是一个整数,n。接下来的n+1行,每行有n个实数,表示球面上一点的n维坐标。每一个实数精确到小数点后6位,且其绝对值都不超过20000。
Output
有且只有一行,依次给出球心的n维坐标(n个实数),两个实数之间用一个空格隔开。每个实数精确到小数点后3位。数据保证有解。你的答案必须和标准输出一模一样才能够得分。
Sample Input
2
0.0 0.0
-1.0 1.0
1.0 0.0
Sample Output
0.500 1.500
HINT
数据规模:
对于40%的数据,1<=n<=3
对于100%的数据,1<=n<=10
提示:给出两个定义:
1、 球心:到球面上任意一点距离都相等的点。
2、 距离:设两个n为空间上的点A, B的坐标为(a1, a2, …, an), (b1, b2, …, bn),则AB的距离定义为:dist = sqrt( (a1-b1)^2 + (a2-b2)^2 + … + (an-bn)^2 )
Source
设解向量是 x1,x2...xn
以
n=2
,解向量是
x,y
为例。
因为半径一样,所以
(a−x)2+(b−y)2=(c−x)2+(d−y)2
解出来是 (a2−c2)+(b2−d2)=2(a−c)x+2(b−c)y
加好几维同理。
然后就是高斯消元了,抄的黄学长的。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
const int SZ = 30;
const int INF = 1000000010;
const double eps = 1e-6;
double g[SZ][SZ];
double pf(double x)
{
return x * x;
}
bool gauss(double g[][SZ],int n,double ans[])
{
int now = 1;
for(int i = 1;i < n;i ++)
{
int to;
for(to = now;to < n;to ++)
if(fabs(g[to][i]) > eps) break;
if(to >= n) continue;
if(to != now)
for(int j = 1;j <= n;j ++)
swap(g[to][j],g[now][j]);
double t = g[now][i];
for(int j = 1;j <= n;j ++)
g[now][j] /= t;
for(int j = 1;j < n;j ++)
if(j != now)
{
double num = g[j][i];
for(int k = 1;k <= n;k ++)
g[j][k] -= num * g[now][k];
}
now ++;
}
for(int i = 1;i < n;i ++)
ans[i] = g[i][n];
for(int i = now;i < n;i ++)
if(fabs(g[i][n]) > eps) return false;
return true;
}
double in[SZ][SZ],ans[SZ];
int main()
{
int n;
scanf("%d",&n);
for(int i = 1;i <= n + 1;i ++)
for(int j = 1;j <= n;j ++)
scanf("%lf",&in[i][j]);
for(int i = 1;i <= n;i ++)
for(int j = 1;j <= n;j ++)
{
g[i][n + 1] += pf(in[i][j]) - pf(in[i + 1][j]);
g[i][j] = 2 * (in[i][j] - in[i + 1][j]);
}
gauss(g,n + 1,ans);
for(int i = 1;i <= n;i ++)
printf("%.3lf%c",ans[i],i == n ? '\n' : ' ');
return 0;
}