D. Almost Identity Permutations

Codeforces
IDCF888D
Time2000ms
Memory256MB
Difficulty
combinatoricsdpmath
English · Original
Chinese · Translation
Formal · Original
A permutation _p_ of size _n_ is an array such that every integer from 1 to _n_ occurs exactly once in this array. Let's call a permutation an _almost identity permutation_ iff there exist at least _n_ - _k_ indices _i_ (1 ≤ _i_ ≤ _n_) such that _p__i_ = _i_. Your task is to count the number of _almost identity_ permutations for given numbers _n_ and _k_. ## Input The first line contains two integers _n_ and _k_ (4 ≤ _n_ ≤ 1000, 1 ≤ _k_ ≤ 4). ## Output Print the number of _almost identity_ permutations for given _n_ and _k_. [samples]
一个大小为 $n$ 的排列 $p$ 是一个数组,其中从 $1$ 到 $n$ 的每个整数恰好出现一次。 我们称一个排列为 _几乎恒等排列_,当且仅当至少有 $n - k$ 个下标 $i$($1 ≤ i ≤ n$)满足 $p_i = i$。 你的任务是计算给定数字 $n$ 和 $k$ 的 _几乎恒等排列_ 的数量。 第一行包含两个整数 $n$ 和 $k$($4 ≤ n ≤ 1000$,$1 ≤ k ≤ 4$)。 请输出给定 $n$ 和 $k$ 的 _几乎恒等排列_ 的数量。 ## Input 第一行包含两个整数 $n$ 和 $k$($4 ≤ n ≤ 1000$,$1 ≤ k ≤ 4$)。 ## Output 请输出给定 $n$ 和 $k$ 的 _几乎恒等排列_ 的数量。 [samples]
Let $ n, k \in \mathbb{N} $ with $ 4 \leq n \leq 1000 $, $ 1 \leq k \leq 4 $. Let $ S_n $ denote the symmetric group on $ n $ elements, i.e., the set of all permutations of $ \{1, 2, \dots, n\} $. A permutation $ \pi \in S_n $ is an *almost identity permutation* if the number of fixed points satisfies: $$ |\{ i \in \{1, 2, \dots, n\} \mid \pi(i) = i \}| \geq n - k. $$ We are to compute: $$ \sum_{j = n - k}^{n} \binom{n}{j} \cdot D_{n - j} $$ where $ D_m $ denotes the number of derangements of $ m $ elements (i.e., permutations of $ m $ elements with no fixed points), and $ D_0 = 1 $. (Note: $ \binom{n}{j} $ counts the ways to choose $ j $ fixed points, and $ D_{n-j} $ counts the derangements of the remaining $ n-j $ elements.)
Samples
Input #1
4 1
Output #1
1
Input #2
4 2
Output #2
7
Input #3
5 3
Output #3
31
Input #4
5 4
Output #4
76
API Response (JSON)
{
  "problem": {
    "name": "D. Almost Identity Permutations",
    "description": {
      "content": "A permutation _p_ of size _n_ is an array such that every integer from 1 to _n_ occurs exactly once in this array. Let's call a permutation an _almost identity permutation_ iff there exist at least _",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF888D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "A permutation _p_ of size _n_ is an array such that every integer from 1 to _n_ occurs exactly once in this array.\n\nLet's call a permutation an _almost identity permutation_ iff there exist at least _...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "一个大小为 $n$ 的排列 $p$ 是一个数组,其中从 $1$ 到 $n$ 的每个整数恰好出现一次。\n\n我们称一个排列为 _几乎恒等排列_,当且仅当至少有 $n - k$ 个下标 $i$($1 ≤ i ≤ n$)满足 $p_i = i$。\n\n你的任务是计算给定数字 $n$ 和 $k$ 的 _几乎恒等排列_ 的数量。\n\n第一行包含两个整数 $n$ 和 $k$($4 ≤ n ≤ 1000$,$1 ≤ ...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ n, k \\in \\mathbb{N} $ with $ 4 \\leq n \\leq 1000 $, $ 1 \\leq k \\leq 4 $.\n\nLet $ S_n $ denote the symmetric group on $ n $ elements, i.e., the set of all permutations of $ \\{1, 2, \\dots, n\\} $.\n\nA...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments