CF1575F.Finding Expected Value

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Mr. Chanek opened a letter from his fellow, who is currently studying at Singanesia. Here is what it says.

Define an array bb (0≤bi<k0 \leq b_i \lt k) with nn integers. While there exists a pair (i,j)(i, j) such that bi≠bjb_i \ne b_j, do the following operation:

  • Randomly pick a number ii satisfying 0≤i<n0 \leq i \lt n. Note that each number ii has a probability of 1n\frac{1}{n} to be picked.
  • Randomly Pick a number jj satisfying 0≤j<k0 \leq j \lt k.
  • Change the value of bib_i to jj. It is possible for bib_i to be changed to the same value.

Denote f(b)f(b) as the expected number of operations done to bb until all elements of bb are equal.

You are given two integers nn and kk, and an array aa (−1≤ai<k-1 \leq a_i \lt k) of nn integers.

For every index ii with ai=−1a_i = -1, replace aia_i with a random number jj satisfying 0≤j<k0 \leq j \lt k. Let cc be the number of occurrences of −1-1 in aa. There are kck^c possibilites of aa after the replacement, each with equal probability of being the final array.

Find the expected value of f(a)f(a) modulo 109+710^9 + 7.

Formally, let M=109+7M = 10^9 + 7. It can be shown that the answer can be expressed as an irreducible fraction pq\frac{p}{q}, where pp and qq are integers and q≢0(modM)q \not \equiv 0 \pmod{M}. Output the integer equal to p⋅q−1 mod Mp \cdot q^{-1} \bmod M. In other words, output such an integer xx that 0≤x<M0 \le x \lt M and x⋅q≡p(modM)x \cdot q \equiv p \pmod{M}.

After reading the letter, Mr. Chanek gave the task to you. Solve it for the sake of their friendship!

陈克先生收到了一封来自他朋友的信,这位朋友目前正在新迦尼西亚学习。信的内容如下:

定义一个长度为 nn 的数组 bb(满足 0≤bi<k0 \leq b_i < k)。当数组中存在一对下标 (i,j)(i, j) 使得 bi≠bjb_i \ne b_j 时,重复执行以下操作:

  • 随机选取一个下标 ii,满足 0≤i<n0 \leq i < n。注意:每个下标 ii 被选中的概率均为 1n\frac{1}{n};
  • 随机选取一个整数 jj,满足 0≤j<k0 \leq j < k;
  • 将 bib_i 的值修改为 jj。注意:bib_i 可能被修改为它原来的值。

记 f(b)f(b) 为将数组 bb 所有元素变为相等所需的操作次数的期望值。

现给定两个整数 nn 和 kk,以及一个长度为 nn 的数组 aa(满足 −1≤ai<k-1 \leq a_i < k)。

对每个满足 ai=−1a_i = -1 的下标 ii,将其替换为一个在 [0,k)[0, k) 范围内均匀随机选取的整数 jj。设 cc 为数组 aa 中 −1-1 的出现次数,则替换后共有 kck^c 种可能的数组 aa,每种结果出现的概率均等。

求 f(a)f(a) 的期望值对 109+710^9 + 7 取模的结果。

形式化地,令 M=109+7M = 10^9 + 7。可以证明该答案可表示为最简分数 pq\frac{p}{q},其中 pp 和 qq 均为整数,且 q≢0(modM)q \not\equiv 0 \pmod{M}。请输出整数 p⋅q−1 mod Mp \cdot q^{-1} \bmod M。换言之,输出满足 0≤x<M0 \le x < M 且 x⋅q≡p(modM)x \cdot q \equiv p \pmod{M} 的唯一整数 xx。

读完这封信后,陈克先生把这个任务交给了你。为了维护他们之间的友谊,请解决这个问题!

输入格式

The first line contains two integers nn and kk (2≤n≤1052 \leq n \leq 10^5, 2≤k≤1092 \leq k \leq 10^9).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (−1≤ai<k-1 \leq a_i \lt k).

第一行包含两个整数 nn 和 kk(2≤n≤1052 \leq n \leq 10^5,2≤k≤1092 \leq k \leq 10^9)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(−1≤ai<k-1 \leq a_i \lt k)。

输出格式

Output an integer denoting the expected value of f(a)f(a) modulo 109+710^9 + 7.

输出一个整数,表示 f(a)f(a) 的期望值对 109+710^9 + 7 取模的结果。

输入输出样例

  • 输入#1

    2 2
    0 1

    输出#1

    2
  • 输入#2

    2 2
    0 -1

    输出#2

    1
  • 输入#3

    3 3
    0 1 1

    输出#3

    12
  • 输入#4

    3 3
    -1 -1 -1

    输出#4

    11
  • 输入#5

    10 9
    -1 0 -1 1 1 2 2 3 3 3

    输出#5

    652419213

输入解题思路,AI测评打分。不知道怎么写?

首页