2014
01-26

Gauss Elimination

Li Zhixiang have already been in “Friendship” ocean-going freighter for three months. The excitement has gradually disappeared. He stands on the board, holding the railing and watching the dazzling ocean in the sun silently. Day after day, the same scenery is monotonous and tasteless, even the merry seagulls following the freighter cannot arouse his interest. Hearing the footsteps behind, he turns back to see the old captain is coming towards him. The captain has understood his idea, however, he starts a new topic with the young man.“Do you know how far our voyage is?” The captain asks. Li Zhixiang feels ashamed because he can not answer. Then the captain says with a smile, “5050 miles. Do you still remember the story of 5050?” This time the young man really blushes. The old captain continues saying:” You definitely know the story of 5050. When the German mathematician, “the prince of mathematicians”, Gauss was 10 years old …” Young man remembers this story and goes on to tell, “ When Gauss was 10 years old, he could add a list of integers from 1 to 100 in a few seconds, which shocked the teachers.” The old captain adds, “Gauss has many other stories like this. When he entered the university at the age of 17, he was able to construct heptadecagon by compass and straightedge. His university teachers were also impressed by his ability. Not only could college graduate students fail to do it, but also they felt hard to understand Gauss’s constructing process.”

At this time, vice-captain greets the old captain. The old captain says to Li Zhixiang: “Come over to my office tonight, let’s continue the conversation.” It is still calm and tranquil in the evening. The freighter travels smoothly on the sea in the silver moonlight. The captain tells the young man the following words.

Among the mathematicians through the ages, there are three greatest mathematicians: Archimedes, Newton and Gauss. Most of Gauss’s mathematical achievements are difficult to understand. Nevertheless, there are some comparatively easy. For instance, when it comes to solving multivariate system of linear equations, there is a solution called “Gauss Elimination”. In the navigation business, many problems can be solved by “Gauss elimination”. If you are interested in it, I will show you a simple question. Try it.”

There are several test cases. In the first line of each case, a number n indicates that there are n equations. The following n lines, each line has n+1 numbers, ai1,ai2,ai3…..ain, bi(1<= i <=n), these numbers indicate the coefficients of systems of the equations. ai1*x1+ai2*x2+……ain*xn=bi. Input is terminated by the end of file.

There are several test cases. In the first line of each case, a number n indicates that there are n equations. The following n lines, each line has n+1 numbers, ai1,ai2,ai3…..ain, bi(1<= i <=n), these numbers indicate the coefficients of systems of the equations. ai1*x1+ai2*x2+……ain*xn=bi. Input is terminated by the end of file.

2
1000000000000000000000000 1000000000000000000000000 1000000000000000000000000
-1000000000000000000000000 1000000000000000000000000 0
1
0 4

1/2
1/2

No solution.

http://acm.hdu.edu.cn/showproblem.php?pid=2449

ai1,ai2,ai3…..ain, bi
ai1*x1+ai2*x2+……ain*xn=bi

No solution.

copy 别人的 高精度 Gauss （留着用）（不会java 啊

import java.util.*;
import java.math.*;

class fraction{
BigInteger a, b;
public fraction(){
a = new BigInteger("0");
b = new BigInteger("1");
}

fraction( BigInteger a0, BigInteger b0){
this.a = a0; this.b = b0;
}
void reduction(){
BigInteger tmp = a.gcd( b );
a = a.divide( tmp );
b = b.divide( tmp );
if( b.compareTo( BigInteger.ZERO ) == - 1 )
{
b = b.multiply( BigInteger.valueOf( -1 ));
a = a.multiply( BigInteger.valueOf( -1 ));
}
}
fraction tmp = new fraction( a.multiply( t.b ).add( b.multiply( t.a )) , b.multiply(t.b) );
tmp.reduction();
return tmp;
}
fraction sub( fraction t ){
fraction tmp = new fraction( a.multiply( t.b ).subtract( b.multiply( t.a )) , b.multiply(t.b) );
tmp.reduction();
return tmp;
}
fraction mult( fraction t){
fraction tmp = new fraction( a.multiply( t.a ), b.multiply( t.b ));
tmp.reduction();
return tmp;
}
fraction div( fraction t){
fraction tmp = new fraction( a.multiply( t.b ), b.multiply( t.a ));
tmp.reduction();
return tmp;
}
public void abs(){
if( this.a.compareTo( BigInteger.ZERO ) == - 1 ){
this.a = this.a.multiply( BigInteger.valueOf( -1 ));
}
}
void out(){
this.reduction();
if( b.compareTo( BigInteger.ONE ) == 0 )
System.out.println(a);
else
System.out.println(a+"/"+b);
}

boolean biger( fraction p ){
fraction tmp = new fraction ( a, b );
fraction t = new fraction(p.a,p.b);
//t = p;
tmp.reduction();
if( tmp.a.compareTo( BigInteger.ZERO ) == - 1 ){
tmp.a = tmp.a.multiply( BigInteger.valueOf( -1 ));
}
if( t.a.compareTo( BigInteger.ZERO ) == - 1 ){
t.a = t.a.multiply( BigInteger.valueOf( -1 ));
}
tmp = tmp.sub( t );
return tmp.a.compareTo( BigInteger.ZERO ) > -1;
}

}

