正题
题目链接:
https://www.lydsy.com/JudgeOnline/problem.php?id=3534
https://www.luogu.org/problem/P4035
题目大意
一个nnn维平面的元,给出圆表面上的n+1n+1n+1个坐标,求圆心位置。
解题思路
对于圆心的第iii维位置xix_ixi有∑j=0n(ai,j−xi)2=C\sum_{j=0}^n (a_{i,j}-x_i)^2=Cj=0∑n(ai,j−xi)2=C
我们将以上公式两两做差,然后可以消去常数CCC。
∑j=1n(ai,j2−ai+1,j2−2xj(ai,j−ai+1,j))=0(i∈[1..n])\sum_{j=1}^n (a_{i,j}^2-a_{i+1,j}^2-2x_j(a_{i,j}-a_{i+1,j}))=0(i\in[1..n])j=1∑n(ai,j2−ai+1,j2−2xj(ai,j−ai+1,j))=0(i∈[1..n])
然后移一下
∑j=1n2(ai,j−ai+1,j)xj=∑j=1n(ai,j2−ai+1,j2)(i∈[1..n])\sum_{j=1}^n 2(a_{i,j}-a{i+1},j)x_j=\sum_{j=1}^n(a_{i,j}^2-a_{i+1,j}^2)(i\in[1..n])j=1∑n2(ai,j−ai+1,j)xj=j=1∑n(ai,j2−ai+1,j2)(i∈[1..n])
然后高斯消元即可。
codecodecode
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=20;
int n;
double r[N][N],a[N][N],b[N];
int main()
{scanf("%d",&n);for(int i=1;i<=n+1;i++)for(int j=1;j<=n;j++)scanf("%lf",&r[i][j]);for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)a[i][j]=2*(r[i][j]-r[i+1][j]),b[i]+=r[i][j]*r[i][j]-r[i+1][j]*r[i+1][j];for(int i=1;i<=n;i++){int z=i;for(int j=i+1;j<=n;j++)if(fabs(a[j][i])>fabs(a[z][i]))z=j;for(int j=1;j<=n;j++)swap(a[i][j],a[z][j]);swap(b[i],b[z]);for(int j=1;j<=n;j++){if(i==j) continue;double rate=a[j][i]/a[i][i];for(int k=i;k<=n;k++)a[j][k]-=rate*a[i][k];b[j]-=rate*b[i];}}for(int i=1;i<=n;i++)printf("%.3lf ",b[i]/a[i][i]);
}