-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path10109 - Solving Systems of Linear Equations.cpp
215 lines (198 loc) · 4.4 KB
/
10109 - Solving Systems of Linear Equations.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
UVa 10109 - Solving Systems of Linear Equations
by Rico Tiongson
Submitted: January 23, 2014
Accepted 0.422s C++
O(n^3) time
*/
#include <iostream>
#include <cstdio>
#include <sstream>
#include <vector>
#include <cmath>
using namespace std;
template<class T>
T gcd( T a, T b ){
if( a == 0 ) return b;
if( b == 0 ) return a;
if( a < 0 ) a = -a;
if( b < 0 ) b = -b;
T temp;
while( b > 0 ){
temp = b;
b = a % b;
a = temp;
}
return a;
}
template<class T>
T lcm( T a, T b ){
return a / gcd(a,b) * b;
}
template<class T>
struct fraction {
T num, den;
// static
// constructors
fraction(): num(0), den(1) {}
fraction( T n, T d = 1 ){
if( n == 0 ){
num = 0;
den = 1;
return;
}
if( d < 0 ){
n = -n;
d = -d;
}
T g = gcd(n, d);
num = n/g;
den = d/g;
}
// unary
bool operator!() const{ return num == 0; }
fraction& operator-() const{ num = -num; return *this; }
fraction abs() const{
return num < 0 ? fraction(-num, den) : *this;
}
// arithmetic
fraction operator+( fraction f ) const{
T low = lcm(den, f.den);
return fraction( num * (low / den) + f.num * (low / f.den), low );
}
fraction operator-( fraction f ) const{
T low = lcm(den, f.den);
return fraction( num * (low / den) - f.num * (low / f.den), low );
}
fraction operator*( fraction f ) const{
T g1 = gcd(num, f.den);
T g2 = gcd(f.num, den);
return fraction( (num/g1) * (f.num/g2), (den/g2) * (f.den/g1) );
}
fraction operator/( fraction f ) const{
return *this * fraction(f.den, f.num);
}
fraction& operator+=( fraction f ){ return *this = (*this + f); }
fraction& operator-=( fraction f ){ return *this = (*this - f); }
fraction& operator*=( fraction f ){ return *this = (*this * f); }
fraction& operator/=( fraction f ){ return *this = (*this / f); }
bool operator < ( fraction f ) const {
return num * f.den < f.num * den;
}
bool operator > ( fraction f ) const {
return f < *this;
}
bool operator == ( fraction f ) const {
return num == f.num && den == f.den;
}
bool operator != ( fraction f ) const {
return !( *this == f );
}
friend std::ostream& operator << ( std::ostream& out, fraction<T> f ){
out << f.num;
if( f.den != 1 ) out << "/" << f.den;
return out;
}
};
typedef fraction<int> Fraction;
using namespace std;
typedef long long lag;
typedef fraction<lag> FR;
char buf[1005];
istream& operator >> ( istream& in, FR& f ){
in >> buf;
// int fn = s.find( "/" );
lag x, y;
if( sscanf( buf, "%lld/%lld", &x, &y ) == 2 ){
f = FR(x, y);
}
else{
sscanf( buf, "%lld", &x );
f = FR(x);
}
return in;
}
int tc, m, n;
vector<vector<FR> > mat;
vector<FR> b, ans;
int gauss(){
int id = 0, rank = 0;
for( int p=0; p<m; ++p ){
if( id >= n ) break;
int nonzero = -1;
for( int i=p; i<m; ++i ){
if( mat[i][id].num != 0 ){
nonzero = i;
break;
}
}
if( nonzero == -1 ){
id++;
p--;
continue;
}
if( nonzero != p ){
mat[nonzero].swap( mat[p] );
swap( b[p], b[nonzero] );
}
rank++;
// singularize
for( int i=id+1; i<n; ++i ){
mat[p][i] /= mat[p][id];
}
b[p] /= mat[p][id];
mat[p][id] = FR(1,1);
for( int i=0; i<m; ++i ){
if( i == p ) continue;
FR alpha = mat[i][id] / mat[p][id];
b[i] -= alpha * b[p];
for( int j=id; j<n; ++j ){
mat[i][j] -= alpha * mat[p][j];
}
}
id++;
}
for( int i=0; i<m; ++i ){
if( b[i].num == 0 ) continue;
bool zero = true;
for( int j=0; j<n; ++j ){
if( mat[i][j].num != 0 ){
zero = false;
break;
}
}
if( zero )
return -1;
}
// return 1;
// solution!
if( rank != n ) return n - rank;
ans.resize(n);
for( int i=n-1; i>=0; --i ){
ans[i] = b[i];
}
for( int i=0; i<n; ++i ){
cout << "x[" << i+1 << "] = " << ans[i] << endl;
}
return 0;
}
int main(){
bool first = true;
while( cin >> tc, tc ){
if( first ) first = false;
else cout << endl;
cout << "Solution for Matrix System # " << tc << endl;
cin >> n >> m;
mat.assign(m, vector<FR>(n));
b.resize(m);
for( int i=0; i<m; ++i ){
for( int j=0; j<n; ++j ){
cin >> mat[i][j];
}
cin >> b[i];
}
int x = gauss();
if( x == -1 ) cout << "No Solution." << endl;
else if( x != 0 ) cout << "Infinitely many solutions containing " << x << " arbitrary constants." << endl;
}
}