Luogu P4035 [JSOI2008] spherical space generator

Title Description

There can be a spherical space is produced in \ (n-\) produced a rigid sphere-dimensional space. Now, you are trapped in this \ (n \) - dimensional sphere, you only know the sphere \ (n + 1 \) coordinates of points, you need to determine the fastest \ (n \) - dimensional sphere the sphere center coordinates, so as to destroy the generator spherical space.

Input Format

The first row is an integer \ (n-\ left (. 1 \ Le N \ Le 10 \ right) \) . The next \ (n + 1 \) rows, each row having (n-\) \ a real number, the sphere point \ (n-\) dimensional coordinates. Each real number after the decimal point \ (6 \) bits, and an absolute value does not exceed \ (20,000 \) .

Output Format

There is only one line, the center is given sequentially \ (n-\) dimensional coordinate ( \ (n-\) real numbers), separated by a space between the two real numbers. Each real numbers after the decimal point \ (3 \) bits. Data guarantee solvable. Your answer is exactly the same as the standard output and to be able to score.

Topic Link

Thinking

According to the meaning of problems too

\[ (x_{1}-a_{1})^2+(x_{2}-a_{2})^2+.......+(x_{n}-a_{n})^2=r^2 \]

Be turned into

$ 2a_{1}x_{1}+2a_{2}x_{2}+......+2a_{n}x_{n}-r^2-x_{1}^{2}-x_{2}^{2}-......-x_{n}^{2}=a_{1}^{2}+.......a_{n}^{2} $

Provided $ t = -r ^ 2-x_ {1} ^ {2} -x_ {2} ^ {2} -......- x_ {n} ^ {2} $

So get

$ 2a_{1}x_{1}+2a_{2}x_{2}+......+2a_{n}x_{n}+t=a_{1}^{2}+.......a_{n}^{2} $

It can be obtained a \ (n + 1 \) element equations

Solved using Gaussian elimination, the total number is not forget \ (n + 1 \) instead of \ (n-\) (adjusted for half an hour seeds)

( \ (\ Mathrm LaTeX {} \) so hard to fight ah)

code

#include<bits/stdc++.h>
using namespace std;
const int MAXN=20;
double a[MAXN][MAXN],num;
int n;

void debug()
{
    puts("---------------------------------");
    for(int i=1;i<=n+1;++i){
        for(int j=1;j<=n+2;++j){
            cout<<fixed<<setprecision(2)<<a[i][j]<<"    ";
        }
        putchar('\n');
    }
}

void gauss()
{
    for(int i=1;i<=n+1;i++){
//      debug();
        int now=i;
        while(a[now][i]==0&&now<=n+1){
            now++;
        }
        if(now==n+2){
            puts("???");
            exit(233);
        }
        for(int j=1;j<=n+2;j++){
            swap(a[i][j],a[now][j]);
        }
        double k=a[i][i];
        for(int j=1;j<=n+2;j++){
            a[i][j]/=k;
        }
        for(int j=1;j<=n+1;j++){
            if(j!=i){
                double kkk=a[j][i];
                for(int k=1;k<=n+2;k++){
                    a[j][k]-=kkk*a[i][k];
                }
            }
        }
        
    }
}

int main()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    cin>>n;
    for(int i=1;i<=n+1;++i){
        for(int j=1;j<=n;j++){
            cin>>num;
            a[i][j]=2*num,a[i][n+2]+=num*num;
        }
        a[i][n+1]=1;
    }
    gauss();
    for(int i=1;i<=n;++i){
        cout<<fixed<<setprecision(3)<<a[i][n+2]<<' ';
    }
    return 0;
}

Reference blog

Guess you like

Origin www.cnblogs.com/zhu-chen/p/11521433.html