#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i, m, n) for(int i=int(m);i inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } typedef long long int ll; using ll = long long int; using ull = long long unsigned int; using Int = long long int; using namespace std; #define INF (1 << 30) - 1 #define INFl (ll)5e15 #define DEBUG 0 #define dump(x) cerr << #x << " = " << (x) << endl #define MOD 1000000007 //edit class Solve { public: Int N, M; double p; void init() { cin >> N >> M >> p; } void init(Int N_, Int M_, double p_) { N = N_; M = M_; p = p_; } double solve() { if (N == 1 && M == 1) { cout << p << endl; return p; } if (N > M) swap(N, M); if (N == 1) { double ans = (M - 2) * pow(p, 3) + 2 * pow(p, 2); cout << ans << endl; return ans; } Int cnt_corner = 4; // if (N == 1 && M == 1) cnt_corner = 1; // else if (N == 1 || M == 1) cnt_corner = 2; Int cnt_outer = max(0ll, N - 2) * 2 + max(0ll, M - 2) * 2; Int cnt_inner = N * M - cnt_corner - cnt_outer; double p_corner = pow(p, 3); double p_outer = pow(p, 4); double p_inner = pow(p, 5); double ans = cnt_corner * p_corner + cnt_outer * p_outer + cnt_inner * p_inner; cout << ans << endl; return ans; } }; class Fool { public: Int N, M; double p; void init() { cin >> N >> M >> p; } void init(Int N_, Int M_, double p_) { N = N_; M = M_; p = p_; } double solve() { // モンテカルロ法で解く Int mon = (Int) 1e3; Int total_score = 0; rep(_, 0, mon) { vector> field(N + 2, vector(M + 2)); for (int i = 1; i <= N; ++i) { for (int j = 1; j <= M; ++j) { Int R = 1000000; if (rand() % R < R * p) { field[i][j] = 1; } else { field[i][j] = -1; } } } Int score = 0; for (int i = 1; i <= N; ++i) { for (int j = 1; j <= M; ++j) { bool flag = field[i][j] == 1; int di[] = {1, 0, -1, 0}; int dj[] = {0, 1, 0, -1}; for (int k = 0; k < 4; ++k) { if (field[i + di[k]][j + dj[k]] == -1) { flag = false; } } if (flag) score++; } } total_score += score; } double ans = 1. * total_score / mon; cout << ans << endl; return ans; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); // Solve().solve(); bool debug = true; debug = false; if (!debug) { Solve solve; solve.init(); solve.solve(); } else { while (true) { Int MIN_N = 1; Int MAX_N = 100; Int MIN_M = 1; Int MAX_M = 100; Int R = 10000000; Int N = MIN_N + rand() % (MAX_N - MIN_N + 1); Int M = MIN_M + rand() % (MAX_M - MIN_M + 1); double p = 1. * (rand() % R) / R; cout << "Input" << endl; cout << N << " " << M << " " << p << endl; cout << "Output" << endl; Solve solve; solve.init(N, M, p); double sans = solve.solve(); Fool fool; fool.init(N, M, p); double fans = fool.solve(); double sotai = (sans - fans) / fans; if (fabs(sotai) > 1e-1 && abs(sans - fans) > 1e-1) { exit(-1); } } } return 0; }