I. Rikka with Sorting Networks

Codeforces
IDCF10201I
Time4000ms
Memory1024MB
Difficulty
English · Original
Formal · Original
Rikka knows that Bubble sort is a simple but beautiful algorithm, Quicksort is a complex but efficient algorithm, and Shellsort is a weird but practical algorithm. Rikka is interested in all sorting algorithms and she can assign as many new problems for ICPC contests as she wants. Rikka hates those guys who create new problems with the same ideas over and over again, and she hopes not to become the person she hates to be. Though she has already assigned several problems for sorting algorithms such as Merge sort and Insertion sort, she decides to show you the last problem about sorting algorithms to end this series forever. Here Rikka introduces the sorting network and she defines a comparator at first. For a permutation $A$ of the $n$ smallest positive integers denoted by $a_1, a_2, \\\\cdots, a_n$, a comparator $[ u, v ]$ ($u != v$) sorts the $u$-th and the $v$-th element in $A$ into nondecreasing order. Formally, a comparator is a mapping $[ u, v ]$ satisfying Rikka defines a sorting network as a composition of comparators and provides for you a sorting network with $k$ ordered comparators. Now, Rikka wants you to count the number of permutations of $1$ to $n$ which, through the given sorting network, would become an almost sorted permutation. She says a permutation of $1$ to $n$ is almost sorted if the length of its longest increasing subsequence is at least $(n -1)$. The input contains several test cases, and the first line contains a single integer $T$ ($1 <= T <= 100$), the number of test cases. For each test case, the first line contains three integers $n$ ($2 <= n <= 50$), the length of permutations, $k$ ($0 <= k <= 10$), the number of comparators, and $q$ ($10^8 <= q <= 10^9$), a prime number for the output. Then $k$ lines follow, the $i$-th line of which contains two integers $u$ and $v$ $(1 <= u < v <= n)$, representing the $i$-th comparator $[ u, v ]$. For each test case, output a single line with a single integer, the remainder of the number of permutations which meet the requirement divided by $q$. ## Input The input contains several test cases, and the first line contains a single integer $T$ ($1 <= T <= 100$), the number of test cases.For each test case, the first line contains three integers $n$ ($2 <= n <= 50$), the length of permutations, $k$ ($0 <= k <= 10$), the number of comparators, and $q$ ($10^8 <= q <= 10^9$), a prime number for the output.Then $k$ lines follow, the $i$-th line of which contains two integers $u$ and $v$ $(1 <= u < v <= n)$, representing the $i$-th comparator $[ u, v ]$. ## Output For each test case, output a single line with a single integer, the remainder of the number of permutations which meet the requirement divided by $q$. [samples]
**Definitions** Let $ n \in \mathbb{Z} $ be the length of permutations, $ k \in \mathbb{Z} $ the number of comparators, and $ q $ a prime modulus. Let $ \mathcal{S}_n $ be the set of all permutations of $ \{1, 2, \dots, n\} $. A comparator $ [u, v] $ with $ 1 \le u < v \le n $ is a function that, given a permutation $ A = (a_1, \dots, a_n) $, replaces $ (a_u, a_v) $ with $ (\min(a_u, a_v), \max(a_u, a_v)) $. A sorting network is a sequence of $ k $ comparators $ C = ([u_1, v_1], [u_2, v_2], \dots, [u_k, v_k]) $. Let $ f_C : \mathcal{S}_n \to \mathcal{S}_n $ denote the function applying the comparators in order to a permutation. A permutation $ A \in \mathcal{S}_n $ is *almost sorted* if the length of its longest increasing subsequence (LIS) satisfies $ \text{LIS}(A) \ge n - 1 $. **Constraints** 1. $ 1 \le T \le 100 $ 2. $ 2 \le n \le 50 $ 3. $ 0 \le k \le 10 $ 4. $ 10^8 \le q \le 10^9 $, $ q $ prime 5. For each comparator: $ 1 \le u_i < v_i \le n $ **Objective** For each test case, compute: $$ \left| \left\{ A \in \mathcal{S}_n \,\middle|\, \text{LIS}(f_C(A)) \ge n - 1 \right\} \right| \mod q $$
API Response (JSON)
{
  "problem": {
    "name": "I. Rikka with Sorting Networks",
    "description": {
      "content": "Rikka knows that Bubble sort is a simple but beautiful algorithm, Quicksort is a complex but efficient algorithm, and Shellsort is a weird but practical algorithm. Rikka is interested in all sorting a",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 4000,
      "memory_limit": 1048576
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10201I"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Rikka knows that Bubble sort is a simple but beautiful algorithm, Quicksort is a complex but efficient algorithm, and Shellsort is a weird but practical algorithm. Rikka is interested in all sorting a...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z} $ be the length of permutations, $ k \\in \\mathbb{Z} $ the number of comparators, and $ q $ a prime modulus.  \nLet $ \\mathcal{S}_n $ be the set of all permutati...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments