2019 Hang electric multi-school seventh HDU - 6656 Kejin Player-- probability && expectations

The meaning of problems

A total of $ n $ floors, the first $ i cost of spending $ a_i $ of $ layer, there is the probability of $ pi $ to $ i + 1 $ layer, or to $ x_i $ ($ x_i \ leq 1 $) layer. Then there are times $ Q $ query, $ L $ query each layer to the desired cost of $ j $ layer.

analysis

Such additive may be desirable to have, therefore, maintains a prefix and $ sum [i] $: $ from $ 1 to $ I $ desired.

Provided from $ I $ $ i + 1 $ the expected price for $ E $, there

$E = a_i + (1-\frac{r_i}{s_i})(sum[i]-sum[x_i]+E)$

解得 $E = [s_ia_i + (s_i-r_i)(sum[i]-sum[x_i])]/r_i$

 

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

typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 500000 + 10;
ll sum[maxn];
int n, q;

ll qpow(ll a, ll b)
{
    ll ret = 1;
    while(b)
    {
        if(b&1)  ret = ret * a % mod;
        a = a *a % mod;
        b >>= 1;
    }
    return ret;
}
ll inv(ll x)
{
    return qpow(x, mod-2);
}

//第i层到第i+1层的期望
void getE(ll i, ll ri, ll si, ll ai, ll xi)
{
    ll e = si * ai % mod;
    e = (e + (si - ri) * (sum[i] - sum[xi]) % mod);
    e = e * inv(ri) % mod;
    e = (e + mod) % mod;
    sum[i+1] = (sum[i] + e) % mod;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &q);
        for(int i = 1;i <= n;i++)
        {
            ll ri, si, xi, ai;
            scanf("%lld%lld%lld%lld", &ri, &si, &xi, &ai);
            getE(i, ri, si, ai, xi);
        }

        for(int i = 0;i < q;i++)
        {
            int l, r;
            scanf("%d%d", &l, &r);
            printf("%lld\n", (sum[r] - sum[l] + mod) % mod);
        }
    }
}

 

 

Reference Links: https://blog.csdn.net/mmk27_word/article/details/99472953

Guess you like

Origin www.cnblogs.com/lfri/p/11360441.html