题目

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. Python接口自动化基础---get请求

    1.没有参数的get请求 import requests r=requests.get('http://docs.python-requests.org/zh_CN/latest/user/quick ...

  2. Euraka适合初学者的简单小demo

    1. 创建父工程:父工程的的打包形式该为pom,删除其余无关的文件 修改父工程的pom文件内容如下: <?xml version="1.0" encoding="U ...

  3. linux设备树的建立过程

    为了阐明表示总线.设备和设备驱动程序的各个数据结构之间彼此的关联,它们的注册过程是很有必要的.顺序一定是如下:(1)注册总线---bus_register:(2)注册设备device_register ...

  4. Xen们和Open Stack们

    1.虚拟化技术:XEN.KVM.ESXI 2.虚拟化管理:Eucalyptus, OpenNebula, OpenStack, OpenQRM, XenServer, Oracle VM, Cloud ...

  5. maven cmd 命令

    1. mvn clean install :重新清理打包 2.详见:https://www.cnblogs.com/lukelook/p/11298168.html mvn  versions:upd ...

  6. Linux将用户添加到组的指令

    原文:https://blog.csdn.net/youmatterhsp/article/details/80549683:           https://www.cnblogs.com/cl ...

  7. Linux命令——tree

    参考:Linux tree Command Tutorial for Beginners (6 Examples) 简介 Linux tree命令用于以树状图列出目录的内容. 执行tree指令,它会列 ...

  8. 使用fail2ban预防被挖洞的笔记

    参考:https://blog.csdn.net/dorisnzy/article/details/82926067 1.安装fail2ban: yum -y install epel-release ...

  9. html实战

    主要html源码 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&qu ...

  10. javaWeb的HttpServletRequest和HttpServletResponse

    HttpServletRequest HttpServletRequest对象是封装了用户的请求信息,包括请求参数去,请求头等信息,service()f方法中的两个HttpServletRequest ...