当前位置: 代码迷 >> 综合 >> [Educational Codeforces Round 67]F.Expected Square Beauty(平方期望)
  详细解决方案

[Educational Codeforces Round 67]F.Expected Square Beauty(平方期望)

热度:56   发布时间:2023-10-22 21:52:50.0

[Educational——手把手教你如何计数系列·第一期]

题面

原题面

Let xxx be an array of integers x=[x1,x2,…,xn]x=[x_1,x_2,…,x_n]x=[x1?,x2?,,xn?]. Let’s define B(x)B(x)B(x) as a minimal size of a partition of xxx into subsegments such that all elements in each subsegment are equal. For example, B([3,3,6,1,6,6,6])=4B([3,3,6,1,6,6,6])=4B([3,3,6,1,6,6,6])=4 using next partition:[3,3∣6∣1∣6,6,6][3,3 | 6 | 1 | 6,6,6][3,3616,6,6].
Now you don’t have any exact values of xxx, but you know that xix_ixi? can be any integer value from [li,ri](li≤ri)[l_i,r_i](l_i≤r_i)[li?,ri?](li?ri?) uniformly at random. All xix_ixi? are independent.
Calculate expected value of (B(x))2(B(x))^2(B(x))2, or E((B(x))2)E((B(x))^2)E((B(x))2). It’s guaranteed that the expected value can be represented as rational fraction PQ\frac{P}{Q}QP? where (P,Q)=1(P,Q)=1(P,Q)=1, so print the value P?Q?1 mod 109+7P?Q^{?1}\bmod 10^9+7P?Q?1mod109+7.

Input

The first line contains the single integer nnn (1≤n≤2?1051≤n≤2?10^51n2?105) — the size of the array xxx.
The second line contains nnn integers l1,l2,…,lnl_1,l_2,…,l_nl1?,l2?,,ln?(1≤li≤1091≤l_i≤10^91li?109).
The third line contains nnn integers r1,r2,…,rnr_1,r_2,…,r_nr1?,r2?,,rn? (li≤ri≤109l_i≤r_i≤10^9li?ri?109).

Output

Print the single integer — E((B(x))2)E((B(x))^2)E((B(x))2) as P?Q?1 mod 109+7P?Q^{?1}\bmod 10^9+7P?Q?1mod109+7.

翻译

有一个长度为nnn的数列{xi}\{x_i\}{ xi?},定义B(x)B(x)B(x)为将xxx划分为所有元素都相等的子段的最小子段数。对于1≤i≤n1≤i≤n1inxix_ixi?将等概率为[li,ri][l_i,r_i][li?,ri?]中的一个整数。给定li,ril_i,r_ili?,ri?,求(B(x))2(B(x))^2(B(x))2的期望,即E((B(x))2)E((B(x))^2)E((B(x))2),对109+710^9+7109+7取模。
来自:洛谷

题解

