题目

CF1187F Expected Square Beauty

做法

\(B(x)=\sum\limits_{i=1}^n I_i(x),I_i(x)=\begin{cases}1&x_i≠x_{i-1}\\0&x_i=x_{i-1}\end{cases}\)

\(E(B(x)^2)=E(\sum\limits_{i=1}^n I_i(x)\sum\limits_{j=1}^n I_j(x))=E(\sum\limits_{i=1}^n\sum\limits_{j=1}^n I_i(x)I_j(x))=\sum\limits_{i=1}^n\sum\limits_{j=1}^n E(I_i(x)I_j(x))\)

分类讨论一下\(E(I_i(x)I_j(x))\)

  • \(|i-j|>1\),这两个互不影响,则\(E(I_i(x)I_j(x))=E(l_i(x))E(l_j(x))\)

  • \(i=j\),因为\(l(x)\)函数仅为\(1\)和\(0\),故\(E(I_i(x)I_j(x))=E(l_i(x))\)

  • \(|i-j|=1\)详细讨论一下:

    \(q_i=P(x_{i-1}=x_i)=E(x_{i-1}=x_i)=max(0,\frac{min(r_{i-1},r_i)-max(l_{i-1},l_i)}{(r_{i-1}-l_{i-1})(r_i-l_i)})\)

    \(E(I_i(x))=1-q_i\)

    则\(E(I_i(x)I_{i+1}(x))=E(x_{i-1}≠x_i\And x_i≠x_{i+1})\)

    故等于\(1-q_i-q_{i+1}+E(x_{i-1}=x_i\And x_i=x_{i+1})\)

    其中\(E(x_{i-1}=x_i\And x_i=x_{i+1})=\frac{min(r_{i-1},r_i,r_{i+1})-max(l_{i-1},l_i,l_{i+1})}{(r_{i-1}-l_{i-1})(r_i-l_i)(r_{i+1}-l_{i+1})})\)

可以用\(O(n)\)算出来

Code

#include<bits/stdc++.h>
typedef int LL;
const LL maxn=1e6+9,mod=1e9+7;
inline LL Read(){
LL x(0),f(1); char c=getchar();
while(c<'0' || c>'9'){
if(c=='-') f=-1; c=getchar();
}
while(c>='0' && c<='9'){
x=(x<<3ll)+(x<<1ll)+c-'0'; c=getchar();
}return x*f;
}
LL n,ans,sum;
LL l[maxn],r[maxn],q[maxn],E[maxn];
inline LL Pow(LL base,LL b){
LL ret(1);
while(b){
if(b&1) ret=1ll*ret*base%mod; base=1ll*base*base%mod; b>>=1;
}return ret;
}
inline LL P(LL x,LL y,LL z){
LL L(std::max(l[x],std::max(l[y],l[z]))),R(std::min(r[x],std::min(r[y],r[z])));
return std::max(0ll,1ll*(R-L)*Pow(1ll*(r[x]-l[x])*(r[y]-l[y])%mod*(r[z]-l[z])%mod,mod-2)%mod);
}
inline LL Calc(LL x){
LL y(x+1),ret(0);
if(x>1) ret=P(x-1,x,y);
return ((1ll-1ll*(q[x]+q[y])%mod+mod)%mod+ret)%mod;
}
int main(){
n=Read();
for(LL i=1;i<=n;++i){
l[i]=Read();
}
for(LL i=1;i<=n;++i){
r[i]=Read()+1;
LL R(std::min(r[i],r[i-1])),L(std::max(l[i],l[i-1]));
q[i]=std::max(0ll,1ll*(R-L)*Pow(1ll*(r[i-1]-l[i-1])*(r[i]-l[i])%mod,mod-2)%mod);
E[i]=1ll*(1-q[i]+mod)%mod;
sum=1ll*(sum+E[i])%mod;
}
for(LL i=1;i<=n;++i){
LL tmp(sum);
for(LL j=std::max(1,i-1);j<=std::min(n,i+1);++j)
tmp=1ll*(tmp-E[j]+mod)%mod;
ans=1ll*(ans+1ll*E[i]*tmp%mod)%mod;
if(i>1) ans=1ll*(ans+Calc(i-1))%mod;
if(i<n) ans=1ll*(ans+Calc(i))%mod;
ans=1ll*(ans+E[i])%mod;
}
printf("%d\n",ans);
return 0;
}

