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
| #include <cstdio> #include <algorithm> using namespace std; typedef long long LL; 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 = 7846, M = 110, mod = 1e9 + 7, dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1}; int n, id[M][M], A[N][N]; int binpow (int b, int k = mod - 2) { int res = 1; while (k) { k & 1 && (res = (LL)res * b % mod); b = (LL)b * b % mod; k >>= 1; } return res; } void Gauss (int d) { for (int k = 0; k < n; k++) { int t = min(n - 1, k + d), inv = binpow(A[k][k]); A[k][n] = (LL)A[k][n] * inv % mod; for (int i = t; i >= k; i--) A[k][i] = (LL)A[k][i] * inv % mod; for (int i = k + 1; i <= t; i++) { (A[i][n] -= (LL)A[k][n] * A[i][k] % mod) %= mod; for (int j = t; j >= k; j--) (A[i][j] -= (LL)A[k][j] * A[i][k] % mod) %= mod; } } for (int i = n - 1; ~i; i--) for (int j = i + 1; j < n; j++) (A[i][n] -= (LL)A[i][j] * A[j][n] % mod) %= mod; } int main () { int r, p[4], inv = 0; read(r); for (int i = 0; i < 4; i++) read(p[i]), (inv += p[i]) %= mod; inv = binpow(inv); for (int i = 0; i < 4; i++) p[i] = (LL)p[i] * inv % mod; for (int i = 0; i <= r << 1; i++) for (int j = 0; j <= r << 1; j++) id[i][j] = (i - r) * (i - r) + (j - r) * (j - r) > r * r ? -1 : n++; int d = 0; for (int i = 0; i <= r << 1; i++) for (int j = 0; j <= r << 1; j++) { if (id[i][j] == -1) continue; for (int k = 0; k < 4; k++) { int nx = i + dx[k], ny = j + dy[k]; if (nx < 0 || ny < 0 || nx > r << 1 || ny > r << 1 || id[nx][ny] == -1) continue; A[id[nx][ny]][id[i][j]] = -p[k]; d = max(d, abs(id[i][j] - abs(id[nx][ny]))); } A[id[i][j]][id[i][j]] = 1, A[id[i][j]][n] = 1; } Gauss(d); write((A[id[r][r]][n] + mod) % mod); return 0; }
|