#P12215. Delayed Escape via Teleportation Portals
Delayed Escape via Teleportation Portals
Delayed Escape via Teleportation Portals
A thief is at coordinate \(0\) and is running along the positive \(x\)-axis. Normally he will eventually reach \(n+1\) and escape. In order to delay him, Xiao Lan plans to set up \(k\) bidirectional teleportation portals. Each portal connects two distinct integer coordinates \(p\) and \(q\) (with \(p,q\in[1,n]\)). Moreover, every coordinate can be an endpoint for at most one portal.
The process is as follows. The thief starts at \(0\) and moves continuously in the positive direction. Whenever he reaches an integer coordinate that has a portal, the portal triggers and immediately teleports him to its paired endpoint. (However, the same portal cannot be triggered consecutively – that is, if he is teleported using a portal, then upon landing at its other endpoint the portal will not trigger immediately; he must walk away and later reach that endpoint by normal movement for the portal to trigger again.) When no portal can be triggered at his current location, he simply continues to move to the right.
Xiao Lan wants to design the portals so that the thief is teleported at least \(2k\) times during his escape. A bit of thought shows that the maximum number of triggers using \(k\) portals is \(2k\) (each portal is triggered exactly twice) and that achieving this maximum requires a very special configuration of the portal endpoints. In fact, one may prove that a necessary and sufficient condition for all \(k\) portals to be used exactly twice is as follows: first choose \(2k\) distinct integers from \([1,n]\). Then, if we label these numbers in increasing order as \(x_1<x_2<\cdots<x_{2k}\), the only way to force the teleportation chain (which due to backward jumps may make the thief revisit some endpoints via normal walking) is to pair them in the following unique "zigzag" manner:
With this arrangement the thief’s journey is delayed as follows. He starts at \(0\) and walks until he first reaches \(x_1\); there the portal triggers and teleports him to \(x_{1+k}\). Because the same portal cannot trigger consecutively, he continues walking until he eventually reaches one of the other portals’ endpoints – and the backward teleportation (resulting from a crossing of the portals) ensures that every portal gets triggered twice. (A short analysis shows that it is impossible to have every portal triggered twice when \(n\) is too small; in fact a necessary condition is \(n\ge 2k\).)
Your task is: given \(n\) and \(k\), count the number of ways to choose the endpoints for the \(k\) portals such that the above configuration holds and therefore the thief is teleported at least \(2k\) times. It turns out that the only possible configuration is the one described above. Hence, if \(n\ge 2k\) the answer is simply the number of ways to choose \(2k\) distinct points from \(n\), i.e. \(\binom{n}{2k}\); otherwise, the answer is \(0\).
Note that all formulas in this problem must be written in \(\LaTeX\) format.
inputFormat
The input consists of a single line containing two integers \(n\) and \(k\) (with \(1 \le k \le n/2\)).
outputFormat
Output a single integer — the number of valid portal setups. As explained above, if \(n \ge 2k\) the answer is \(\binom{n}{2k}\); otherwise, output \(0\).
sample
7 2
35