CF1187F Expected Square Beauty(期望)的更多相关文章

  1. Codeforces 1187 F - Expected Square Beauty

    F - Expected Square Beauty 思路:https://codeforces.com/blog/entry/68111 代码: #pragma GCC optimize(2) #p ...

  2. @codeforces - 1187F@ Expected Square Beauty

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个序列 x = {x1, x2, ..., xn},已知 ...

  3. 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)

    A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...

  4. Educational Codeforces Round 67

    Educational Codeforces Round 67 CF1187B Letters Shop 二分 https://codeforces.com/contest/1187/submissi ...

  5. CF-diary

    (做题方式:瞟题解然后码) 1238E. Keyboard Purchase \(\texttt{Difficulty:2200}\) 题意 给你一个长度为 \(n\) 的由前 \(m\) 个小写字母 ...

  6. NLP&数据挖掘基础知识

    Basis(基础): SSE(Sum of Squared Error, 平方误差和) SAE(Sum of Absolute Error, 绝对误差和) SRE(Sum of Relative Er ...

  7. 常用的机器学习&数据挖掘知识点【转】

    转自: [基础]常用的机器学习&数据挖掘知识点 Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Le ...

  8. 【基础】常用的机器学习&数据挖掘知识点

    Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Least Square Methods 最小二乘法),ML ...

  9. 概率分布之间的距离度量以及python实现(三)

    概率分布之间的距离,顾名思义,度量两组样本分布之间的距离 . 1.卡方检验 统计学上的χ2统计量,由于它最初是由英国统计学家Karl Pearson在1900年首次提出的,因此也称之为Pearson ...

随机推荐

  1. 【洛谷 P2051】 [AHOI2009]中国象棋(DP)

    题目链接 首先想到状压dp,但是\(n,m\)高达100,怎么压? 容易发现,每行每列最多两个象棋,否则就直接gg了. 一个巧妙的设置状态的方式是,只需要记录到当前行有多少列是放了1个炮和2个炮. 然 ...

  2. docker第一章--介绍和安装

  3. 3.建造模式(Builder)

    注:图片来源于 https://www.cnblogs.com/-saligia-/p/10216752.html 建造模式UML图解析: 代码: Director.h // // Created b ...

  4. HTML学习摘要5

    DAY 5 HTML <address> 元素定义文档或文章的联系信息(作者/拥有者). 此元素通常以斜体显示.大多数浏览器会在此元素前后添加折行. HTML <cite> 元 ...

  5. org.apache.commons.beanutils.ConversionException: No value specified解决办法

    转自:https://www.cnblogs.com/linjiqin/archive/2011/07/21/2112628.html 当用到了java.sql.Date时间等非内置对象时,如果对象为 ...

  6. c# zip写comment注释

    //生成的压缩文件为test.zip using (FileStream fsOut = File.Create("test.zip")) { //ZipOutputStream类 ...

  7. js 删除 数组中某个元素(转载)

    来源:https://www.jb51.net/article/134312.htm js删除数组中某一项或几项的几种方法 https://www.jb51.net/article/154737.ht ...

  8. Linux主机之间传输文件的几种方法对比

    1.scp传输 scp -r /data/file root@ip:/data/ scp -C /data/sda.img root@ip:/data/img/#-r: 支持目录#-C: 启用压缩传送 ...

  9. yarn 错误There appears to be trouble with your network connection. Retrying...

    原因:yarn超时 解决途径:1.安装好后更换淘宝镜像 yarn config set registry https://registry.npm.taobao.org

  10. typescript 接口

    接口:用来建立某种代码约定,使得其他开发者在调用某个方法或创建新的类时必须遵循接口所定义的代码约定   在js里面没有接口这个概念,在ts里面通过两个关键字来支撑接口这个特性   interface ...