题目

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. kubernetes第六章--如何访问pod

  2. PHP 结合 Bootstrap 实现学生列表以及添加学生功能实现(继上篇登录及注册功能之后)

    本人是一位学生,正在学习当中,可能BUG众多,请见谅并指正,谢谢!!! 学生列表实现 HTML: <!DOCTYPE html> <html> <head> < ...

  3. “proxy” in package.json must be a string 解决办法

    今天学习一个react项目.想从本地服务器获取数据. 直接axios.get('http://localhost:80/api/react/header.json'),报错跨域. 网上查了下,需要在p ...

  4. SQL SERVER-Extendevent捕获执行慢的语句

    USE MASTER; GO /* Conditionally drop the session if it already exists */ IF EXISTS (SELECT * FROM sy ...

  5. Flask之基础

    一,flask Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求 ...

  6. sql语句,数据库中,同表添加,主键不同,数据相同。

    insert into tablename(各个字段名) select #新主键值,其他字段名 from tablename where No = #表中主键值

  7. Java Decompiler反编译Jar文件

    1.重新编译已经打包的Jar包,使用 Java Decompiler 打开需要重新编译的jar包,找到自己需要自己修改的Class文件 ,修改之后电子保存文件 ,保存的时候编译工具自动将class文件 ...

  8. [https][tls] 如何使用wireshark查看tls/https加密消息--使用keylog

    姊妹篇: [ipsec][strongswan] 使用wireshark查看strongswan ipsec esp ikev1 ikev2的加密内容 [https][tls] 如何使用wiresha ...

  9. C实现除法

    C实现除法 来源 Leetcode上的一个题,做完后感觉很有意义,因而记录. 实际上自己也查阅了不少的实现除法的方式,最后还是感觉这个方法是最好的,没有别的原因,就是快. 需要注意的一些点 正整数之间 ...

  10. 用js刷剑指offer(数组中的逆序对)

    题目描述 题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P ...