【CodeForces - 1025G】Company Acquisitions

版权声明:本文为博主原创文章……懂吗?要尊重别人的劳动成果呐 https://blog.csdn.net/Tiw_Air_Op1721/article/details/83750688


@Description - English@

time limit per test: 1 second
memory limit per test: 256 megabytes

There are n 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.

Two distinct active startups A, B, are chosen uniformly at random.
A fair coin is flipped, and with equal probability, A acquires B or B acquires A (i.e. if A acquires B, then that means B’s state changes from active to acquired, and its starts following A).
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 A, B are active startups. C, D, E are acquired startups under A, and F, G are acquired startups under B:
题面图片1

If A acquires B, then the state will be A, F, G are active startups. C, D, E, B are acquired startups under A. F and G have no acquired startups:
题面图片2

If instead, B acquires A, then the state will be B, C, D, E are active startups. F, G, A are acquired startups under B. C, D, E have no acquired startups:题面图片3

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/Q, where P and Q are co-prime integers, and Q≠0(mod109+7). Return the value of P⋅Q−1 modulo 109+7.

Input
The first line contains a single integer n (2≤n≤500), the number of startups.
The next line will contain n space-separated integers a1,a2,…,an (ai=−1 or 1≤ai≤n). If ai=−1, then that means startup i is active. Otherwise, if 1≤ai≤n, then startup i is acquired, and it is currently following startup ai. It is guaranteed if a i a_i ≠−1, then a a i a_{a_i} =−1 (that is, all startups that are being followed are active).

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

Examples
input
3
-1 -1 -1
output
3

input
2
2 -1
output
0

@Description - Chinese@

有 n 个公司,每个公司要么是独立状态,要么是附属于某个公司的状态。

每一天都会发生一个事件:首先随机性地选中两个独立的公司A, B。相等的概率下,A 会吞并 B 或 B 会吞并 A。假设 A 吞并了 B,则 B 的状态变为附属于 A;且原先所有附属于 B 的公司会变为独立公司;反之亦然。

给出一开始所有公司的状态,-1 表示独立;否则给出它附属于哪个公司。
问最后只剩下一个独立公司的期望天数。

@Solution@

【巧妙,太巧妙了。膜拜出题人orz】

对于每一个独立公司 i i ,设附属于 i i 的公司个数为 x x ,定义其势函数 f ( i ) = 2 x 1 f(i) = 2^x - 1 (自己不附属于自己)。
对于每一个局面 S S ,定义势函数 F ( S ) = i i S f ( i ) F(S) = \sum_i^{i\in S} f(i) ,即所有公司势函数之和。

那么发生一个事件,势函数会怎么变化呢?

定义事件发生前的局面为 S S ,发生后的局面为 S S'
因为吞并事件等概率地发生在两个公司见,故不妨假设 A A B B 发生了吞并。
则:
F ( S ) = F ( S ) f ( A ) f ( B ) + 1 / 2 f ( A + 1 ) + 1 / 2 f ( B + 1 ) = F ( S ) 2 A + 1 2 B + 1 + 1 / 2 2 A + 1 1 / 2 + 1 / 2 2 ( B + 1 ) 1 / 2 = F ( S ) 2 A + 1 2 B + 1 + 2 A + 2 B 1 = F ( S ) + 1 F(S') = F(S)-f(A)-f(B)+1/2*f(A+1)+1/2*f(B+1)\\ =F(S)-2^A+1-2^B+1+1/2*2^{A+1}-1/2+1/2*2^(B+1)-1/2\\ =F(S)-2^A+1-2^B+1+2^A+2^B-1\\ =F(S) + 1

也就是说:不论怎样发生吞并,整个局面的势函数都会 + 1。
所以 a n s = F ( ) F ( ) ans = F(终止局面) - F(起始局面)
然后就很简单了。

【以我的数学能力真心无法严谨地证明 orz】

@Code@

我还是选择打表找规律好了qwq。

#include<cstdio>
const int MOD = int(1E9) + 7;
const int MAXN = 100000;
int pw[MAXN + 5], cnt[MAXN + 5];
void init() {
	pw[0] = 1;
	for(int i=1;i<=MAXN;i++)
		pw[i] = 2LL*pw[i-1]%MOD;
}
int main() {
	init(); int n, x;
	scanf("%d", &n);
	for(int i=1;i<=n;i++) {
		scanf("%d", &x);
		if( x != -1 ) cnt[x]++;
	}
	int ans = pw[n-1] - 1;
	for(int i=1;i<=n;i++)
		ans = (ans - 1LL*(pw[cnt[i]]-1)%MOD) % MOD;
	printf("%d\n", (ans + MOD)%MOD);
}

@END@

就是这样,新的一天里,也请多多关照哦(ノω<。)ノ))☆.。

猜你喜欢

转载自blog.csdn.net/Tiw_Air_Op1721/article/details/83750688
今日推荐