-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmatrix_double.cpp
309 lines (282 loc) · 9.41 KB
/
matrix_double.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//
// 実数行列 (行列累乗と、掃き出し法)
//
// verified:
// AOJ 2171 Strange Couple
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2171
//
// AOJ 1328 Find the Outlier
// https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1328
//
#include <bits/stdc++.h>
using namespace std;
// basic settings
long double EPS = 1e-10; // to be set appropriately
// matrix
template<class T> struct Matrix {
// inner value
vector<vector<T>> val;
// constructors
Matrix(int H, int W, T x = 0) : val(H, vector<T>(W, x)) {}
Matrix(const Matrix &mat) : val(mat.val) {}
void init(int H, int W, T x = 0) {
val.assign(H, vector<T>(W, x));
}
void resize(int H, int W) {
val.resize(H);
for (int i = 0; i < H; ++i) val[i].resize(W);
}
// getter and debugger
constexpr int height() const { return (int)val.size(); }
constexpr int width() const { return (int)val[0].size(); }
vector<T>& operator [] (int i) { return val[i]; }
constexpr vector<T>& operator [] (int i) const { return val[i]; }
friend constexpr ostream& operator << (ostream &os, const Matrix<T> &mat) {
os << endl;
for (int i = 0; i < mat.height(); ++i) {
for (int j = 0; j < mat.width(); ++j) {
if (j) os << ", ";
os << mat.val[i][j];
}
os << endl;
}
return os;
}
// comparison operators
constexpr bool operator == (const Matrix &r) const {
return this->val == r.val;
}
constexpr bool operator != (const Matrix &r) const {
return this->val != r.val;
}
// arithmetic operators
constexpr Matrix& operator += (const Matrix &r) {
assert(height() == r.height());
assert(width() == r.width());
for (int i = 0; i < height(); ++i) {
for (int j = 0; j < width(); ++j) {
val[i][j] += r.val[i][j];
}
}
return *this;
}
constexpr Matrix& operator -= (const Matrix &r) {
assert(height() == r.height());
assert(width() == r.width());
for (int i = 0; i < height(); ++i) {
for (int j = 0; j < width(); ++j) {
val[i][j] -= r.val[i][j];
}
}
return *this;
}
constexpr Matrix& operator *= (T v) {
for (int i = 0; i < height(); ++i)
for (int j = 0; j < width(); ++j)
val[i][j] *= v;
return *this;
}
constexpr Matrix& operator *= (const Matrix &r) {
assert(width() == r.height());
Matrix<T> res(height(), r.width());
for (int i = 0; i < height(); ++i)
for (int j = 0; j < r.width(); ++j)
for (int k = 0; k < width(); ++k)
res[i][j] += val[i][k] * r.val[k][j];
return (*this) = res;
}
constexpr Matrix operator + () const { return Matrix(*this); }
constexpr Matrix operator - () const { return Matrix(*this) *= T(-1); }
constexpr Matrix operator + (const Matrix &r) const { return Matrix(*this) += r; }
constexpr Matrix operator - (const Matrix &r) const { return Matrix(*this) -= r; }
constexpr Matrix operator * (T v) const { return Matrix(*this) *= v; }
constexpr Matrix operator * (const Matrix &r) const { return Matrix(*this) *= r; }
// pow
constexpr Matrix pow(long long n) const {
assert(height() == width());
Matrix<T> res(height(), width()), mul(*this);
while (n > 0) {
if (n & 1) res *= mul;
mul *= mul;
n >>= 1;
}
return res;
}
friend constexpr Matrix<T> pow(const Matrix<T> &mat, long long n) {
return mat.pow(n);
}
// gauss-jordan
constexpr int find_pivot(int cur_rank, int col) const {
int pivot = -1;
T max_v = EPS;
for (int row = cur_rank; row < height(); ++row) {
if (abs(val[row][col]) > max_v) {
max_v = abs(val[row][col]);
pivot = row;
}
}
return pivot;
}
constexpr void sweep(int cur_rank, int col, int pivot) {
swap(val[pivot], val[cur_rank]);
auto fac = val[cur_rank][col];
for (int col2 = 0; col2 < width(); ++col2) {
val[cur_rank][col2] /= fac;
}
for (int row = 0; row < height(); ++row) {
if (row != cur_rank && abs(val[row][col]) > EPS) {
auto fac = val[row][col];
for (int col2 = 0; col2 < width(); ++col2) {
val[row][col2] -= val[cur_rank][col2] * fac;
}
}
}
}
constexpr int gauss_jordan(int not_sweep_width = 0) {
int rank = 0;
for (int col = 0; col < width(); ++col) {
if (col == width() - not_sweep_width) break;
int pivot = find_pivot(rank, col);
if (pivot == -1) continue;
sweep(rank++, col, pivot);
}
return rank;
}
friend constexpr int gauss_jordan(Matrix<T> &mat, int not_sweep_width = 0) {
return mat.gauss_jordan(not_sweep_width);
}
friend constexpr vector<T> linear_equation(const Matrix<T> &mat, const vector<T> &b) {
// extend
Matrix<T> A(mat.height(), mat.width() + 1);
for (int i = 0; i < mat.height(); ++i) {
for (int j = 0; j < mat.width(); ++j) A[i][j] = mat.val[i][j];
A[i].back() = b[i];
}
int rank = A.gauss_jordan(1);
// check if it has no solution
vector<T> res;
for (int row = rank; row < mat.height(); ++row)
if (abs(A[row].back()) > EPS)
return res;
// answer
res.assign(mat.width(), 0);
for (int i = 0; i < rank; ++i) res[i] = A[i].back();
return res;
}
};
//------------------------------//
// Examples
//------------------------------//
void AOJ_2171() {
int N, s, t;
while (cin >> N >> s >> t, N) {
--s, --t;
vector<int> q(N);
vector<vector<int>> a(N, vector<int>(N));
for (int i = 0; i < N; ++i) cin >> q[i];
for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) cin >> a[i][j];
// Dijkstra
const int INF = 1<<29;
vector<int> dist(N, INF);
vector<bool> seen(N, 0);
dist[t] = 0;
for (int iter = 0; iter < N; ++iter) {
int curd = INF;
int v = -1;
for (int i = 0; i < N; ++i) {
if (seen[i]) continue;
if (curd > dist[i]) {
curd = dist[i];
v = i;
}
}
if (v == -1) break;
for (int w = 0; w < N; ++w) {
if (w == v) continue;
if (a[v][w] == 0) continue;
dist[w] = min(dist[w], curd + a[v][w]);
}
seen[v] = true;
}
if (dist[s] >= INF) {
cout << "impossible" << endl;
return;
}
// 連立一次方程式を作る
Matrix<double> A(N, N, 0);
vector<double> b(N, 0);
for (int v = 0; v < N; ++v) {
if (v == t) {
A[v][v] = 1;
b[v] = 0;
}
else {
vector<int> neigbor;
for (int w = 0; w < N; ++w) {
if (a[v][w] == 0) continue;
if (q[v] == 1 && dist[w] + a[v][w] != dist[v]) continue;
neigbor.push_back(w);
}
int K = neigbor.size();
for (auto w : neigbor) {
A[v][w] -= 1;
b[v] += a[v][w];
}
A[v][v] += K;
}
}
// 解く
auto res = linear_equation(A, b);
if (res.empty()) cout << "impossible" << endl;
else cout << fixed << setprecision(15) << res[s] << endl;
}
}
void AOJ_1328() {
using D = long double;
EPS = 1e-5; // set EPS
auto dpow = [&](D a, int n) -> D {
D res = 1.0;
for (int i = 0; i < n; ++i) res *= a;
return res;
};
auto func = [&](const vector<D> &coef, int i) -> D {
D res = 0.0;
for (int p = 0; p < (int)coef.size(); ++p)
res += coef[p] * pow(i, p);
return res;
};
int d;
while (cin >> d, d) {
vector<D> v(d + 3);
for (int i = 0; i < d + 3; ++i) cin >> v[i];
bool finish = false;
int res = 0;
for (int i = 0; i < d + 3 && !finish; ++i) {
for (int j = i + 1; j < d + 3 && !finish; ++j) {
Matrix<D> A(d + 1, d + 1);
vector<D> b(d + 1);
for (int k = 0, iter = 0; k < d+3; ++k) {
if (k == i || k == j) continue;
for (int p = 0; p < d + 1; ++p) {
A[iter][p] = pow(k, p);
b[iter] = v[k];
}
++iter;
}
vector<D> ans = linear_equation(A, b);
if (ans.empty()) continue;
D vi = func(ans, i), vj = func(ans, j);
int num = 0;
if (fabs(vi - v[i]) > EPS) res = i, ++num;
if (fabs(vj - v[j]) > EPS) res = j, ++num;
if (num == 1) goto end;
}
}
end:
cout << res << endl;
}
}
int main() {
//AOJ_2171();
AOJ_1328();
}