Editorial for Expected Fixed Points


Approach

Let IiI_i be the indicator that person ii receives gift ii. The total value is

X=∑i=1nwiIiX = \sum_{i=1}^n w_i I_i.

By linearity of expectation,

E[X]=∑i=1nwiE[Ii]=∑i=1nwiPr⁡(Ii=1)E[X] = \sum_{i=1}^n w_i E[I_i] = \sum_{i=1}^n w_i \Pr(I_i = 1).

In a uniformly random permutation, person ii is equally likely to receive any of the nn gifts, so Pr⁡(Ii=1)=1/n\Pr(I_i = 1) = 1/n. Therefore

E[X]=1n∑i=1nwiE[X] = \frac{1}{n} \sum_{i=1}^n w_i.

The events IiI_i are dependent (exactly one person can receive gift 11, for example), but linearity does not care. Output the fraction modulo 109+710^9 + 7.

This is O(n)O(n).

Solution (C++)

Code 1
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1000000007;

long long modpow(long long a, long long e) {
    long long r = 1;
    a %= MOD;
    while (e) {
        if (e & 1) {
            r = r * a % MOD;
        }
        a = a * a % MOD;
        e >>= 1;
    }
    return r;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    long long sum = 0;
    for (int i = 0; i < n; i++) {
        long long w;
        cin >> w;
        sum += w;
        if (sum >= MOD) {
            sum %= MOD;
        }
    }
    sum %= MOD;
    cout << sum * modpow(n, MOD - 2) % MOD << "\n";
}

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.