【BZOJ】【P1013】【JSOI2008】【球形空间产生器sphere】【题解】【高斯消元】

传送门:

http://www.lydsy.com/JudgeOnline/problem.php?id=1013

设给定的矩阵为a[n+1][n]

求sigma(xi-a[k][i])^2=C (C为常数)

=>sigma(xi^2-2a[k][i]+a[k][i]^2)=C

=>sigma 2*xi*(a[k+1][i]-a[k][i]) = sigma(a[k][i]^2-a[k+1][i]^2)  (用k式减k+1式)

Gauss消元即可

Code:

#include<cstdio>
#include<cmath>
#include<iomanip>
#include<iostream>
#include<algorithm>
using namespace std;
double a[21][21];
double A[21][21];
int n;
void Gauss(){
	int r,k;
	for(int i=0;i<n;i++){
		r=i;
		for(int j=i+1;j<n;j++)
			if(fabs(A[j][i])>fabs(A[r][i]))r=j;
		if(r!=i)for(int j=0;j<=n;j++)swap(A[i][j],A[r][j]);
		for(int k=i+1;k<n;k++){
			double f=A[k][i]/A[i][i];
			for(int j=i;j<=n;j++)A[k][j]-=f*A[i][j];
		}
	}
	for(int i=n-1;i>=0;i--){
		for(int j=i+1;j<n;j++)
			A[i][n]-=A[j][n]*A[i][j];
		A[i][n]/=A[i][i];
	}
	for(int i=0;i<n-1;i++)
		cout<<fixed<<setprecision(3)<<A[i][n]<<" ";
	cout<<fixed<<setprecision(3)<<A[n-1][n];
		
}
int main(){
	cin>>n;
	for(int i=0;i<=n;i++)
	for(int j=0;j<n;j++)
	cin>>a[i][j];
	
	for(int i=0;i<n;i++){
		double k=0;
		for(int j=0;j<n;j++){
			A[i][j]=2*(a[i][j]-a[i+1][j]);
			k+=(a[i][j]*a[i][j]-a[i+1][j]*a[i+1][j]);
		}
		A[i][n]=k;
	}
	Gauss();
	return 0;
}


你可能感兴趣的:(bzoj,省选)