CF1025G.Company Acquisitions

NOI/NOI+/CTSC

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

There are nn startups. Startups can be active or acquired. If a startup is acquired, then that means it has exactly one active startup that it is following. An active startup can have arbitrarily many acquired startups that are following it. An active startup cannot follow any other startup.

The following steps happen until there is exactly one active startup. The following sequence of steps takes exactly 1 day.

  1. Two distinct active startups AA, BB, are chosen uniformly at random.
  2. A fair coin is flipped, and with equal probability, AA acquires BB or BB acquires AA (i.e. if AA acquires BB, then that means BB's state changes from active to acquired, and its starts following AA).
  3. When a startup changes from active to acquired, all of its previously acquired startups become active.

For example, the following scenario can happen: Let's say AA, BB are active startups. CC, DD, EE are acquired startups under AA, and FF, GG are acquired startups under BB:

Active startups are shown in red.

If AA acquires BB, then the state will be AA, FF, GG are active startups. CC, DD, EE, BB are acquired startups under AA. FF and GG have no acquired startups:

If instead, BB acquires AA, then the state will be BB, CC, DD, EE are active startups. FF, GG, AA are acquired startups under BB. CC, DD, EE have no acquired startups:

You are given the initial state of the startups. For each startup, you are told if it is either acquired or active. If it is acquired, you are also given the index of the active startup that it is following.

You're now wondering, what is the expected number of days needed for this process to finish with exactly one active startup at the end.

It can be shown the expected number of days can be written as a rational number P/QP/Q, where PP and QQ are co-prime integers, and Q≠0(mod109+7)Q \not= 0 \pmod{10^9+7}. Return the value of P⋅Q−1P \cdot Q^{-1} modulo 109+710^9+7.

共有 nn 家初创公司。每家初创公司处于“活跃”或“被收购”状态。若一家初创公司被收购,则它恰好正在追随一家活跃的初创公司。一家活跃的初创公司可以被任意数量的被收购初创公司所追随。而一家活跃的初创公司不能追随任何其他初创公司。

以下步骤将不断重复,直至仅剩一家活跃的初创公司为止。该步骤序列恰好耗时 1 天:

  1. 随机、均匀地选择两家不同的活跃初创公司 AA 和 BB;
  2. 抛一枚公平硬币:以相等的概率,要么 AA 收购 BB,要么 BB 收购 AA(例如,若 AA 收购 BB,则 BB 的状态由“活跃”变为“被收购”,并开始追随 AA);
  3. 当一家初创公司由“活跃”变为“被收购”时,其原先所有被收购的下属初创公司均变为“活跃”。

例如,如下情形可能发生:设 AA、BB 为活跃初创公司;CC、DD、EE 是追随 AA 的被收购初创公司;FF、GG 是追随 BB 的被收购初创公司:

图中活跃初创公司以红色显示。

若 AA 收购 BB,则新状态为:AA、FF、GG 为活跃初创公司;CC、DD、EE、BB 均为追随 AA 的被收购初创公司;FF 和 GG 没有下属被收购初创公司:

反之,若 BB 收购 AA,则新状态为:BB、CC、DD、EE 为活跃初创公司;FF、GG、AA 为追随 BB 的被收购初创公司;CC、DD、EE 没有下属被收购初创公司:

你已知所有初创公司的初始状态:对每家公司,你被告知其是“被收购”还是“活跃”;若为“被收购”,你还被告知它所追随的活跃初创公司的编号。

你现在想知道:该过程结束(即最终仅剩一家活跃初创公司)所需的期望天数是多少?

可以证明,该期望天数可表示为一个既约有理数 P/QP/Q,其中 PP 与 QQ 互质,且 Q≢0(mod109+7)Q \not\equiv 0 \pmod{10^9+7}。请返回 P⋅Q−1 mod (109+7)P \cdot Q^{-1} \bmod (10^9+7) 的值。

输入格式

The first line contains a single integer nn (2≤n≤5002 \leq n \leq 500), the number of startups.

The next line will contain nn space-separated integers a1,a2,…,ana_1, a_2, \ldots, a_n (ai=−1a_i = -1 or 1≤ai≤n1 \leq a_i \leq n). If ai=−1a_i = -1, then that means startup ii is active. Otherwise, if 1≤ai≤n1 \leq a_i \leq n, then startup ii is acquired, and it is currently following startup aia_i. It is guaranteed if ai≠−1a_i \not= -1, then aai=−1a_{a_i} =-1 (that is, all startups that are being followed are active).

第一行包含一个整数 nn(2≤n≤5002 \leq n \leq 500),表示初创公司的数量。

第二行包含 nn 个用空格分隔的整数 a1,a2,…,ana_1, a_2, \ldots, a_n(每个 aia_i 要么为 −1-1,要么满足 1≤ai≤n1 \leq a_i \leq n)。若 ai=−1a_i = -1,则表示第 ii 家初创公司处于活跃状态;否则,若 1≤ai≤n1 \leq a_i \leq n,则表示第 ii 家初创公司已被收购,且当前正追随第 aia_i 家初创公司。题目保证:若 ai≠−1a_i \neq -1,则必有 aai=−1a_{a_i} = -1(即所有被追随的初创公司均为活跃状态)。

输出格式

Print a single integer, the expected number of days needed for the process to end with exactly one active startup, modulo 109+710^9+7.

输出一个整数,表示该过程最终恰好剩下一个活跃创业公司所需的期望天数,对 109+710^9+7 取模。

输入输出样例

  • 输入#1

    3
    -1 -1 -1

    输出#1

    3
  • 输入#2

    2
    2 -1

    输出#2

    0
  • 输入#3

    40
    3 3 -1 -1 4 4 -1 -1 -1 -1 -1 10 10 10 10 10 10 4 20 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 3 3 3 3 3 3 3

    输出#3

    755808950

说明/提示

In the first sample, there are three active startups labeled 11, 22 and 33, and zero acquired startups. Here's an example of how one scenario can happen

  1. Startup 11 acquires startup 22 (This state can be represented by the array [−1,1,−1][-1, 1, -1])
  2. Startup 33 acquires startup 11 (This state can be represented by the array [3,−1,−1][3, -1, -1])
  3. Startup 22 acquires startup 33 (This state can be represented by the array [−1,−1,2][-1, -1, 2]).
  4. Startup 22 acquires startup 11 (This state can be represented by the array [2,−1,2][2, -1, 2]).

At this point, there is only one active startup, and this sequence of steps took 44 days. It can be shown the expected number of days is 33.

For the second sample, there is only one active startup, so we need zero days.

For the last sample, remember to take the answer modulo 109+710^9+7.

在第一个样例中,有三个活跃的初创公司,编号为 11、22 和 33,且没有被收购的初创公司。以下是一个可能的发生过程示例:

  1. 初创公司 11 收购初创公司 22(该状态可用数组 [−1,1,−1][-1, 1, -1] 表示);
  2. 初创公司 33 收购初创公司 11(该状态可用数组 [3,−1,−1][3, -1, -1] 表示);
  3. 初创公司 22 收购初创公司 33(该状态可用数组 [−1,−1,2][-1, -1, 2] 表示);
  4. 初创公司 22 收购初创公司 11(该状态可用数组 [2,−1,2][2, -1, 2] 表示)。

此时仅剩一家活跃的初创公司,该过程共耗时 44 天。可以证明,所需天数的期望值为 33。

在第二个样例中,仅有一家活跃的初创公司,因此需要 00 天。

在最后一个样例中,请记得将答案对 109+710^9+7 取模。

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

首页