Official

D - Not Intersect Editorial by evima


When \(M=0\), the answer is \(1\). Hereafter, we assume \(M \ge 1\).

In fact, when \(N\) is fixed, it can be proven that \(M \le 2N - 3\) is necessary for the answer to be non-zero. We will assume \(M \le 2N - 3\) from now on. (By taking one edge that connects non-adjacent vertices on the circumference, we can establish a recurrence relation, and the proof can be done inductively.)

If we allow multi-edges, all graphs can be constructed by the following procedure:

  • Create an empty sequence $A$ and perform the following for $v=1,2,\dots,N$ in this order.
    • Perform the following any number of times: "Let $u$ be the last element of $A$. Add an edge connecting vertex $u$ and vertex $v$, and remove $u$ from $A$."
    • Insert $v$ any number of times at the end of $A$.

It can be confirmed that the graphs obtained by this procedure satisfy the conditions, that graphs obtained by different procedures are different, and that all graphs satisfying the conditions can be obtained by this procedure.

Now, let \(F(x)\) and \(G(x)\) be the generating functions for the number of ways without and with allowing multi-edges, respectively. Here, \(G_i = \sum_{j=0}^{i} \binom{i-1}{j-1} F_i\). That is, if we define \(F'(x) = \sum_{i=1}^{\infty} \frac{F_ix^{i-1}}{(i-1)!}\) and \(G'(x) = \sum_{i=1}^{\infty} \frac{G_ix^{i-1}}{(i-1)!}\), then \(F'(x)e^x = G'(x)\). Therefore, from \(G'(x)e^{-x} = F'(x)\), it follows that \(F_M= (M-1)! \sum_{i=0}^{M-1} \frac{(-1)^{M+i+1}G_{i+1}}{i!(M-1-i)!}\).

Let’s consider how to quickly find \(G_k\) for \(k=1,2,\dots,M\). \(G_k\) can be rephrased as the answer to the following problem:

You are initially at $(0,0)$. Find the number of ways to reach $(k,k)$ by repeating the following moves $N$ times, with the condition that the $x$ coordinate must always be greater than or equal to the $y$ coordinate.
  • Choose a non-negative integer $p$ and add $p$ to the $y$-coordinate.
  • Choose a non-negative integer $q$ and add $q$ to the $x$-coordinate.

First, it is unnecessary to move the \(y\)-coordinate in the first step and the \(x\)-coordinate in the \(N\)-th step, so it can be rephrased as follows:

You are initially at $(0,0)$. Find the number of ways to reach $(k,k)$ by repeating the following moves $N-1$ times, with the condition that the $x$ coordinate must always be greater than or equal to the $y$ coordinate.
  • Choose a non-negative integer $q$ and add $q$ to the $x$-coordinate.
  • Choose a non-negative integer $p$ and add $p$ to the $y$-coordinate.

Furthermore, considering the transitions of the \(x\)- and \(y\)-coordinates separately, it can be rephrased as follows:

Find the number of pairs of non-negative integer sequences of length $N$, $A=(A_0,A_1,\dots,A_{N-1})$ and $B=(B_0,B_1,\dots,B_{N-1})$, that satisfy the following conditions:
  • $0 = A_0 \le A_1 \le A_2 \le \dots \le A_{N-1} = k$
  • $0 = B_0 \le B_1 \le B_2 \le \dots \le B_{N-1} = k$
  • $A_i \ge B_i(0 \le i \le k')$

Then, by plotting \(A\) as a path \((0,0) \rightarrow (A_1,0) \rightarrow (A_1,1) \rightarrow (A_2,1) \rightarrow (A_2,2) \rightarrow \dots \rightarrow (A_{N-2},N-3) \rightarrow (A_{N-2},N-2) \rightarrow (A_{N-1},N-2)\) and \(B\) similarly, the problem can be further rephrased as follows:

Find the number of pairs of paths $(P,Q)$ from $(0,0)$ to $(k,N-2)$ that only move right or up such that $P$ is always above $Q$ (they may intersect).

Here, let us fix the numbers of times the \(x\)-coordinates of \(P\) and \(Q\) change by \((0,0),(0,+1),(+1,0),(+1,+1)\) in the sequence of \(N+k-2\) moves to be \(a,b,c,d\), respectively. These \(a,b,c,d\) can be expressed as \((N-2-i,i,i,k-i)\) using a non-negative integer \(i\). Then, the number of pairs \((P,Q)\) that satisfy the condition in this case is \(\frac{C_i(N+k-2)}{(N-2-i)!(2i)!(k-i)!}\). (\(C_i\) is the Catalan number)

Thus, the answer to this problem is \(\sum_{i=0}^{k} \frac{C_i(N+k-2)}{(N-2-i)!(2i)!(k-i)!} = \sum_{i=0}^{k} \frac{(N+k-2)!}{(N-2-i)i!(i+1)!(k-i)!}\).

Now, let’s consider the following problem out of the blue:

There are $N+k-1$ balls. Paint $N-2$ of them red and $k$ of them blue. Balls painted in both colors become purple. Find the number of ways to paint the balls.

The answer to this problem is \(\binom{N+k-1}{N-1}\binom{N+k-1}{N-2}\). It can also be expressed by an exhaustive search of the number of purple balls as \(\sum_{i=0}^{k} \frac{(N+k-1)!}{(N-2-i)i!(i+1)!(k-i)!}\). Thus, \(\sum_{i=0}^{k} \frac{(N+k-1)!}{(N-2-i)i!(i+1)!(k-i)!} = \binom{N+k-1}{N-1}\binom{N+k-1}{N-2}\).

Using this, we can see that the above expression \(\sum_{i=0}^{k} \frac{(N+k-2)!}{(N-2-i)i!(i+1)!(k-i)!}\) equals \(\frac{\binom{N+k-1}{N-1}\binom{N+k-1}{N-2}}{N+k-1}\). Therefore, \(G_k = \frac{\binom{N+k-1}{N-1}\binom{N+k-1}{N-2}}{N+k-1}\).

Therefore, the answer is \((M-1)! \sum_{i=0}^{M-1} \frac{(-1)^{M+i+1}\binom{N+i}{N-1}\binom{N+i}{N-2}}{i!(M-1-i)!(N+i)}\). This value can be calculated in \(\mathrm{O}(N)\) time.

posted:
last update: