import math (n, m) = tuple(map(int, input().split())) def parity(T): ret = 0 for i in range(n): cur = i&1 for j in range(m): if T[i][j] == 'O' and cur == 0: ret ^= 1 cur ^= 1 return ret def getDeg(T): ret = 0 for i in range(n): for j in range(m): if i != 0 and T[i][j] != T[i-1][j]: ret += 1 if j != 0 and T[i][j] != T[i][j-1]: ret += 1 return ret def getK(T): ret = 0 for line in T: ret += line.count('O') return ret T = [] for i in range(n): T.append(input()) par0 = parity(T) mtLine = input() for i in range(n): T[i] = input() par1 = parity(T) k = getK(T) d = getDeg(T) s = math.comb(n*m-2, k-1) * (n*m*2 - n - m) res = par0 == par1 and d / s or 0 print('{0:.16f}'.format(res))
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 | import math (n, m) = tuple(map(int, input().split())) def parity(T): ret = 0 for i in range(n): cur = i&1 for j in range(m): if T[i][j] == 'O' and cur == 0: ret ^= 1 cur ^= 1 return ret def getDeg(T): ret = 0 for i in range(n): for j in range(m): if i != 0 and T[i][j] != T[i-1][j]: ret += 1 if j != 0 and T[i][j] != T[i][j-1]: ret += 1 return ret def getK(T): ret = 0 for line in T: ret += line.count('O') return ret T = [] for i in range(n): T.append(input()) par0 = parity(T) mtLine = input() for i in range(n): T[i] = input() par1 = parity(T) k = getK(T) d = getDeg(T) s = math.comb(n*m-2, k-1) * (n*m*2 - n - m) res = par0 == par1 and d / s or 0 print('{0:.16f}'.format(res)) |