bzoj1013: [JSOI2008]球形空间产生器sphere
传送门:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1013
思路:肯定是解方程...
好像有哪里不对,二次项很坑爹。
但是题目里有n+1个点,我们还是可以得出n个一次方程的
设球心坐标为(x1,x2,x3...xn)
那么就有
(a1-x1)^2+(a2-x2)^2+...(an-xn)^2=r^2
(b1-x1)^2+(b2-x2)^2+...(bn-xn)^2=r^2
....
只要拿后n个方程分别去减第一个方程,就可以得到n个一次方程了
2*(a1-b1)x1+2*(a2-b2)*x2+.....+2*(an-bn)xn=a1^2-b1^2+a2^2-b2^2....an^2-bn^2
...
高斯消元即可
#include<cmath> #include<cstdio> #include<cstring> #include<algorithm> const int maxn=15; using namespace std; int n;double a[maxn][maxn],f[maxn]; void gauss(){ for (int i=1;i<=n;i++){ double maxs=-1.0;int id; for (int j=i;j<=n;j++) if (fabs(a[j][i])>maxs) maxs=fabs(a[j][i]),id=j; if (id!=i) for (int j=1;j<=n+1;j++) swap(a[id][j],a[i][j]); double t=a[i][i]; for (int j=i+1;j<=n+1;j++) a[i][j]/=t; for (int j=1;j<=n;j++) if (i!=j){ double t=a[j][i]; for (int k=1;k<=n+1;k++) a[j][k]-=t*a[i][k]; } } for (int i=1;i<n;i++) printf("%.3f ",a[i][n+1]); printf("%.3f\n",a[n][n+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 x;scanf("%lf",&x); a[i][j]=2*(x-f[j]); a[i][n+1]+=(x*x)-(f[j]*f[j]); } gauss(); return 0; }