G. Partitions

Codeforces
IDCF961G
Time2000ms
Memory256MB
Difficulty
combinatoricsmathnumber theory
English · Original
Chinese · Translation
Formal · Original
You are given a set of _n_ elements indexed from 1 to _n_. The weight of _i_\-th element is _w__i_. The weight of some subset of a given set is denoted as . The weight of some partition _R_ of a given set into _k_ subsets is (recall that a partition of a given set is a set of its subsets such that every element of the given set belongs to exactly one subset in partition). Calculate the sum of weights of all partitions of a given set into exactly _k_ **non-empty** subsets, and print it modulo 109 + 7. Two partitions are considered different iff there exist two elements _x_ and _y_ such that they belong to the same set in one of the partitions, and to different sets in another partition. ## Input The first line contains two integers _n_ and _k_ (1 ≤ _k_ ≤ _n_ ≤ 2·105) — the number of elements and the number of subsets in each partition, respectively. The second line contains _n_ integers _w__i_ (1 ≤ _w__i_ ≤ 109)— weights of elements of the set. ## Output Print one integer — the sum of weights of all partitions of a given set into _k_ **non-empty** subsets, taken modulo 109 + 7. [samples] ## Note Possible partitions in the first sample: 1. {{1, 2, 3}, {4}}, _W_(_R_) = 3·(_w_1 + _w_2 + _w_3) + 1·_w_4 = 24; 2. {{1, 2, 4}, {3}}, _W_(_R_) = 26; 3. {{1, 3, 4}, {2}}, _W_(_R_) = 24; 4. {{1, 2}, {3, 4}}, _W_(_R_) = 2·(_w_1 + _w_2) + 2·(_w_3 + _w_4) = 20; 5. {{1, 3}, {2, 4}}, _W_(_R_) = 20; 6. {{1, 4}, {2, 3}}, _W_(_R_) = 20; 7. {{1}, {2, 3, 4}}, _W_(_R_) = 26; Possible partitions in the second sample: 1. {{1, 2, 3, 4}, {5}}, _W_(_R_) = 45; 2. {{1, 2, 3, 5}, {4}}, _W_(_R_) = 48; 3. {{1, 2, 4, 5}, {3}}, _W_(_R_) = 51; 4. {{1, 3, 4, 5}, {2}}, _W_(_R_) = 54; 5. {{2, 3, 4, 5}, {1}}, _W_(_R_) = 57; 6. {{1, 2, 3}, {4, 5}}, _W_(_R_) = 36; 7. {{1, 2, 4}, {3, 5}}, _W_(_R_) = 37; 8. {{1, 2, 5}, {3, 4}}, _W_(_R_) = 38; 9. {{1, 3, 4}, {2, 5}}, _W_(_R_) = 38; 10. {{1, 3, 5}, {2, 4}}, _W_(_R_) = 39; 11. {{1, 4, 5}, {2, 3}}, _W_(_R_) = 40; 12. {{2, 3, 4}, {1, 5}}, _W_(_R_) = 39; 13. {{2, 3, 5}, {1, 4}}, _W_(_R_) = 40; 14. {{2, 4, 5}, {1, 3}}, _W_(_R_) = 41; 15. {{3, 4, 5}, {1, 2}}, _W_(_R_) = 42.
你被给定一个包含 #cf_span[n] 个元素的集合,元素编号从 #cf_span[1] 到 #cf_span[n]。第 #cf_span[i] 个元素的权重为 #cf_span[wi]。一个子集的权重定义为该子集中所有元素权重之和。一个将给定集合划分为 #cf_span[k] 个子集的划分 #cf_span[R] 的权重定义为 (回忆:一个划分是原集合的若干子集的集合,满足原集合中的每个元素恰好属于其中一个子集)。 计算将给定集合划分为恰好 #cf_span[k] 个 *非空* 子集的所有划分的权重之和,并对 #cf_span[109 + 7] 取模输出。两个划分被认为是不同的,当且仅当存在两个元素 #cf_span[x] 和 #cf_span[y],使得它们在其中一个划分中属于同一个子集,而在另一个划分中属于不同的子集。 第一行包含两个整数 #cf_span[n] 和 #cf_span[k](#cf_span[1 ≤ k ≤ n ≤ 2·105])——元素个数和每个划分中的子集个数。 第二行包含 #cf_span[n] 个整数 #cf_span[wi](#cf_span[1 ≤ wi ≤ 109])——集合中各元素的权重。 输出一个整数——将给定集合划分为 #cf_span[k] 个 *非空* 子集的所有划分的权重之和,对 #cf_span[109 + 7] 取模。 ## Input 第一行包含两个整数 #cf_span[n] 和 #cf_span[k](#cf_span[1 ≤ k ≤ n ≤ 2·105])——元素个数和每个划分中的子集个数。第二行包含 #cf_span[n] 个整数 #cf_span[wi](#cf_span[1 ≤ wi ≤ 109])——集合中各元素的权重。 ## Output 输出一个整数——将给定集合划分为 #cf_span[k] 个 *非空* 子集的所有划分的权重之和,对 #cf_span[109 + 7] 取模。 [samples] ## Note 第一个样例的可能划分: #cf_span[{{1, 2, 3}, {4}}], #cf_span[W(R) = 3·(w1 + w2 + w3) + 1·w4 = 24]; #cf_span[{{1, 2, 4}, {3}}], #cf_span[W(R) = 26]; #cf_span[{{1, 3, 4}, {2}}], #cf_span[W(R) = 24]; #cf_span[{{1, 2}, {3, 4}}], #cf_span[W(R) = 2·(w1 + w2) + 2·(w3 + w4) = 20]; #cf_span[{{1, 3}, {2, 4}}], #cf_span[W(R) = 20]; #cf_span[{{1, 4}, {2, 3}}], #cf_span[W(R) = 20]; #cf_span[{{1}, {2, 3, 4}}], #cf_span[W(R) = 26]; 第二个样例的可能划分: #cf_span[{{1, 2, 3, 4}, {5}}], #cf_span[W(R) = 45]; #cf_span[{{1, 2, 3, 5}, {4}}], #cf_span[W(R) = 48]; #cf_span[{{1, 2, 4, 5}, {3}}], #cf_span[W(R) = 51]; #cf_span[{{1, 3, 4, 5}, {2}}], #cf_span[W(R) = 54]; #cf_span[{{2, 3, 4, 5}, {1}}], #cf_span[W(R) = 57]; #cf_span[{{1, 2, 3}, {4, 5}}], #cf_span[W(R) = 36]; #cf_span[{{1, 2, 4}, {3, 5}}], #cf_span[W(R) = 37]; #cf_span[{{1, 2, 5}, {3, 4}}], #cf_span[W(R) = 38]; #cf_span[{{1, 3, 4}, {2, 5}}], #cf_span[W(R) = 38]; #cf_span[{{1, 3, 5}, {2, 4}}], #cf_span[W(R) = 39]; #cf_span[{{1, 4, 5}, {2, 3}}], #cf_span[W(R) = 40]; #cf_span[{{2, 3, 4}, {1, 5}}], #cf_span[W(R) = 39]; #cf_span[{{2, 3, 5}, {1, 4}}], #cf_span[W(R) = 40]; #cf_span[{{2, 4, 5}, {1, 3}}], #cf_span[W(R) = 41]; #cf_span[{{3, 4, 5}, {1, 2}}], #cf_span[W(R) = 42].
**Definitions** Let $ n, k \in \mathbb{Z}^+ $ with $ 1 \leq k \leq n \leq 2 \cdot 10^5 $. Let $ W = (w_1, w_2, \dots, w_n) $ be a sequence of positive integers representing the weights of $ n $ elements. Let $ \mathcal{P}_{n,k} $ denote the set of all partitions of the set $ [n] = \{1, 2, \dots, n\} $ into exactly $ k $ non-empty, unlabeled subsets. For a partition $ R \in \mathcal{P}_{n,k} $, define its weight as: $$ \text{weight}(R) = \sum_{S \in R} \left( \sum_{i \in S} w_i \right) $$ **Constraints** 1. $ 1 \leq k \leq n \leq 2 \cdot 10^5 $ 2. $ 1 \leq w_i \leq 10^9 $ for all $ i \in [n] $ **Objective** Compute: $$ \sum_{R \in \mathcal{P}_{n,k}} \text{weight}(R) \mod (10^9 + 7) $$
Samples
Input #1
4 2
2 3 2 3
Output #1
160
Input #2
5 2
1 2 3 4 5
Output #2
645
API Response (JSON)
{
  "problem": {
    "name": "G. Partitions",
    "description": {
      "content": "You are given a set of _n_ elements indexed from 1 to _n_. The weight of _i_\\-th element is _w__i_. The weight of some subset of a given set is denoted as . The weight of some partition _R_ of a given",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF961G"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a set of _n_ elements indexed from 1 to _n_. The weight of _i_\\-th element is _w__i_. The weight of some subset of a given set is denoted as . The weight of some partition _R_ of a given...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "你被给定一个包含 #cf_span[n] 个元素的集合,元素编号从 #cf_span[1] 到 #cf_span[n]。第 #cf_span[i] 个元素的权重为 #cf_span[wi]。一个子集的权重定义为该子集中所有元素权重之和。一个将给定集合划分为 #cf_span[k] 个子集的划分 #cf_span[R] 的权重定义为  (回忆:一个划分是原集合的若干子集的集合,满足原集合中的每个元素...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, k \\in \\mathbb{Z}^+ $ with $ 1 \\leq k \\leq n \\leq 2 \\cdot 10^5 $.  \nLet $ W = (w_1, w_2, \\dots, w_n) $ be a sequence of positive integers representing the weights of $ n $ el...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments