#P9896. Counting Sub‐cycle Graphs
Counting Sub‐cycle Graphs
Counting Sub‐cycle Graphs
An undirected simple graph on n vertices (with n ≥ 3) and m edges is called a sub‐cycle graph if it is possible to add a non‐negative number of edges (without introducing self loops or multiple edges) so that the resulting graph becomes exactly one simple cycle containing all n vertices.
Recall that a simple cycle on n vertices (with n ≥ 3) is a connected undirected simple graph with n edges in which every vertex has degree exactly 2. Two graphs are considered different if their edge‐sets differ (edges are denoted as (u, v) with u < v).
It turns out that an n-vertex graph is a sub–cycle graph if and only if its edge–set is a subset of the edge–set of some cycle on the n vertices. In other words, every edge in the graph is "consecutive" in some cyclic ordering of the vertices.
It can be shown that the answer can be written in closed–form. In particular, if we denote MOD = 109+7, then the number of sub–cycle graphs with n vertices and m edges is computed as follows:
- If m > n, no graph can be extended to an n–cycle, so the answer is 0.
- If m = 0, the empty graph is always a sub–cycle graph (since any cycle contains the empty set), so the answer is 1.
- If 1 ≤ m < n, one may prove that the answer is
\[ answer = \frac{(n-1) \cdot \binom{n}{m}}{2} \pmod{10^9+7}. \] Note: Although every edge appears in some Hamiltonian cycle, the over–counting cancels exactly so that the formula above holds. - If m = n, then the graph is itself a cycle and the number of simple cycles on n labeled vertices is
\[ answer = \frac{(n-1)!}{2} \pmod{10^9+7}. \]
Your task is to compute the answer modulo 109+7 given n and m.
inputFormat
The input consists of a single line containing two integers n and m (n ≥ 3, 0 ≤ m ≤ n).
outputFormat
Output a single integer, the number of different sub–cycle graphs with n vertices and m edges, taken modulo 109+7.
sample
3 0
1