public class Main{

public static void lup_solve( fraction x[],fraction y[], fraction L[][], fraction U[][],int pi[],fraction b[],int n)
{
int i, j;
fraction z = new fraction( BigInteger.ZERO , BigInteger.ONE);
fraction sum = z;//double sum;
for ( i = 0 ; i < n ; i ++ ){
sum = z; //sum = 0;
for ( j = 0 ; j < i ; j ++ ){
sum = sum.add( L[i][j].mult( y[j] ));//sum += L[i][j] * y[ j ];
}
y[i] = b[ pi[i] ].sub( sum );//y[i] = b[ pi[i] ] - sum;
}
for ( i = n - 1 ; i >= 0 ; i -- ){
sum = z ; //sum = 0;
for ( j = i + 1 ; j < n ; j ++ ){
sum = sum.add( U[i][j].mult( x[j] ));//sum += U[i][j] * x[ j ];
}
x[i] = (y[i].sub( sum )).div( U[i][i] );//x[i] = (y[i] - sum)/U[i][i];
}
}

public static int lup_decomposition( fraction a[][] , int n , int pi[] )
{
int i, j, k, k1 = 0 ;
fraction p = new fraction(BigInteger.valueOf(0), BigInteger.ONE ), z = new fraction( BigInteger.valueOf(0), BigInteger.ONE );
for ( i = 0 ; i < n ; i ++ )
pi[i] = i;// 置换

for ( k = 0 ; k < n ; k ++ ){
p = z;
for ( i = k ; i < n ; i ++ )
{
if(  a[i][k].biger( p ) )
{
p = new fraction( a[i][k].a,a[i][k].b) ;
k1 = i;
}
}
if( p.a.compareTo( BigInteger.ZERO ) == 0 ){
return 0 ;// error
}
fraction tmp;

int t = pi[ k ]; pi[ k ] = pi[ k1 ]; pi[k1] = t;
for ( i = 0 ; i < n ; i ++ ){
tmp = a[ k ][i];  a[ k ][i] = a[ k1 ][i];  a[k1][i] = tmp;
}//swap( a[k][i], a[k1][i] );
for ( i = k + 1 ; i < n ; i ++ )
{
a[i][k] = a[i][k].div( a[k][k] );
for ( j = k + 1 ; j < n ; j ++ )
a[i][j] = a[i][j].sub( a[i][k].mult(a[k][j]));// - a[i][k] * a[k][j] ;
}
}
return 1;
}

public static void check(fraction a[][], fraction x[], int n){
int i, j;
fraction sum, z = new fraction( BigInteger.ZERO , BigInteger.ONE);
for ( i = 0 ;  i < n ; i++ ){
sum = z;
for ( j = 0 ;j < n ; j ++ )
{
sum = sum.add( a[i][j].mult( x[j] ));
}
sum.out();
}
}

public static void main(String[] agrs){
Scanner cin = new Scanner( System.in );
int i, j;
int n;
while( cin.hasNextInt() )
{
//任何函数都要和一个class相连
n = cin.nextInt();
int pi[] = new int[n];
fraction a[][] = new fraction[n][n];
fraction aa[][] = new fraction[n][n];
fraction B[] = new fraction[n];
fraction x[] = new fraction[n];
fraction y[] = new fraction[n];

for ( i = 0 ; i < n ; i ++ )
{
for ( j = 0 ;j < n ; j ++ ){
a[i][j] = new fraction( BigInteger.valueOf(0),BigInteger.valueOf(1));
a[i][j].a = cin.nextBigInteger();
aa[i][j] = new fraction( BigInteger.valueOf(0),BigInteger.valueOf(1));
aa[i][j] = a[i][j];
}
B[i] = new fraction( BigInteger.valueOf(0),BigInteger.valueOf(1));
B[i].a = cin.nextBigInteger();
x[i] = new fraction( BigInteger.valueOf(0),BigInteger.valueOf(1));
y[i] = new fraction( BigInteger.valueOf(0),BigInteger.valueOf(1));
}
if( 1 == lup_decomposition( a, n, pi) )
{
lup_solve( x, y, a, a, pi, B, n);
for ( i = 0 ;i < n; i ++)
x[i].out();
//check( aa, x, n);
}
else
{
System.out.println("No solution.");
}
System.out.println();
}
}
}

1. if(j){
int ans=a ;
for(int x=j-1;x>=0;x–){
if(!a ) break;
ans=min(ans,a );
sum+=ans;
}
}
求解释，，dp的思路是什么呢？

2. 你的理解应该是：即使主持人拿走一个箱子对结果没有影响。这样想，主持人拿走的箱子只是没有影响到你初始选择的那个箱子中有奖品的概率，但是改变了其余两个箱子的概率分布。由 1/3,1/3 变成了 0, 2/3

3. Often We don’t set up on weblogs, but I would like to condition that this established up really forced me individually to do this! considerably outstanding publish