codeforces 1187F. Expected Square Beauty

求$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

codeforces 1187F. Expected Square Beauty的相关文章

CF1187F Expected Square Beauty

Expected Square Beauty 有一个长度为 n 的数列,第 i 个数的取值范围为 \([l_i,r_i]\) ,定义一个数列的价值为这个数列极长连续相同段的个数,求一个数列价值的平方期望,对 \(10^9+7\) 取模 . n≤200000 . 题解 https://codeforces.com/blog/entry/68111 As usual with tasks on an expected value, let's denote \(I_i(x)\) as indicat

Codeforces 828B Black Square(简单题)

Codeforces 828B Black Square(简单题) Description Polycarp has a checkered sheet of paper of size n?×?m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum pos

CodeForces 1A Theatre Square

A - Theatre Square Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 1A Description Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the

(dp)CodeForces - 300D Painting Square

Vasily the bear has got a large square white table of n rows and n columns. The table has got a black border around this table.  The example of the initial table at n = 5. Vasily the bear wants to paint his square table in exactly k moves. Each move

CodeForces Beta Round #1

Codeforces Beta Round #1 A. Theatre Square [题意]一个n*m的矩形广场,用a*a的方形石板铺设,问最少需要多少块石板能铺满广场. [思路]水题,从n方向来看能能够铺设ceil(n/a)块,从m方向来看能能够铺设ceil(m/a)块,总共有ceil(n/a)*ceil(m/a)块. 1 /* 2 ** CodeForces 1A Theatre Square 3 ** Created by Rayn @@ 2014/05/18 4 */ 5 #inclu

PRML 2: Regression Models

1. Linear Regression Model The Least Square Method is derived from maximum likelihood under the assumption of a Gaussian noise distribution, and hence could give rise to the problem of over-fitting, which is a general property of MLE. The Regularized

1.5 Scipy:高级科学计算

医药统计项目可联系 QQ:231469242 http://www.kancloud.cn/wizardforcel/scipy-lecture-notes/129867 作者:Adrien Chauve, Andre Espaze, Emmanuelle Gouillart, Ga?l Varoquaux, Ralf Gommers Scipy scipy包包含许多专注于科学计算中的常见问题的工具箱.它的子模块对应于不同的应用,比如插值.积分.优化.图像处理.统计和特殊功能等. scipy可以

python之scipy模块

一  简单介绍 SciPy是基于NumPy开发的高级模块,它提供了许多数学算法和函数的实现,用于解决科学计算中的一些标准问题.例如数值积分和微分方程求解,扩展的矩阵计算,最优化,概率分布和统计函数,甚至包括信号处理等. 作为标准科学计算程序库,SciPy类似于Matlab的工具箱,它是Python科学计算程序的核心包,它用于有效地计算NumPy矩阵,与NumPy矩阵协同工作. SciPy库由一些特定功能的子模块构成,如下表所示: 模块 功能 cluster 矢量量化 / K-均值 constan

Codeforces Round #249 (Div. 2) A. Black Square

水题 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ vector<int> a(4); cin >> a[0] >> a[1]>>a[2]>>a[3]; string str; cin >> str; int res = 0; for(int i = 0 ; i