#include <bits/stdc++.h> using namespace std; char start[20][20], finish[20][20]; long long binom(int a, int b) { if (b < 0 || a < b) { return 0ll; } long long result = 1; for (long long i = a - b + 1; i <= a; i++) { result *= i; } for (long long i = 2; i <= b; i++) { result /= i; } return result; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; for (int row = 0; row < n; row++) { cin >> start[row]; } for (int row = 0; row < n; row++) { cin >> finish[row]; } int k = 0; int start_parity = 0; for (int row = 0; row < n; row++) { for (int column = 0; column < m; column++) { if (start[row][column] == 'O') { k++; start_parity ^= row; start_parity ^= column; } } } start_parity &= 1; long long numerator = 0, denominator = 0; int finish_parity = 0; for (int row = 0; row < n; row++) { for (int column = 0; column < m; column++) { if (finish[row][column] == 'O') { finish_parity ^= row; finish_parity ^= column; if (row > 0 && finish[row - 1][column] != 'O') { numerator++; } if (row < n - 1 && finish[row + 1][column] != 'O') { numerator++; } if (column > 0 && finish[row][column - 1] != 'O') { numerator++; } if (column < m - 1 && finish[row][column + 1] != 'O') { numerator++; } } } } finish_parity &= 1; if (start_parity != finish_parity) { cout << fixed << setprecision(16) << 0.0 << '\n'; return 0; } denominator = binom(n * m - 2, k - 1) * (m * (n - 1) + n * (m - 1)); cout << fixed << setprecision(16) << (long double) numerator / denominator << '\n'; return 0; }
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 | #include <bits/stdc++.h> using namespace std; char start[20][20], finish[20][20]; long long binom(int a, int b) { if (b < 0 || a < b) { return 0ll; } long long result = 1; for (long long i = a - b + 1; i <= a; i++) { result *= i; } for (long long i = 2; i <= b; i++) { result /= i; } return result; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; for (int row = 0; row < n; row++) { cin >> start[row]; } for (int row = 0; row < n; row++) { cin >> finish[row]; } int k = 0; int start_parity = 0; for (int row = 0; row < n; row++) { for (int column = 0; column < m; column++) { if (start[row][column] == 'O') { k++; start_parity ^= row; start_parity ^= column; } } } start_parity &= 1; long long numerator = 0, denominator = 0; int finish_parity = 0; for (int row = 0; row < n; row++) { for (int column = 0; column < m; column++) { if (finish[row][column] == 'O') { finish_parity ^= row; finish_parity ^= column; if (row > 0 && finish[row - 1][column] != 'O') { numerator++; } if (row < n - 1 && finish[row + 1][column] != 'O') { numerator++; } if (column > 0 && finish[row][column - 1] != 'O') { numerator++; } if (column < m - 1 && finish[row][column + 1] != 'O') { numerator++; } } } } finish_parity &= 1; if (start_parity != finish_parity) { cout << fixed << setprecision(16) << 0.0 << '\n'; return 0; } denominator = binom(n * m - 2, k - 1) * (m * (n - 1) + n * (m - 1)); cout << fixed << setprecision(16) << (long double) numerator / denominator << '\n'; return 0; } |