显示代码纯文本
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef double db;
const int N=1010;
const db eps=1e-9;
int n;
db a[N][N],b[N],ans[N];//系数数组
void Swap(int x,int y){
for (int i=1;i<=n;i++) swap(a[x][i],a[y][i]);
swap(b[x],b[y]);
}
void sub(int x,int y){
for (int i=1;i<=n;i++) a[x][i]-=a[y][i];
b[x]-=b[y];
}
void mul(int x,db d){
for (int i=1;i<=n;i++) a[x][i]*=d;
b[x]*=d;
}
void solve(int x){
if (x==n){
ans[x]=b[x]/a[x][x];
return;
}
if (abs(a[x][x])<=eps){
for (int i=x+1;i<=n;i++)
if (abs(a[i][x])>eps){
Swap(x,i);break;
}
}
for (int i=x+1;i<=n;i++)
if (abs(a[i][x])>eps){
db s=sqrt(abs(a[x][x]*a[i][x]));
mul(x,s/a[x][x]);
mul(i,s/a[i][x]);
sub(i,x);
}
solve(x+1);
for (int i=x+1;i<=n;i++) b[x]-=ans[i]*a[x][i];
ans[x]=b[x]/a[x][x];
}
int main()
{
freopen("bzoj_1013.in","r",stdin);
freopen("bzoj_1013.out","w",stdout);
scanf("%d",&n);
for (int i=0;i<=n;i++)
for (int j=1;j<=n;j++){
scanf("%lf",&a[i][j]);
b[i]+=a[i][j]*a[i][j];
a[i][j]=2*a[i][j];
}
for (int i=1;i<=n;i++) sub(i,0);
solve(1);
for (int i=1;i<=n;i++) printf("%.3lf ",ans[i]);
puts("");
return 0;
}