http://acm.hdu.edu.cn/showproblem.php?pid=2449
题意 :
纯高斯消元 ;
输入 n 行 ,每行 n+1个数带代表 系数和 值
ai1,ai2,ai3…..ain, bi
ai1*x1+ai2*x2+......ain*xn=bi
求解 xi 若没有整数解 输出 分数 ,
若没有解 输出
No solution.
copy 别人的 高精度 Gauss (留着用)(不会java 啊 )
1 import java.util.*;
2 import java.math.*;
3
4 class fraction{
5 BigInteger a, b;
6 public fraction(){
7 a = new BigInteger("0");
8 b = new BigInteger("1");
9 }
10
11 fraction( BigInteger a0, BigInteger b0){
12 this.a = a0; this.b = b0;
13 }
14 void reduction(){
15 BigInteger tmp = a.gcd( b );
16 a = a.divide( tmp );
17 b = b.divide( tmp );
18 if( b.compareTo( BigInteger.ZERO ) == - 1 )
19 {
20 b = b.multiply( BigInteger.valueOf( -1 ));
21 a = a.multiply( BigInteger.valueOf( -1 ));
22 }
23 }
24 fraction add( fraction t ){
25 fraction tmp = new fraction( a.multiply( t.b ).add( b.multiply( t.a )) , b.multiply(t.b) );
26 tmp.reduction();
27 return tmp;
28 }
29 fraction sub( fraction t ){
30 fraction tmp = new fraction( a.multiply( t.b ).subtract( b.multiply( t.a )) , b.multiply(t.b) );
31 tmp.reduction();
32 return tmp;
33 }
34 fraction mult( fraction t){
35 fraction tmp = new fraction( a.multiply( t.a ), b.multiply( t.b ));
36 tmp.reduction();
37 return tmp;
38 }
39 fraction div( fraction t){
40 fraction tmp = new fraction( a.multiply( t.b ), b.multiply( t.a ));
41 tmp.reduction();
42 return tmp;
43 }
44 public void abs(){
45 if( this.a.compareTo( BigInteger.ZERO ) == - 1 ){
46 this.a = this.a.multiply( BigInteger.valueOf( -1 ));
47 }
48 }
49 void out(){
50 this.reduction();
51 if( b.compareTo( BigInteger.ONE ) == 0 )
52 System.out.println(a);
53 else
54 System.out.println(a+"/"+b);
55 }
56
57 boolean biger( fraction p ){
58 fraction tmp = new fraction ( a, b );
59 fraction t = new fraction(p.a,p.b);
60 //t = p;
61 tmp.reduction();
62 if( tmp.a.compareTo( BigInteger.ZERO ) == - 1 ){
63 tmp.a = tmp.a.multiply( BigInteger.valueOf( -1 ));
64 }
65 if( t.a.compareTo( BigInteger.ZERO ) == - 1 ){
66 t.a = t.a.multiply( BigInteger.valueOf( -1 ));
67 }
68 tmp = tmp.sub( t );
69 return tmp.a.compareTo( BigInteger.ZERO ) > -1;
70 }
71
72 }
73
74 public class Main{
75
76 public static void lup_solve( fraction x[],fraction y[], fraction L[][], fraction U[][],int pi[],fraction b[],int n)
77 {
78 int i, j;
79 fraction z = new fraction( BigInteger.ZERO , BigInteger.ONE);
80 fraction sum = z;//double sum;
81 for ( i = 0 ; i < n ; i ++ ){
82 sum = z; //sum = 0;
83 for ( j = 0 ; j < i ; j ++ ){
84 sum = sum.add( L[i][j].mult( y[j] ));//sum += L[i][j] * y[ j ];
85 }
86 y[i] = b[ pi[i] ].sub( sum );//y[i] = b[ pi[i] ] - sum;
87 }
88 for ( i = n - 1 ; i >= 0 ; i -- ){
89 sum = z ; //sum = 0;
90 for ( j = i + 1 ; j < n ; j ++ ){
91 sum = sum.add( U[i][j].mult( x[j] ));//sum += U[i][j] * x[ j ];
92 }
93 x[i] = (y[i].sub( sum )).div( U[i][i] );//x[i] = (y[i] - sum)/U[i][i];
94 }
95 }
96
97 public static int lup_decomposition( fraction a[][] , int n , int pi[] )
98 {
99 int i, j, k, k1 = 0 ;
100 fraction p = new fraction(BigInteger.valueOf(0), BigInteger.ONE ), z = new fraction( BigInteger.valueOf(0), BigInteger.ONE );
101 for ( i = 0 ; i < n ; i ++ )
102 pi[i] = i;// 置换
103
104 for ( k = 0 ; k < n ; k ++ ){
105 p = z;
106 for ( i = k ; i < n ; i ++ )
107 {
108 if( a[i][k].biger( p ) )
109 {
110 p = new fraction( a[i][k].a,a[i][k].b) ;
111 k1 = i;
112 }
113 }
114 if( p.a.compareTo( BigInteger.ZERO ) == 0 ){
115 return 0 ;// error
116 }
117 fraction tmp;
118
119 int t = pi[ k ]; pi[ k ] = pi[ k1 ]; pi[k1] = t;
120 for ( i = 0 ; i < n ; i ++ ){
121 tmp = a[ k ][i]; a[ k ][i] = a[ k1 ][i]; a[k1][i] = tmp;
122 }//swap( a[k][i], a[k1][i] );
123 for ( i = k + 1 ; i < n ; i ++ )
124 {
125 a[i][k] = a[i][k].div( a[k][k] );
126 for ( j = k + 1 ; j < n ; j ++ )
127 a[i][j] = a[i][j].sub( a[i][k].mult(a[k][j]));// - a[i][k] * a[k][j] ;
128 }
129 }
130 return 1;
131 }
132
133 public static void check(fraction a[][], fraction x[], int n){
134 int i, j;
135 fraction sum, z = new fraction( BigInteger.ZERO , BigInteger.ONE);
136 for ( i = 0 ; i < n ; i++ ){
137 sum = z;
138 for ( j = 0 ;j < n ; j ++ )
139 {
140 sum = sum.add( a[i][j].mult( x[j] ));
141 }
142 sum.out();
143 }
144 }
145
146 public static void main(String[] agrs){
147 Scanner cin = new Scanner( System.in );
148 int i, j;
149 int n;
150 while( cin.hasNextInt() )
151 {
152 //任何函数都要和一个class相连
153 n = cin.nextInt();
154 int pi[] = new int[n];
155 fraction a[][] = new fraction[n][n];
156 fraction aa[][] = new fraction[n][n];
157 fraction B[] = new fraction[n];
158 fraction x[] = new fraction[n];
159 fraction y[] = new fraction[n];
160
161 for ( i = 0 ; i < n ; i ++ )
162 {
163 for ( j = 0 ;j < n ; j ++ ){
164 a[i][j] = new fraction( BigInteger.valueOf(0),BigInteger.valueOf(1));
165 a[i][j].a = cin.nextBigInteger();
166 aa[i][j] = new fraction( BigInteger.valueOf(0),BigInteger.valueOf(1));
167 aa[i][j] = a[i][j];
168 }
169 B[i] = new fraction( BigInteger.valueOf(0),BigInteger.valueOf(1));
170 B[i].a = cin.nextBigInteger();
171 x[i] = new fraction( BigInteger.valueOf(0),BigInteger.valueOf(1));
172 y[i] = new fraction( BigInteger.valueOf(0),BigInteger.valueOf(1));
173 }
174 if( 1 == lup_decomposition( a, n, pi) )
175 {
176 lup_solve( x, y, a, a, pi, B, n);
177 for ( i = 0 ;i < n; i ++)
178 x[i].out();
179 //check( aa, x, n);
180 }
181 else
182 {
183 System.out.println("No solution.");
184 }
185 System.out.println();
186 }
187 }
188 }
2 import java.math.*;
3
4 class fraction{
5 BigInteger a, b;
6 public fraction(){
7 a = new BigInteger("0");
8 b = new BigInteger("1");
9 }
10
11 fraction( BigInteger a0, BigInteger b0){
12 this.a = a0; this.b = b0;
13 }
14 void reduction(){
15 BigInteger tmp = a.gcd( b );
16 a = a.divide( tmp );
17 b = b.divide( tmp );
18 if( b.compareTo( BigInteger.ZERO ) == - 1 )
19 {
20 b = b.multiply( BigInteger.valueOf( -1 ));
21 a = a.multiply( BigInteger.valueOf( -1 ));
22 }
23 }
24 fraction add( fraction t ){
25 fraction tmp = new fraction( a.multiply( t.b ).add( b.multiply( t.a )) , b.multiply(t.b) );
26 tmp.reduction();
27 return tmp;
28 }
29 fraction sub( fraction t ){
30 fraction tmp = new fraction( a.multiply( t.b ).subtract( b.multiply( t.a )) , b.multiply(t.b) );
31 tmp.reduction();
32 return tmp;
33 }
34 fraction mult( fraction t){
35 fraction tmp = new fraction( a.multiply( t.a ), b.multiply( t.b ));
36 tmp.reduction();
37 return tmp;
38 }
39 fraction div( fraction t){
40 fraction tmp = new fraction( a.multiply( t.b ), b.multiply( t.a ));
41 tmp.reduction();
42 return tmp;
43 }
44 public void abs(){
45 if( this.a.compareTo( BigInteger.ZERO ) == - 1 ){
46 this.a = this.a.multiply( BigInteger.valueOf( -1 ));
47 }
48 }
49 void out(){
50 this.reduction();
51 if( b.compareTo( BigInteger.ONE ) == 0 )
52 System.out.println(a);
53 else
54 System.out.println(a+"/"+b);
55 }
56
57 boolean biger( fraction p ){
58 fraction tmp = new fraction ( a, b );
59 fraction t = new fraction(p.a,p.b);
60 //t = p;
61 tmp.reduction();
62 if( tmp.a.compareTo( BigInteger.ZERO ) == - 1 ){
63 tmp.a = tmp.a.multiply( BigInteger.valueOf( -1 ));
64 }
65 if( t.a.compareTo( BigInteger.ZERO ) == - 1 ){
66 t.a = t.a.multiply( BigInteger.valueOf( -1 ));
67 }
68 tmp = tmp.sub( t );
69 return tmp.a.compareTo( BigInteger.ZERO ) > -1;
70 }
71
72 }
73
74 public class Main{
75
76 public static void lup_solve( fraction x[],fraction y[], fraction L[][], fraction U[][],int pi[],fraction b[],int n)
77 {
78 int i, j;
79 fraction z = new fraction( BigInteger.ZERO , BigInteger.ONE);
80 fraction sum = z;//double sum;
81 for ( i = 0 ; i < n ; i ++ ){
82 sum = z; //sum = 0;
83 for ( j = 0 ; j < i ; j ++ ){
84 sum = sum.add( L[i][j].mult( y[j] ));//sum += L[i][j] * y[ j ];
85 }
86 y[i] = b[ pi[i] ].sub( sum );//y[i] = b[ pi[i] ] - sum;
87 }
88 for ( i = n - 1 ; i >= 0 ; i -- ){
89 sum = z ; //sum = 0;
90 for ( j = i + 1 ; j < n ; j ++ ){
91 sum = sum.add( U[i][j].mult( x[j] ));//sum += U[i][j] * x[ j ];
92 }
93 x[i] = (y[i].sub( sum )).div( U[i][i] );//x[i] = (y[i] - sum)/U[i][i];
94 }
95 }
96
97 public static int lup_decomposition( fraction a[][] , int n , int pi[] )
98 {
99 int i, j, k, k1 = 0 ;
100 fraction p = new fraction(BigInteger.valueOf(0), BigInteger.ONE ), z = new fraction( BigInteger.valueOf(0), BigInteger.ONE );
101 for ( i = 0 ; i < n ; i ++ )
102 pi[i] = i;// 置换
103
104 for ( k = 0 ; k < n ; k ++ ){
105 p = z;
106 for ( i = k ; i < n ; i ++ )
107 {
108 if( a[i][k].biger( p ) )
109 {
110 p = new fraction( a[i][k].a,a[i][k].b) ;
111 k1 = i;
112 }
113 }
114 if( p.a.compareTo( BigInteger.ZERO ) == 0 ){
115 return 0 ;// error
116 }
117 fraction tmp;
118
119 int t = pi[ k ]; pi[ k ] = pi[ k1 ]; pi[k1] = t;
120 for ( i = 0 ; i < n ; i ++ ){
121 tmp = a[ k ][i]; a[ k ][i] = a[ k1 ][i]; a[k1][i] = tmp;
122 }//swap( a[k][i], a[k1][i] );
123 for ( i = k + 1 ; i < n ; i ++ )
124 {
125 a[i][k] = a[i][k].div( a[k][k] );
126 for ( j = k + 1 ; j < n ; j ++ )
127 a[i][j] = a[i][j].sub( a[i][k].mult(a[k][j]));// - a[i][k] * a[k][j] ;
128 }
129 }
130 return 1;
131 }
132
133 public static void check(fraction a[][], fraction x[], int n){
134 int i, j;
135 fraction sum, z = new fraction( BigInteger.ZERO , BigInteger.ONE);
136 for ( i = 0 ; i < n ; i++ ){
137 sum = z;
138 for ( j = 0 ;j < n ; j ++ )
139 {
140 sum = sum.add( a[i][j].mult( x[j] ));
141 }
142 sum.out();
143 }
144 }
145
146 public static void main(String[] agrs){
147 Scanner cin = new Scanner( System.in );
148 int i, j;
149 int n;
150 while( cin.hasNextInt() )
151 {
152 //任何函数都要和一个class相连
153 n = cin.nextInt();
154 int pi[] = new int[n];
155 fraction a[][] = new fraction[n][n];
156 fraction aa[][] = new fraction[n][n];
157 fraction B[] = new fraction[n];
158 fraction x[] = new fraction[n];
159 fraction y[] = new fraction[n];
160
161 for ( i = 0 ; i < n ; i ++ )
162 {
163 for ( j = 0 ;j < n ; j ++ ){
164 a[i][j] = new fraction( BigInteger.valueOf(0),BigInteger.valueOf(1));
165 a[i][j].a = cin.nextBigInteger();
166 aa[i][j] = new fraction( BigInteger.valueOf(0),BigInteger.valueOf(1));
167 aa[i][j] = a[i][j];
168 }
169 B[i] = new fraction( BigInteger.valueOf(0),BigInteger.valueOf(1));
170 B[i].a = cin.nextBigInteger();
171 x[i] = new fraction( BigInteger.valueOf(0),BigInteger.valueOf(1));
172 y[i] = new fraction( BigInteger.valueOf(0),BigInteger.valueOf(1));
173 }
174 if( 1 == lup_decomposition( a, n, pi) )
175 {
176 lup_solve( x, y, a, a, pi, B, n);
177 for ( i = 0 ;i < n; i ++)
178 x[i].out();
179 //check( aa, x, n);
180 }
181 else
182 {
183 System.out.println("No solution.");
184 }
185 System.out.println();
186 }
187 }
188 }