Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- #define all(x) x.begin(), x.end()
- #define rall(x) x.rbegin(), x.rend()
- //#define endl '\n'
- #define boostIO() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
- typedef long double ld;
- typedef vector<vector<ld>> matrix;
- matrix mul(const matrix& a, const matrix& b) {
- matrix ans(a.size(), vector<ld>(b[0].size()));
- for (int i = 0; i < a.size(); ++i)
- for (int j = 0; j < b[0].size(); ++j)
- for (int k = 0; k < b.size(); ++k)
- ans[i][j] += a[i][k] * b[k][j];
- return ans;
- }
- void print(string name,const matrix& a) {
- cout << name << " = \n";
- for (auto row : a) {
- for (auto x : row) {
- cout << fixed << setw(15) << x;
- }
- cout << endl;
- }
- }
- int main() {
- matrix A = {
- {352809.0 / 50000,-233253.0 / 100000.0,-662893.0 / 100000.0,406369.0 / 50000.0,-518723.0 / 100000.0},
- {65007.0 / 10000.0,-281899.0 / 100000.0,-451277.0 / 100000.0,-27897.0 / 10000.0,906247.0 / 100000.0},
- {-787103.0 / 100000,17213.0 / 2000,75103.0 / 10000,855647.0 / 100000.0,-614673.0 / 100000.0},
- {15981.0 / 5000.0,-210577.0 / 10000000.0,26733.0 / 4000.0,-493469.0 / 50000.0,31539.0 / 6250.0},
- {177343.0 / 100000.0,-453597.0 / 100000,-187841.0 / 100000,93833.0 / 12500.0,-417783.0 / 50000.0}
- };
- matrix b = { {2.23609}, {-8.3874}, {-2.47353}, {7.43828}, {-0.746177} };
- print("A", A);
- matrix L = {
- {1,0,0,0,0},
- {36115.0 / 39201,1,0,0,0},
- {-787103.0 / 705618,-15692444283.0 / 1751193736,1,0,0},
- {53270.0 / 117603,-135308249341.0 / 87559686800,1436157077576729763.0 / 1702456511488374550,1,0},
- {177343.0 / 705618,10322237821.0 / 1751193736,-22718846198335381.0 / 34049130229767491.0,1572772953187969223765.0 / 3211649824104307778373.0,1}
- };
- print("L", L);
- matrix U = {
- {352809.0 / 50000,-233253.0 / 100000,-662893.0 / 100000,406369.0 / 50000,-518723.0 / 100000},
- {0,-218899217.0 / 326675000,3124935509.0 / 1960050000,-251799599.0 / 24500625,1695608431.0 / 122503125},
- {0,0,34049130229767491.0 / 2364111543600000,-176060634498793.0 / 2364111543600,26501529123999967.0 / 236411154360000},
- {0,0,0,9634949472312923335119.0 / 288551951099724500000.0,-379615477841909321289209.0 / 5771039021994490000000.0},
- {0,0,0,0,118003540165276470656138483873.0 / 6423299648208615556746000000.0}
- };
- print("U", U);
- auto A2 = mul(L, U);
- print("A2", A2);
- matrix y(5, vector<ld>(1));
- for (int i = 0; i < 5; ++i) {
- ld sum = 0;
- for (int j = 0; j < i; ++j)
- sum += L[i][j] * y[j][0];
- y[i][0] = (b[i][0] - sum) / L[i][i];
- }
- print("y", y);
- print("L * y", mul(L, y));
- matrix x(5, vector<ld>(1));
- for (int i = 4; i >= 0; --i) {
- ld sum = 0;
- for (int j = i + 1; j < 5; ++j) {
- sum += U[i][j] * x[j][0];
- }
- x[i][0] = (y[i][0] - sum) / U[i][i];
- }
- print("x", x);
- print("A * x", mul(A, x));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement