Luogu4035 / BZOJ1013 [JSOI2008] spherical space generator

Topic Portal

Analysis of Algorithms

May assume unknown \ (D \) represents each point the distance to the center of the sphere, provided unknown \ (b_1, b_2, \ cdots , b_n \) represents the coordinates of the center of the sphere.

We can easily get \ (n + 1 \) equations:

\[d^2=\sum_{i=1}^n{(a_i-b_i)^2}\]

Found to occur quadratic term, and here apparently Conversion method could not be used to replace a quadratic term.

We launched this formula:

\[d^2=\sum_{i=1}^n{a_i^2}+\sum_{i=1}^n{b_i^2}+2\times \sum_{i=1}^n{a_ib_i}\]

Unknown wrote on the same side:

\[-2\times \sum_{i=1}^n{a_ib_i}-\sum_{i=1}^n{b_i^2}+d^2=\sum_{i=1}^n{a_i^2}\]

Then we find that, for each equation, quadratic coefficient are all the same, so we consider the cancellation terms ortho, obtained following \ (n-\) equations:

\[-2\times \sum_{i=1}^n{(a_i-a_{i-1})b_i}=\sum_{i=1}^n{a_i^2-a_{i-1}^2}\]

This is in line with the conditions of Gaussian elimination, you can do directly.

Code

#include<bits/stdc++.h>
using namespace std;
#define maxn 105
int n;
double a[maxn][maxn],b[maxn][maxn],x[maxn][maxn];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n+1;i++){
        for(int j=1;j<=n;j++){
            scanf("%lf",&x[i][j]);
        }
    }
    for(int i=1;i<=n+1;i++){
        for(int j=1;j<=n;j++){
            b[i][j]=(-2)*x[i][j];
        }
        for(int j=1;j<=n;j++)b[i][n+1]-=(x[i][j]*x[i][j]);
    }
    for(int i=1;i<=n+1;i++){
        for(int j=2;j<=n+1;j++){
            a[i][j]=b[i][j]-b[i-1][j];
        }
    }
    for(int i=1;i<=n;i++){
        int p=0;
        for(int j=i;j<=n;j++){if(a[j][i]){p=j;break;}}
        if(!p)continue;
        for(int j=1;j<=n+1;j++)swap(a[i][j],a[p][j]);
        double k=a[i][i];
        for(int j=1;j<=n+1;j++)a[i][j]/=k;
        for(int j=1;j<=n;j++){
            if(i==j)continue;
            double kk=a[j][i];
            for(int l=1;l<=n+1;l++)a[j][l]-=kk*a[i][l];
        }
    }
    for(int i=1;i<=n;i++)printf("%.3lf ",a[i][n+1]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/ZigZagKmp/p/11491053.html