I'm stuck at the following post: How many ways are there to place the knights on the m*n chessboard so that they do not attack each other (modulo 10^9+9) M<=4, N<=10^9
| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3603 |
| 4 | jiangly | 3583 |
| 5 | turmax | 3559 |
| 6 | tourist | 3541 |
| 7 | strapple | 3515 |
| 8 | ksun48 | 3461 |
| 9 | dXqwq | 3436 |
| 10 | Otomachi_Una | 3413 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 157 |
| 2 | adamant | 153 |
| 3 | Um_nik | 147 |
| 4 | Proof_by_QED | 146 |
| 5 | Dominater069 | 145 |
| 6 | errorgorn | 141 |
| 7 | cry | 139 |
| 8 | YuukiS | 135 |
| 9 | TheScrasse | 134 |
| 10 | chromate00 | 133 |
I'm stuck at the following post: How many ways are there to place the knights on the m*n chessboard so that they do not attack each other (modulo 10^9+9) M<=4, N<=10^9
| Название |
|---|



[deleted]
So how can I implement this idea? If yes, can you be more specific?
[deleted]
I misread the question. Ignore that.
answer is max(amount of black squares,amount of white squares)
nevermind, read it wrong
Firstly you can notice that the condition $$$M ≤ 4$$$ is kinda suspicious.
Then you can use bitmask DP: $$$DP[i][mask]$$$ is the total number of ways after we have already built $$$2 * i$$$ columns and the last 2 columns have the state $$$mask$$$, bit $$$1$$$ stand for placing a knight. Just remember to handle the case where $$$n$$$ is odd.
Obviously, with the complexity of $$$O(N * 2 ^ M)$$$, this approach is not good enough. So we have to use matrix exponentiation. If you have not heard about it, you can watch it here.
With the help of matrix exponentiation, our time complexity would be $$$O((2 ^ M)^ 3 * log N)$$$, fast enough to pass the 1s time limit.
If you find any mistake please tell me ^_^
EDIT: The complexity is wrong, a mask represents 2 columns (8 cells and I forgot this :( ), so 1s is not enough. The correct complexity has already been mentioned by A_Le_K
Exponentiation of adjacency matrix for a graph of masks transitions? Nice.
If I'm not missing anything, you need $$$2M$$$ (instead of $$$M$$$) bits to "remember" the state of 2 previous columns (each column having $$$M$$$ bits that represent state of board cells of that column), so there'll be $$$2^{(2M)} = 4^M$$$ instead of $$$2^M$$$ possible $$$mask$$$ values.
So the mentioned complexities will be more like $$$O(N∗4^M)$$$ and $$$O((4^M)^3∗logN)$$$ respectively.
However it seems that it's possible to improve from $$$4^M$$$ $$$mask$$$ states to $$$3^M$$$ states (for $$$M$$$ divisible by 4, other $$$M$$$ are left as an exercise to the reader). For $$$M=4$$$ if we enumerate the bits (state of board cells: 1 — there's a knight, 0 — there's no knight) of 2 last columns as follows:
$$$X_1 | X_2$$$
$$$X_3 | X_4$$$
$$$Y_2 | Y_1$$$
$$$Y_4 | Y_3$$$
We note that if $$$mask$$$ corresponds to correct filling of the board with knights, then for each $$$i$$$ each of $$$(X_i, Y_i) \neq (1, 1)$$$ (since knights on board cells $$$X_i, Y_i$$$ attack each other), so we can only have $$$(X_i, Y_i) = (0, 0) or (0, 1) or (1, 0)$$$ (3 possible values).
Also, it seems to me that with the first approach ($$$O(N∗2^M)$$$ in your analysis, $$$O(N∗4^M)$$$ in my analysis) the correct complexity should be $$$O(N∗M∗2^{2M+1})$$$ (if we use a messy approach where columns $$$i, i+2$$$ are partially filled, column $$$i+1$$$ is filled fully).
With the second approach (matrix exponentiation) (for $$$M$$$ divisible by 4), $$$O((3^M)^3∗logN)$$$ still seems achievable.
Oh, thanks for pointing me out! I did forget that the mask represents 2 columns, which led to completely wrong complexity for both approaches (tbh I always forget important things).
And I have a question, for $$$M$$$ doesn't divisible by 4, can we calculate the numbers of ways for the table size $$$N * (M - M mod 4)$$$ and combine it with the table size $$$N * (M mod 4)$$$ ?
I don't see a way how to do it efficiently, since in this case these 2 tables have a long common border of size $$$N \leq 10^9 $$$ (consequently, we may need to "remember" up to $$$4*N$$$ bits for 2 bottom and 2 top rows of upper/lower table).
Expanding on my remark about $$$3^M$$$ masks/states, here's a small script to count number of "good" state masks (here $$$x$$$ is mask for column $$$i$$$, $$$y$$$ is mask for column $$$i+1$$$, also note that in previous comment example $$$M=4$$$ I've used different enumeration order for $$$X, Y$$$)
So if the time limit wasn't very strict (or we had very efficient matrix multiplication implementation), then maybe we could even solve this problem even for $$$M \leq 6, N \leq 10^9$$$ ($$$625^3 * log_2(10^9)$$$ operations)
UPD: coming to think about it,
countGoodXYmasks(M)is just a number of ways to place knights on $$$2 * M$$$ table.Thank you! The $$$O((3^M)^3 * log N)$$$ optimization is really beautiful btw ^_~