In 2014, the 5th Shandong Province ACM College Student Programming Competition Circle (Gaussian Elimination||Find Rules)

Gaussian elimination: d[i]-1/2d[i-1]-1/2d[i+1]=1;

Rule: d[3]=n-1+n-3+n-5 d[2]=n-1+n-3 d[1]=n-1

#include <bits/stdc++.h>
using namespace std;

const int MAXN=1005;
double a[MAXN][MAXN];//Augmented matrix
double x[MAXN];//解集
bool free_x[MAXN];//Whether the mark is an indeterminate argument


// Gauss-Jordan elimination method to solve equations (Gauss-Jordan elimination).(-2 means that there are floating point solutions, but no integer solutions
//-1 means no solution, 0 means unique solution, greater than 0 means infinite solution, and returns the number of free variables)
//There are equal equations, var arguments. The augmented matrix has rows of equ from 0 to equ-1, and columns of var+1 from 0 to var.
int Gauss (int equ, int var, int MOD)
{
    int i,j,k;
    int max_r;// The row with the largest absolute value of the current column.
    int col;//The currently processed column
    double ta,tb;
    double LCM;
    double temp;

    for(int i=0;i<=var;i++)
    {
        x[i]=0;
        free_x[i]=true;
    }

    //Convert to echelon matrix.
    col=0; // currently processed column
    for(k = 0;k < equ && col < var;k++,col++)
    {// Enumerate currently processed lines.
// Find the row with the largest absolute value of the col column and exchange it with the kth row. (In order to reduce the error during division)
        max_r=k;
        for(i=k+1;i<equ;i++)
        {
            if(abs(a[i][col])>abs(a[max_r][col])) max_r=i;
        }
        if(max_r!=k)
        {// Swap with line k.
            for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]);
        }
        if(a[k][col]==0)
        {// Indicates that the col column below the kth row is all 0, then the next column of the current row is processed.
            k--;
            continue;
        }
        for(i=k+1;i<equ;i++)
        {// Enumerate the lines to delete.
            if(a[i][col]!=0)
            {
                LCM = abs(a[i][col])*abs(a[k][col]);
                ta = LCM/abs(a[i][col]);
                tb = LCM/abs(a[k][col]);
                if(a[i][col]*a[k][col]<0)tb=-tb;//The case of the opposite sign is the addition
                for(j=col;j<var+1;j++)
                {
                    a[i][j] = a[i][j]*ta-a[k][j]*tb;
                }
            }
        }
    }

    // 3. The unique solution case: a strictly upper triangular matrix is ​​formed in the augmented matrix of var * (var + 1).
    // Calculate Xn-1, Xn-2 ... X0.
    for (i = var - 1; i >= 0; i--)
    {
        temp = a[i][var];
        for (j = i + 1; j < var; j++)
        {
            if (a[i][j] != 0) temp -= a[i][j] * x[j];
        }
        x[i] =temp / a[i][i] ;
    }
    return 0;
}
intmain()
{
   int t,y,n;
   scanf("%d",&t);
   while(t--)
   {
       memset(a,0,sizeof(a));
      scanf("%d%d",&n,&y);
 a[0][0]=1;
   for(int i=1;i<n;i++)
   {
       a[i][i-1]=-0.5;
       a[i][(i+1)%n]=-0.5;
       a[i][n]=1;
       a[i][i]=1;
   }
   Gauss(n,n,100000);

   printf("%.4f\n",x[y]);
   }
    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324628970&siteId=291194637