Can anyone try to explain me how to solve this interesting problem.
http://www.spoj.com/SPOJ/problems/WALK1/
Thanks
| № | Пользователь | Рейтинг |
|---|---|---|
| 1 | jiangly | 3810 |
| 2 | Benq | 3676 |
| 3 | Kevin114514 | 3655 |
| 4 | maroonrk | 3463 |
| 5 | strapple | 3390 |
| 6 | Um_nik | 3387 |
| 7 | tourist | 3384 |
| 8 | heuristica | 3322 |
| 9 | turmax | 3319 |
| 10 | jiangbowen | 3291 |
| Страны | Города | Организации | Всё → |
| № | Пользователь | Вклад |
|---|---|---|
| 1 | Qingyu | 156 |
| 2 | nik_exists | 150 |
| 2 | maspy | 150 |
| 4 | Um_nik | 144 |
| 5 | Errichto | 139 |
| 6 | adamant | 137 |
| 6 | AmShZ | 137 |
| 8 | BledDest | 132 |
| 8 | maroonrk | 132 |
| 10 | qwexd | 129 |
Can anyone try to explain me how to solve this interesting problem.
http://www.spoj.com/SPOJ/problems/WALK1/
Thanks
| Название |
|---|



First, we can see that answer for (x, y) is same as answer to (abs(x), abs(y)).
Let's take H and V that H + V = N.
H — how many horizontal moves we will do (west and east). X <= H <= N.
We must reach point X in L left moves and R right moves, so
L + R = H
R — L = x
If we solve this equation, we get R = (H + X) / 2, L = (H — X) / 2.
Then, we need to count number of move-sequences (LR) of length H which have exactly L left moves and R right moves. It is C(H, L) = C(H, R).
V = how many vertical moves we will do (south and norh). Y <= V <= N.
There everything is same as above so U = (V + Y) / 2, D = (V — Y) / 2, and number of sequence is C(V, D)
So for every (H, V) we have a two sequences of lengths H and V. We should merge them and get the sequence of length N. But how? We have a sequence of length N, let's choose H of them and put there a first sequence. There will be N-H positions left which is equal to V.
I can't explain it more detaily because of my bad english, but I think code will do. My code.
UPD. C(N, K) can be found in O(log MOD).