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
| #include <cstdio> #include <algorithm> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; flag && (x = ~x + 1); } template <class Type> void write(Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar(x % 10 + '0'); } const int N = 1e3 + 10; double f[N][N], A[N][N]; void Gauss (int n, int d) { for (int k = 0; k < n; k++) { int t = min(n - 1, k + d); A[k][n] /= A[k][k]; for (int i = t; i >= k; i--) A[k][i] /= A[k][k]; for (int i = k + 1; i <= t; i++) { A[i][n] -= A[k][n] * A[i][k]; for (int j = t; j >= k; j--) A[i][j] -= A[k][j] * A[i][k]; } } for (int i = n - 1; ~i; i--) for (int j = i + 1; j < n; j++) A[i][n] -= A[i][j] * A[j][n]; } int main () { int n, m, p, q; read(n), read(m), read(p), read(q); if (m == 1) return printf("%.8lf", 2.0 * (n - p)), 0; for (int i = n - 1; i; i--) { A[0][0] = A[m - 1][m - 1] = 2; A[0][1] = A[m - 1][m - 2] = -1; A[0][m] = f[i + 1][0] + 3; A[m - 1][m] = f[i + 1][m - 1] + 3; for (int j = 1; j + 1 < m; j++) { A[j][j] = 3; A[j][j - 1] = A[j][j + 1] = -1; A[j][m] = 4 + f[i + 1][j]; } Gauss(m, 1); for (int j = 0; j < m; j++) f[i][j] = A[j][m]; } printf("%.8lf", f[p][q - 1]); return 0; }
|