bzoj1013 [JSOI2008]球形空间产生器

Description

  有一个球形空间产生器能够在n维空间中产生一个坚硬的球体。现在,你被困在了这个n维球体中,你只知道球面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧毁这个球形空间产生器。

Input

  第一行是一个整数n(1<=N=10)。接下来的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

  提示:给出两个定义:1、 球心:到球面上任意一点距离都相等的点。2、 距离:设两个n为空间上的点A, B的坐标为(a1, a2, …, an), (b1, b2, …, bn),则AB的距离定义为:dist = sqrt( (a1-b1)^2 + (a2-b2)^2 + … + (an-bn)^2 )

 

正解:高斯消元。

高斯消元入门题。。个人感觉高斯-约当消元更好用吧,代码短一些。。

我们发现列出点到圆心的方程后是n+1个二次方程。然而我们只需把后n个方程都与第一个方程相减就能得到n个一次方程。然后直接套板子就行了。

 

 1 //It is made by wfj_2048~
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <complex>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <cstdio>
 8 #include <vector>
 9 #include <cmath>
10 #include <queue>
11 #include <stack>
12 #include <map>
13 #include <set>
14 #define inf (1e18)
15 #define il inline
16 #define RG register
17 #define ll long long
18 #define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
19 
20 using namespace std;
21 
22 double a[20][20];
23 int n;
24 
25 il int gi(){
26     RG int x=0,q=1; RG char ch=getchar(); while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
27     if (ch=='-') q=-1,ch=getchar(); while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q*x;
28 }
29 
30 il void gauss(){
31     for (RG int i=1;i<=n;++i){
32     RG double maxs=-inf; RG int id;
33     for (RG int j=1;j<=n;++j) if (fabs(a[j][i])>maxs) maxs=fabs(a[j][i]),id=j;
34     if (id!=i) for (RG int j=1;j<=n+1;++j) swap(a[id][j],a[i][j]); RG double t=a[i][i];
35     for (RG int j=i;j<=n+1;++j) a[i][j]/=t;
36     for (RG int j=1;j<=n;++j){
37         if (i==j) continue; t=a[j][i];
38         for (RG int k=1;k<=n+1;++k) a[j][k]-=t*a[i][k];
39     }
40     }
41     for (RG int i=1;i<n;++i) printf("%0.3lf ",a[i][n+1]);
42     printf("%0.3lf\n",a[n][n+1]); return;
43 }
44 
45 il void work(){
46     n=gi(); RG double x;
47     for (RG int i=1;i<=n;++i) scanf("%lf",&a[0][i]);
48     for (RG int i=1;i<=n;++i)
49     for (RG int j=1;j<=n;++j){
50         scanf("%lf",&x);
51         a[i][j]=2*(x-a[0][j]),a[i][n+1]+=x*x-a[0][j]*a[0][j];
52     }
53     gauss(); return;
54 }
55 
56 int main(){
57     File("sphere");
58     work();
59     return 0;
60 }

 

posted @ 2017-03-10 20:42  wfj_2048  阅读(157)  评论(0编辑  收藏  举报