【CodeForces - 1025G】Company Acquisitions
@Company [email protected]
@Description - [email protected]
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:
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:
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:
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
≠−1, then
=−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 - [email protected]
有 n 個公司,每個公司要麼是獨立狀態,要麼是附屬於某個公司的狀態。
每一天都會發生一個事件:首先隨機性地選中兩個獨立的公司A, B。相等的概率下,A 會吞併 B 或 B 會吞併 A。假設 A 吞併了 B,則 B 的狀態變為附屬於 A;且原先所有附屬於 B 的公司會變為獨立公司;反之亦然。
給出一開始所有公司的狀態,-1 表示獨立;否則給出它附屬於哪個公司。
問最後只剩下一個獨立公司的期望天數。
@[email protected]
【巧妙,太巧妙了。膜拜出題人orz】
對於每一個獨立公司
,設附屬於
的公司個數為
,定義其勢函式
(自己不附屬於自己)。
對於每一個局面
,定義勢函式
,即所有公司勢函式之和。
那麼發生一個事件,勢函式會怎麼變化呢?
定義事件發生前的局面為
,發生後的局面為
。
因為吞併事件等概率地發生在兩個公司見,故不妨假設
與
發生了吞併。
則:
也就是說:不論怎樣發生吞併,整個局面的勢函式都會 + 1。
所以
。
然後就很簡單了。
【以我的數學能力真心無法嚴謹地證明 orz】
@[email protected]
我還是選擇打表找規律好了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);
}
@[email protected]
就是這樣,新的一天裡,也請多多關照哦(ノω<。)ノ))☆.。