求$E({B(x)}^2)$,考虑$B(x)$为每一位与前一位不同的期望次数
令$A(x)$表示第$x$位与第$x-1$位不同的概率,特别地,$A(1)=1$
$$E({B(x)}^2)=E({(\sum_{i=1}^n A(i))}^2)$$
把式子展开得,
$$E({B(x)}^2)=\sum_{i=1}^n \sum_{j=1}^n E(A(i)\times A(j))$$
显然如果$|i-j|>1$,$A(i)$与$A(j)$是独立的,$E(A(i)\times A(j))=E(A(i))\times E(A(j))$
$A(x)$可以在$O(1)$的时间内算出来,$|i-j|>1$的情况也可以快速计算出来。
若$|i-j|=0$,$E({A(i)}^2)=E(A(i))$。
剩下的我们就要统计$a_{i-1}\neq a_{i}且a_{i}\neq a_{i+1}$这样的概率。
可以用容斥$O(1)$计算出来。
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 #define MOD 1000000007 5 int l[200010], r[200010]; 6 int P[200010], sum[200010]; 7 inline int Power(int x, int y) { 8 int ret = 1; 9 while(y) { 10 if(y & 1) ret = 1ll * ret * x % MOD; 11 x = 1ll * x * x % MOD; y >>= 1; 12 } 13 return ret; 14 } 15 inline int min(int a, int b, int c) { 16 return min(a, min(b, c)); 17 } 18 inline int max(int a, int b, int c) { 19 return max(a, max(b, c)); 20 } 21 inline int calc(int l, int r) { 22 if(l > r) return 0; 23 int ret = sum[r] - sum[l - 1] + MOD; 24 if(ret >= MOD) ret -= MOD; 25 return ret; 26 } 27 inline void Add(int &x, int y) { 28 x += y; 29 if(x >= MOD) x -= MOD; 30 } 31 int main(){ 32 int n; 33 scanf("%d", &n); 34 for(int i = 1; i <= n; ++ i) { 35 scanf("%d", &l[i]); 36 -- l[i]; 37 } 38 for(int i = 1; i <= n; ++ i) { 39 scanf("%d", &r[i]); 40 } 41 P[1] = 1; 42 for(int i = 2; i <= n; ++ i) { 43 int B = 1ll * (r[i - 1] - l[i - 1]) * (r[i] - l[i]) % MOD; 44 int A = min(r[i - 1], r[i]) - max(l[i - 1], l[i]); 45 if(A < 0) A = 0; 46 P[i] = 1ll * (B - A + MOD) * Power(B, MOD - 2) % MOD; 47 } 48 for(int i = 1; i <= n; ++ i) { 49 sum[i] = sum[i - 1] + P[i]; 50 if(sum[i] >= MOD) sum[i] -= MOD; 51 } 52 int Ans = 0; 53 for(int i = 1; i <= n; ++ i) { 54 Add(Ans, P[i]); 55 Add(Ans, 1ll * P[i] * calc(1, i - 2) % MOD); 56 Add(Ans, 1ll * P[i] * calc(i + 2, n) % MOD); 57 } 58 for(int i = 2; i <= n; ++ i) { 59 if(i == 2) { 60 Add(Ans, 2 * P[i] % MOD); 61 continue; 62 } 63 int E = 1; 64 E -= (1 - P[i] + MOD); 65 if(E < 0) E += MOD; 66 E -= (1 - P[i - 1] + MOD); 67 if(E < 0) E += MOD; 68 int B = 1ll * (r[i - 2] - l[i - 2]) * (r[i - 1] - l[i - 1]) % MOD * (r[i] - l[i]) % MOD; 69 int A = min(r[i - 2], r[i - 1], r[i]) - max(l[i - 2], l[i - 1], l[i]); 70 if(A < 0) A = 0; 71 E += 1ll * A * Power(B, MOD - 2) % MOD; 72 if(E >= MOD) E -= MOD; 73 Add(Ans, E * 2 % MOD); 74 } 75 printf("%d\n", Ans); 76 }
原文地址:https://www.cnblogs.com/iamqzh233/p/11137506.html
时间: 2024-10-16 18:53:21