QOJ.ac

QOJ

时间限制: 1.0 s 内存限制: 256 MB 总分: 100 可 Hack ✓

#11769. 另一个期望值问题

统计

给定一个包含 $n$ 个整数的数组 $a$。你需要执行以下过程 $k$ 次:

  • 均匀随机选择一个整数 $i$,满足 $1 \le i \le n$。
  • 对于每个 $1 \le j \le n$,将 $a_j$ 向 $a_i$ 的方向移动一个单位。形式化地,对于每个 $j$:
    • 如果 $a_j < a_i$,将 $a_j$ 加 1。
    • 如果 $a_j > a_i$,将 $a_j$ 减 1。
    • 如果 $a_j = a_i$,则不改变 $a_j$ 的值。

在执行该过程 $k$ 次后,你均匀随机选择一个整数 $i$($1 \le i \le n$)。求 $a_i$ 的期望值。

可以证明该值可以表示为 $\frac{P}{Q}$,其中 $P$ 和 $Q$ 是互质的整数且 $Q \not\equiv 0 \pmod{10^9 + 7}$。请输出 $P \cdot Q^{-1}$ 对 $10^9 + 7$ 取模的结果。

输入格式

第一行包含一个整数 $t$ ($1 \le t \le 10^4$) —— 测试用例的数量。

每个测试用例的第一行包含两个整数 $n$ 和 $k$ ($1 \le n, k \le 2 \cdot 10^5$) —— 数组的长度和执行的操作次数。

每个测试用例的第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) —— 初始数组 $a$。

保证所有测试用例中 $n$ 的总和以及 $k$ 的总和均不超过 $2 \cdot 10^5$。

输出格式

对于每个测试用例,输出一行,包含上述过程结束后 $a_i$ 的期望值,对 $10^9 + 7$ 取模的结果。

样例

样例输入 1

3
3 5
8 8 8
2 1
10 11
7 7
9 8 7 6 5 4 2

样例输出 1

8
500000014
857142869

说明

在第一个样例中,由于 $a$ 的所有元素初始相等,在 $k=5$ 次操作中的任何一次后,它们都不会改变。因此,最终数组为 $[8, 8, 8]$,所以最终数组中随机元素的期望值为 $8$。

在第二个样例中,操作中有 $50\%$ 的概率选择 $i=1$,有 $50\%$ 的概率选择 $i=2$。

  1. 如果选择了 $i=1$,数组中的所有元素都会向 $a_1 = 10$ 靠拢,因此 $a$ 将从 $[10, 11]$ 变为 $[10, 10]$。此时数组中随机元素的期望值为 $10$。
  2. 如果选择了 $i=2$,数组中的所有元素都会向 $a_2 = 11$ 靠拢,因此 $a$ 将从 $[10, 11]$ 变为 $[11, 11]$。此时数组中随机元素的期望值为 $11$。

因此,期望值有 $50\%$ 的概率为 $10$,有 $50\%$ 的概率为 $11$。最终的期望值为 $10.5 = \frac{21}{2}$,这在模 $10^9 + 7$ 下等价于 $500000014$。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.