首页 > 其他 > 详细

CF1025G Company Acquisitions 题解

时间:2020-05-15 15:48:22      阅读:58      评论:0      收藏:0      [点我收藏+]

很妙的一道题.

解决这一类题目, 可以考虑构造势能函数(Potential function), 使得每操作一次函数的期望增加值为 \(1\).

比如这道题可以构造这样一个势能函数, 对于每个点, 假设它的 followers 个数为 cnt, 那么它的值为 \(2^{cnt}-1\).
总的函数值就是每一个人的函数值的加和.

假设现在操作两个 followers 个数分别为 \(p,q\) 的人, 我们算出这个函数的期望变化值:
\(p\) follows \(q\): \((2^{q+1}-1)-(2^q-1)-(2^p-1)\).
\(q\) follows \(p\): \((2^{p+1}-1)-(2^p-1)-(2^q-1)\).

那么它们的期望变化即为它们的平均值:
\(\frac{(2^{q+1}-1)-(2^q-1)-(2^p-1)+(2^{p+1}-1)-(2^p-1)-(2^q-1)}{2}=1\).

即每操作一次, 这个值的期望变化为 1. 那么答案即为最终的函数值 - 初始函数值.

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;

inline int fsp(int x, int p = mod - 2) {
    int res = 1;
    while (p) {
        if (p & 1) res = 1ll * res * x % mod;
        x = 1ll * x * x % mod, p >>= 1;
    }
    return res;
}

int n, cnt[555];

int main() {
    cin >> n;
    for (int i = 1, x; i <= n; ++i) {
        scanf("%d", &x);
        if (~x) ++cnt[x];
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i)
        ans = (ans + fsp(2, cnt[i]) - 1) % mod;
    ans = (fsp(2, n - 1) - 1 - ans + mod) % mod;
    cout << ans << endl;
    return 0;
}

CF1025G Company Acquisitions 题解

原文:https://www.cnblogs.com/hnfms-jerry/p/solution_cf1025g.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!