[BZOJ1013][JSOI2008]球形空间产生器sphere(高斯消元)

题目描述

传送门

题解

(x1x)2+(y1y)2+(z1z)2=r2
(x2x)2+(y2y)2+(z2z)2=r2

(xn+1x)2+(yn+1y)2+(zn+1z)2=r2
各项展开
x21+x2+y21+y2+z21+z2=2x1x+2y1y+2z1z
x21+x2+y21+y2+z21+z2=2x2x+2y1y+2z2z

x21+x2+y21+y2+z21+z2=2x1x+2y1y+2z1z
用每个方程与第一个方程左右分别做差就可以把所以未知量的二次项全部消去
x22x21+y22y21+z22z21=2(x2x1)x+2(y2y1)y+2(z2z1)z

x2nx21+y2ny21+z2nz21=2(xnx1)x+2(yny1)y+2(znz1)z
得到n个线性方程
然后用高斯消元求解。

代码

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

const double eps=1e-6;
const int max_n=20;

int n;
double a[max_n][max_n],f[max_n];

inline bool gauss(){
    double t; int now=1,to; 
    for (int i=1;i<=n;++i){
        for (to=now;to<=n;to++) if (fabs(a[to][i])>eps) break;
        if (to>n) continue;
        if (to!=now)
          for (int j=1;j<=n+1;++j)
            swap(a[to][j],a[now][j]);
        t=a[now][i];
        for (int j=1;j<=n+1;j++) a[now][j]/=t;
        for (int j=1;j<=n;++j)
          if (j!=now){
            t=a[j][i];
            for (int k=1;k<=n+1;++k)
              a[j][k]-=t*a[now][k];
          }
        now++;
    }
    for (int i=now;i<=n;++i)
      if (fabs(a[i][n+1])>eps) return 0;
    return 1;
}
int main(){
    scanf("%d",&n);
    for (int i=1;i<=n;++i) scanf("%lf",&f[i]);
    for (int i=1;i<=n;++i)
      for (int j=1;j<=n;++j){
        double t; scanf("%lf",&t);
        a[i][j]=2*(t-f[j]);
        a[i][n+1]+=t*t-f[j]*f[j];
      }
    bool k=gauss();
    for (int i=1;i<=n;++i)
      printf("%0.3lf%c",a[i][n+1]," \n"[i==n]);
}

你可能感兴趣的:(数论,bzoj,JSOI)