神奇的期望题。
设事件I(i)I(i)I(i)iii,i?1i-1i?1两个数相同这一个事件的发生或者不发生。
容易发现B(x)=∑i=1nI(i)B(x)=\sum^{n}_{i=1} I(i)B(x)=i=1n?I(i),其中I(1)I(1)I(1)默认是一定发生的。
注意这里的B(x),I(i)B(x),I(i)B(x),I(i)都是事件
然后同时平方(B(x))2=(∑i=1nI(i))2(B(x))^2=(\sum^{n}_{i=1} I(i))^2(B(x))2=(i=1n?I(i))2
由期望性质得:E((B(x))2)=E((∑i=1nI(i))2)E((B(x))^2)=E((\sum^{n}_{i=1} I(i))^2)E((B(x))2)=E((i=1n?I(i))2)
现在,我们就转化为求E((∑i=1nI(i))2)E((\sum^{n}_{i=1} I(i))^2)E((i=1n?I(i))2)了。
接下来展开一下E((∑i=1nI(i))2)=E(∑i=1n∑j=1nI(i)I(j))E((\sum^{n}_{i=1} I(i))^2)=E(\sum^{n}_{i=1}\sum^{n}_{j=1}I(i)I(j))E((i=1n?I(i))2)=E(i=1n?j=1n?I(i)I(j))
注意这里乘法的意思是两个事件同时发生。
首先显然有I(i)I(i)=I(i)I(i)I(i)=I(i)I(i)I(i)=I(i),然后我们可以将两个无关的事件直接分开,显然对于I(i),I(j)(∣i?j∣>1)I(i),I(j)(|i-j|>1)I(i),I(j)(i?j>1)两个事件是无关的,因此我们可以把式子分为:
E(∑i=1n∑j=1nI(i)I(j))=E(∑i=1n(I(i)+I(i)∑∣i?j∣>1I(j)+I(i?1)I(i)+I(i)I(i+1)))=∑i=1n(E(I(i))+E(I(i))∑∣i?j∣>1E(I(j))+E(I(i?1)I(i))+E(I(i)I(i+1)))E(\sum^{n}_{i=1}\sum^{n}_{j=1}I(i)I(j))=E(\sum^{n}_{i=1}(I(i)+I(i)\sum_{|i-j|>1}I(j)+I(i-1)I(i)+I(i)I(i+1)))=\sum^{n}_{i=1}(E(I(i))+E(I(i))\sum_{|i-j|>1}E(I(j))+E(I(i-1)I(i))+E(I(i)I(i+1)))E(i=1n?j=1n?I(i)I(j))=E(i=1n?(I(i)+I(i)i?j>1?I(j)+I(i?1)I(i)+I(i)I(i+1)))=i=1n?(E(I(i))+E(I(i))i?j>1?E(I(j))+E(I(i?1)I(i))+E(I(i)I(i+1)))
现在考虑计算E(I(i))E(I(i))E(I(i))。(注意接下来的等号都仅仅是数值上的相等)
由于这是一个0/1事件那么就有E(I(i))=P(I(i))E(I(i))=P(I(i))E(I(i))=P(I(i))也就是分别在[li?1,ri?1],[li,ri][l_{i-1},r_{i-1}],[l_i,r_i][li?1?,ri?1?],[li?,ri?]随意取一个数不相等的概率,显然就是1?min(ri?1,ri)?max(li?1,li)+1(ri?1?li?1+1)(ri?li+1)1-\frac{min(r_{i-1},r_i)-max(l_{i-1},l_i)+1}{(r_{i-1}-l_{i-1}+1)(r_i-l_i+1)}1?(ri?1??li?1?+1)(ri??li?+1)min(ri?1?,ri?)?max(li?1?,li?)+1?,首先满足两区间相交,记为pip_ipi?
然后是E(I(i)I(i+1))E(I(i)I(i+1))E(I(i)I(i+1)),同理有E(I(i)I(i+1))=P(I(i)I(i+1))E(I(i)I(i+1))=P(I(i)I(i+1))E(I(i)I(i+1))=P(I(i)I(i+1)),可以认为是在[li?1,ri?1],[li,ri],[li+1,ri+1][l_{i-1},r_{i-1}],[l_i,r_i],[l_{i+1},r_{i+1}][li?1?,ri?1?],[li?,ri?],[li+1?,ri+1?]随意取一个数都相等的概率,为min(ri?1,ri,ri+1)?max(li?1,li,li+1)+1(ri?1?li?1+1)(ri?li+1)(ri+1?li+1+1)\frac{min(r_{i-1},r_i,r_{i+1})-max(l_{i-1},l_i,l_{i+1})+1}{(r_{i-1}-l_{i-1}+1)(r_i-l_i+1)(r_{i+1}-l_{i+1}+1)}(ri?1??li?1?+1)(ri??li?+1)(ri+1??li+1?+1)min(ri?1?,ri?,ri+1?)?max(li?1?,li?,li+1?)+1?,然后互不相同的就容斥一下变为1?pi+1?pi+min(ri?1,ri,ri+1)?max(li?1,li,li+1)+1(ri?1?li?1+1)(ri?li+1)(ri+1?li+1+1)1-p_{i+1}-p_i+\frac{min(r_{i-1},r_i,r_{i+1})-max(l_{i-1},l_i,l_{i+1})+1}{(r_{i-1}-l_{i-1}+1)(r_i-l_i+1)(r_{i+1}-l_{i+1}+1)}1?pi+1??pi?+(ri?1??li?1?+1)(ri??li?+1)(ri+1??li+1?+1)min(ri?1?,ri?,ri+1?)?max(li?1?,li?,li+1?)+1?(满足三区间相交),记为val(i)val(i)val(i)
那么就有E((B(x))2)=∑i=1n(pi+pi?∑∣i?j∣>1pj+val(i?1)+val(i))E((B(x))^2)=\sum^{n}_{i=1}(p_i+p_i*\sum_{|i-j|>1}p_j+val(i-1)+val(i))E((B(x))2)=i=1n?(pi?+pi??i?j>1?pj?+val(i?1)+val(i))
依次计算即可。注意各种各样奇怪的细节。
复杂度:O(nlog? mod )O(n\log \bmod )O(nlogmod)

实现

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
using namespace std;
#define MAXN 200000
#define MOD 1000000007
#define LL long long
int n,l[MAXN+5],r[MAXN+5];
int p[MAXN+5],q[MAXN+5],sum;
LL add(LL a,LL b){
    return (a+b)%MOD;}
LL mul(LL a,LL b){
    return 1LL*a*b%MOD;}
LL dec(LL a,LL b){
    return (a-b+MOD)%MOD;}
int fst_pow(int a,int b){
    int res=1;while(b){
    if(b&1)res=mul(res,a);a=mul(a,a);b>>=1;}return res;
}
int val(int i){
    int ans=1;ans=dec(ans,p[i]),ans=dec(ans,p[i+1]);int L=max(l[i-1],max(l[i],l[i+1])),R=min(r[i-1],min(r[i],r[i+1]));if(L<R)ans=add(ans,mul(R-L,fst_pow(mul(mul(r[i]-l[i],r[i-1]-l[i-1]),r[i+1]-l[i+1]),MOD-2)));return ans;
}
int main()
{
    scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&l[i]);for(int i=1;i<=n;i++){
    scanf("%d",&r[i]);r[i]++;}int sum=0;for(int i=1;i<=n;i++){
    int L=max(l[i],l[i-1]),R=min(r[i],r[i-1]);if(L<R)p[i]=mul(R-L,fst_pow(mul(r[i]-l[i],r[i-1]-l[i-1]),MOD-2));q[i]=dec(1,p[i]);sum=add(sum,q[i]);}//i==1 默认值int ans=0;for(int i=1;i<=n;i++){
    ans=add(ans,q[i]);int ps=dec(sum,q[i]);if(i>2)ans=add(ans,val(i-1)),ps=dec(ps,q[i-1]);if(i<n)ans=add(ans,val(i)),ps=dec(ps,q[i+1]);ans=add(ans,mul(q[i],ps));//printf("%d\n",ans);}printf("%d",ans);
}
  相关解决方案