CF1187F Expected Square Beauty(期望)
题目
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(期望)的更多相关文章
- Codeforces 1187 F - Expected Square Beauty
F - Expected Square Beauty 思路:https://codeforces.com/blog/entry/68111 代码: #pragma GCC optimize(2) #p ...
- @codeforces - 1187F@ Expected Square Beauty
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个序列 x = {x1, x2, ..., xn},已知 ...
- 【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 ...
- Educational Codeforces Round 67
Educational Codeforces Round 67 CF1187B Letters Shop 二分 https://codeforces.com/contest/1187/submissi ...
- CF-diary
(做题方式:瞟题解然后码) 1238E. Keyboard Purchase \(\texttt{Difficulty:2200}\) 题意 给你一个长度为 \(n\) 的由前 \(m\) 个小写字母 ...
- NLP&数据挖掘基础知识
Basis(基础): SSE(Sum of Squared Error, 平方误差和) SAE(Sum of Absolute Error, 绝对误差和) SRE(Sum of Relative Er ...
- 常用的机器学习&数据挖掘知识点【转】
转自: [基础]常用的机器学习&数据挖掘知识点 Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Le ...
- 【基础】常用的机器学习&数据挖掘知识点
Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Least Square Methods 最小二乘法),ML ...
- 概率分布之间的距离度量以及python实现(三)
概率分布之间的距离,顾名思义,度量两组样本分布之间的距离 . 1.卡方检验 统计学上的χ2统计量,由于它最初是由英国统计学家Karl Pearson在1900年首次提出的,因此也称之为Pearson ...
随机推荐
- 【洛谷 P3804】 【模板】后缀自动机
题目链接 #include <cstdio> #include <cstring> #include <algorithm> using namespace std ...
- 在Xcode4 中将iPhone使用的xib转换成iPad使用的xib
来源:http://blog.3snews.net/space.php?uid=6188&do=blog&id=64200 http://www.giser.net/?p=982 1 ...
- php验证码案例
<?php header('Content-type:image/jpeg'); $img=imagecreatetruecolor(120,40); // 背景颜色 $bg_color=ima ...
- 小程序canvas绘制倒计时
如果本文对你有用,请爱心点个赞,提高排名,帮助更多的人.谢谢大家!❤ 如果解决不了,可以在文末进群交流. 效果展示: //广告倒计时 advTimeCountDown:function(advTime ...
- 阿里云ECS服务器设置端口(允许访问设置)
1.登录阿里云找到对应的服务器按照如下箭头指示: 2.点击“安全组配置”后进入到如下界面,点击“配置规则”进入详情配置界面. 3.点击“修改”可对特定的端口进行访问配置,如下图: 至此结束.
- Android笔记(六十九) 仿微信界面(一)
综合之前的Fragment和自定义组件的知识,实现微信界面 MainActivity.java package cn.lixyz.test; import android.app.Acti ...
- Flask之Flask_Session插件
一,Flask_Session介绍 因为flask自带的session是将session存在cookie中: 所以才有了第三方Flask_session插件,可以将session存储在我们想存储的数据 ...
- Flink 使用(一)——从kafka中读取数据写入到HBASE中
1.前言 本文是在<如何计算实时热门商品>[1]一文上做的扩展,仅在功能上验证了利用Flink消费Kafka数据,把处理后的数据写入到HBase的流程,其具体性能未做调优.此外,文中并未就 ...
- USB之基本协议和数据波形1
============= 本系列参考 ============= <圈圈教你玩USB>.<Linux那些事儿之我是USB> 协议文档:https://www.usb.or ...
- Microsoft Onenote shortcuts / Onenote快捷键大全
Onenote跟Libreoffice ,有道笔记比起来一个快捷键特别不太好用,就是Ctrl + Shift +v 并不会提供一个选择粘贴模式选项. 而在Onenote 中 Ctrl + Shift ...