A14496 | Finding Expected Value
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Mr. Chanek opened a letter from his fellow, who is currently studying at Singanesia. Here is what it says.
Define an array $b$ ( $0 \leq b_i < k$ ) with $n$ integers. While there exists a pair $(i, j)$ such that $b_i \ne b_j$ , do the following operation:
- Randomly pick a number $i$ satisfying $0 \leq i < n$ . Note that each number $i$ has a probability of $\frac{1}{n}$ to be picked.
- Randomly Pick a number $j$ satisfying $0 \leq j < k$ .
- Change the value of $b_i$ to $j$ . It is possible for $b_i$ to be changed to the same value.
Denote $f(b)$ as the expected number of operations done to $b$ until all elements of $b$ are equal.
You are given two integers $n$ and $k$ , and an array $a$ ( $-1 \leq a_i < k$ ) of $n$ integers.
For every index $i$ with $a_i = -1$ , replace $a_i$ with a random number $j$ satisfying $0 \leq j < k$ . Let $c$ be the number of occurrences of $-1$ in $a$ . There are $k^c$ possibilites of $a$ after the replacement, each with equal probability of being the final array.
Find the expected value of $f(a)$ modulo $10^9 + 7$ .
Formally, let $M = 10^9 + 7$ . It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$ , where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$ . Output the integer equal to $p \cdot q^{-1} \bmod M$ . In other words, output such an integer $x$ that $0 \le x < M$ and $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!
Define an array $b$ ( $0 \leq b_i < k$ ) with $n$ integers. While there exists a pair $(i, j)$ such that $b_i \ne b_j$ , do the following operation:
- Randomly pick a number $i$ satisfying $0 \leq i < n$ . Note that each number $i$ has a probability of $\frac{1}{n}$ to be picked.
- Randomly Pick a number $j$ satisfying $0 \leq j < k$ .
- Change the value of $b_i$ to $j$ . It is possible for $b_i$ to be changed to the same value.
Denote $f(b)$ as the expected number of operations done to $b$ until all elements of $b$ are equal.
You are given two integers $n$ and $k$ , and an array $a$ ( $-1 \leq a_i < k$ ) of $n$ integers.
For every index $i$ with $a_i = -1$ , replace $a_i$ with a random number $j$ satisfying $0 \leq j < k$ . Let $c$ be the number of occurrences of $-1$ in $a$ . There are $k^c$ possibilites of $a$ after the replacement, each with equal probability of being the final array.
Find the expected value of $f(a)$ modulo $10^9 + 7$ .
Formally, let $M = 10^9 + 7$ . It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$ , where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$ . Output the integer equal to $p \cdot q^{-1} \bmod M$ . In other words, output such an integer $x$ that $0 \le x < M$ and $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!
输入格式
The first line contains two integers $n$ and $k$ ( $2 \leq n \leq 10^5$ , $2 \leq k \leq 10^9$ ).
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $-1 \leq a_i < k$ ).
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $-1 \leq a_i < k$ ).
输出格式
Output an integer denoting the expected value of $f(a)$ modulo $10^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
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted