6. МСГ

 ``` 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``` ```#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #define vd vector #define vvd vector > #define ui unsigned int using namespace std; inline void print(vd g); inline double sqr(const double& a) { return a * a; } inline double dot(const vd& a, const vd& b) { double ret = 0; for (int i = 0; i < a.size(); ++i) ret += a[i] * b[i]; return ret; } inline double norm(const vd& a) { double ret = 0; for (int i = 0; i < a.size(); ++i) ret += sqr(a[i]); return sqrt(ret); } vd operator +(const vd& a, const vd& b) { vd ret = vd(a.size()); for (int i = 0; i < a.size(); ++i) ret[i] = a[i] + b[i]; return ret; } vd operator -(const vd& a, const vd& b) { vd ret = vd(a.size()); for (int i = 0; i < a.size(); ++i) ret[i] = a[i] - b[i]; return ret; } vd operator *(const vd& a, double d) { vd ret = vd(a.size()); for (int i = 0; i < a.size(); ++i) ret[i] = a[i] * d; return ret; } vd operator *(const vvd& a, const vd& b) { vd ret = vd(a.size(), 0.0); for (int i = 0; i < a.size(); ++i) { ret[i] = dot(a[i], b); } return ret; } int n; int k = 0; vvd A; vd B; vvd p; vvd q; vvd x; vvd e; void solve(); int main(){ freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); n; cin >> n; A.resize(n, vd(n, 0.0)); B.resize(n); p.resize(2, vd(n, 0.0)); q.resize(2, vd(n, 0.0)); x.resize(2, vd(n, 0.0)); e.resize(2, vd(n, 0.0)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> A[i][j]; } cin >> B[i]; } /* vd test = A * B; print(test); return 0;*/ e[k] = B - A * x[k]; p[k] = e[k]; solve(); cout << "solution:" << endl; for (int i = 0; i < n; ++i) cout << "x[" << i + 1 << "] " << setw(12) << fixed << setprecision(6) << x[k][i] << endl; cout << endl; } void solve() { /*for (int i = 0; i < n; ++i) cout << "x[" << i + 1 << "] " << setw(12) << fixed << setprecision(6) << x[k][i] << endl; cout << endl << endl;*/ q[k] = A * p[k]; double alpha = dot(e[k], p[k]) / dot(q[k], p[k]); x[1 - k] = x[k] + p[k] * alpha; e[1 - k] = e[k] - q[k] * alpha; double norm_e = norm(e[1 - k]); if (norm(e[1 - k]) < 1e-18) return; double beta = dot(e[1 - k], q[k]) / dot(p[k], q[k]); p[1 - k] = e[1 - k] - p[k] * beta; k = 1 - k; solve(); } inline void print(vd g) { cout << "( "; for (int j = 0; j < n; ++j) cout << setw(12) << fixed << setprecision(6) << g[j]; cout << endl; } ```