No.2871 Universal Serial Bus
Problem Statement
The keyboard that Neguse-kun ordered from an online shopping site last night was delivered to his home this morning.
Eager to try it right away, he tries to connect the keyboard to his PC with a cable, but for some reason, even when he tries to plug it in with the correct orientation, the insertion fails with a certain probability.
When Neguse-kun fails to insert the connector, he rotates the cable by $180^\circ$ so that it is flipped upside down and tries to insert it again, and he continues this until he succeeds.
Here, if the $i$-th insertion is a correct insertion, the probability that the insertion succeeds is $1-2^{-(i-1)}$; otherwise, the probability that the insertion succeeds is $0$.
The shapes $S$ and $T$ of the connectors on the cable side and the PC side are given as $H \times W$ grids.
If the expected number of attempts until the insertion succeeds exists, find that expected value.
If the expected value does not exist (it is infinite), print -1.
Note that the $1$-st insertion is performed in the same orientation as $S$. For two connector shapes $S,T$ represented as $H\times W$ grids, an insertion in a state where there is no pair $(i, j)\ (1 \le i \le H, 1 \le j \le W)$ satisfying $S_{i,j}=T_{i,j}$ is called a correct insertion.
Input
$H\ W$
$S_{1,1}S_{1,2}\dots S_{1, W}$
$S_{2,1}S_{2,2}\dots S_{2, W}$
$\vdots$
$S_{H,1}S_{H,2}\dots S_{H, W}$
$T_{1,1}T_{1,2}\dots T_{1, W}$
$T_{2,1}T_{2,2}\dots T_{2, W}$
$\vdots$
$T_{H,1}T_{H,2}\dots T_{H, W}$
- $3 \le H, W \le 20$
- $S,T$ consist only of
#and. - When $i\in\{1,H\}$ or $j\in\{1,W\}$, $S_{i,j}=$
.and $T_{i,j}=$#
Output
If the expected number of attempts until the insertion succeeds exists, print that expected value on one line. If it does not exist, print -1.
Your answer is judged correct if its absolute error or relative error from the expected answer is at most $10^{-6}$.
Sample
Sample 1
Input
6 20 .................... .##################. .#................#. .##################. .##################. .................... #################### #..................# #.################.# #..................# #..................# ####################
Output
3.5317401904617327
The probability of succeeding on the $1$-st attempt is $1-\frac{1}{2^0}=0,$
the probability of succeeding on the $2$-nd attempt is $0$ (because the connector shapes do not match),
the probability of succeeding on the $3$-rd attempt is $1-\frac{1}{2^2}=\frac{3}{4},$
the probability of succeeding on the $4$-th attempt is $0$ (because the connector shapes do not match),
the probability of succeeding on the $5$-th attempt is $\frac{1}{4}\times(1-\frac{1}{2^4})=\frac{15}{64},$
$\vdots$
and the expected number of attempts until success is $1\cdot0+2\cdot0+3\cdot\frac{3}{4}+4\cdot0+5\cdot\frac{15}{64}+\cdots=3.53174...\dots$.
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
寝癖