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;
- }
- }
- const ld EPS = 1e-6;
- const ld INF = LONG_MAX;
- int gauss(matrix& a, vector<ld>& ans) {
- int n = (int)a.size();
- int m = (int)a[0].size() - 1;
- vector<int> where(m, -1);
- for (int col = 0, row = 0; col < m && row < n; ++col) {
- int sel = row;
- for (int i = row; i < n; ++i)
- if (abs(a[i][col]) > abs(a[sel][col]))
- sel = i;
- if (abs(a[sel][col]) < EPS)
- continue;
- for (int i = col; i <= m; ++i)
- swap(a[sel][i], a[row][i]);
- where[col] = row;
- for (int i = 0; i < n; ++i)
- if (i != row) {
- double c = a[i][col] / a[row][col];
- for (int j = col; j <= m; ++j)
- a[i][j] -= a[row][j] * c;
- }
- ++row;
- }
- //print("a", a);
- ans.assign(m, 0);
- for (int i = 0; i < m; ++i)
- if (where[i] != -1)
- ans[i] = a[where[i]][m] / a[where[i]][i];
- for (int i = 0; i < n; ++i) {
- double sum = 0;
- for (int j = 0; j < m; ++j)
- sum += ans[j] * a[i][j];
- if (abs(sum - a[i][m]) > EPS)
- return 0;
- }
- for (int i = 0; i < m; ++i)
- if (where[i] == -1)
- return INF;
- return 1;
- }
- ld a = -2;
- ld b = -1;
- ld y_a = 0;
- ld y_b = 0;
- ld p(ld x) {
- return 0;
- }
- ld q(ld x) {
- return -2.0 / (x * x);
- }
- ld f(ld x) {
- return 3 * log(-x);
- }
- vector<ld> get_y(int n) {
- ld h = (b - a) / n;
- vector<ld> x(n + 1), y(n + 1);
- x[0] = a;
- for (int i = 1; i <= n; ++i) {
- x[i] = a + i * h;
- }
- vector<vector<ld>> mtrx(n - 1, vector<ld>(n));
- vector<ld> ans(n - 1);
- mtrx[0][0] = h * h * q(x[1]) - 2;
- mtrx[0][1] = 1 + h / 2 * p(x[1]);
- mtrx[0].back() = h * h * f(x[1]) - (1 - h / 2 * p(x[1])) * y_a;
- for (int i = 1; i < n - 2; ++i) {
- mtrx[i][i - 1] = (1 - h / 2 * p(x[i + 1]));
- mtrx[i][i] = h * h * q(x[i + 1]) - 2;
- mtrx[i][i + 1] = 1 + h / 2 * p(x[i + 1]);
- mtrx[i].back() = h * h * f(x[i]);
- }
- mtrx[n - 2][n - 3] = (1 - h / 2 * p(x[n - 1]));
- mtrx[n - 2][n - 2] = h * h * q(x[n - 1]) - 2;
- mtrx[n - 2].back() = h * h * f(x[n - 1]) - (1 + h / 2 * p(x[n - 1])) * y_b;
- gauss(mtrx, ans);
- vector<ld> result(n + 1);
- result[0] = y_a;
- result[n] = y_b;
- for (int i = 0; i < n - 1; ++i)
- result[i + 1] = ans[i];
- return result;
- }
- signed main() {
- for (int n = 5;; ++n) {
- auto y1 = get_y(n); //size == n + 1
- auto y2 = get_y(2 * n); //size == 2 * n + 1
- ld max_eps = 0;
- for (int i = 0; i <= n; ++i) {
- ld mx = max(y1[i], y2[i * 2]);
- ld mn = min(y1[i], y2[i * 2]);
- max_eps = max(max_eps, mx / mn - 1);
- }
- if (max_eps < 0.01) {
- cout << "n = " << n << "\n";
- cout << "ANS: \n";
- for (auto y : y1) cout << y << "\n";
- break;
- }
- }
- }
- /*
- 0 //
- 1 =====
- 2
- 3
- n - 3
- n - 2
- n - 1 ====
- n //
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement