\(\mathcal{Description}\)

  Link.

  用给定的 \(\{a_{n-1}\},\{c_n\}\) 生成一棵含有 \(n\) 个点的树,其中 \(u\) 连向 \([1,u)\) 中的某个 \(v\),概率为 \(\frac{a_v}{a_1+a_2+\cdots+a_{u-1}}\),边权为 \(c_u+c_v\)。并给出 \(q\) 组询问 \((u_i,v_i)\),每次回答 \(u_i\) 到 \(v_i\) 的树上距离的期望。答案对 \((10^9+7)\) 取模。

  \(n,q\le3\times10^5\)。

\(\mathcal{Solution}\)

\[\textit{Defining }\LaTeX\textit{ macros...}
\newcommand{\vct}[1]{\boldsymbol{#1}}
\newcommand{\stir}[2]{\genfrac{\{}{\}}{0pt}{}{#1}{#2}}
\newcommand{\opn}[1]{\operatorname{#1}}
\newcommand{\lcm}[0]{\opn{lcm}}
\newcommand{\sg}[0]{\opn{sg}}
\newcommand{\dist}[0]{\opn{dist}}
\newcommand{\lca}[0]{\opn{lca}}
\newcommand{\floor}[2]{\left\lfloor\frac{#1}{#2}\right\rfloor}
\newcommand{\ceil}[2]{\left\lceil\frac{#1}{#2}\right\rceil}
\]

  问题卡壳,必有结论。

  令 \(1\) 为根,把 \(\dist(u,v)\) 转化成 \(\dist(1,u)+\dist(1,v)-2\dist(\lca(u,v))\)。记 \(f(u)=E(\dist(1,u))\),显然有

\[f(u)=c_u+\frac{1}{s_{u-1}}\sum_{v<u}a_v(f_v+c_v).
\]

其中 \(s_i=\sum_{j=1}^ia_i\),可见 \(f\) 可以轻易地 \(\mathcal O(n)\) 求出。我们接下来研究 \(\dist(\lca(u,v))\)。不妨设 \(u<v\),可以发现一个结论:

\[\forall v>u,~E(\dist(\lca(u,v)))=g(u).
\]

其中 \(g(u)\) 是仅与 \(u\) 有关的量。

证明

  考虑求 $\lca(u,v)$ 的方式,在 $v$ 沿着祖先跳跃时,我们只关心第一次使得 $v\le u$ 的位置。此时仅有两种情况

  • \(v=u\),概率为 \(\frac{a_u}{s_u}\);
  • \(v<u\),概率为 \(\frac{s_{u-1}}{s_u}\)。

  可见与 \(v\) 无关。

  在证明的基础上,亦能得到 \(g(u)\) 的转移:

\[g(u)=\frac{1}{s_u}\left(a_uc_u+\sum_{v<u}a_vg_v\right).
\]

也能 \(\mathcal O(n)\) 求出,所以本题就解决啦。

\(\mathcal{Code}\)

/*~Rainybunny~*/

#include <bits/stdc++.h>

#define rep( i, l, r ) for ( int i = l, rep##i = r; i <= rep##i; ++i )
#define per( i, r, l ) for ( int i = r, per##i = l; i >= per##i; --i ) const int MAXN = 3e5, MOD = 1e9 + 7;
int n, q, a[MAXN + 5], s[MAXN + 5], invs[MAXN + 5];
int c[MAXN + 5], f[MAXN + 5], g[MAXN + 5]; inline int mul( const int a, const int b ) { return 1ll * a * b % MOD; }
inline int sub( int a, const int b ) { return ( a -= b ) < 0 ? a + MOD : a; }
inline int add( int a, const int b ) { return ( a += b ) < MOD ? a : a - MOD; }
inline int mpow( int a, int b ) {
int ret = 1;
for ( ; b; a = mul( a, a ), b >>= 1 ) ret = mul( ret, b & 1 ? a : 1 );
return ret;
} int main() {
std::ios::sync_with_stdio( false ), std::cin.tie( 0 ); std::cin >> n >> q;
rep ( i, 1, n - 1 ) {
std::cin >> a[i], s[i] = a[i] + s[i - 1];
invs[i] = mpow( s[i], MOD - 2 );
}
rep ( i, 1, n ) std::cin >> c[i]; for ( int i = 2, pre = mul( a[1], c[1] ); i <= n; ++i ) {
f[i] = add( c[i], mul( invs[i - 1], pre ) );
pre = add( pre, mul( a[i], add( f[i], c[i] ) ) );
} for ( int i = 2, pre = 0; i < n; ++i ) {
g[i] = mul( invs[i], add( mul( a[i], f[i] ), pre ) );
pre = add( pre, mul( a[i], g[i] ) );
} for ( int u, v; q--; ) {
std::cin >> u >> v;
if ( u > v ) u ^= v ^= u ^= v;
if ( u == v ) std::cout << "0\n";
else std::cout << sub( add( f[u], f[v] ), mul( 2, g[u] ) ) << '\n';
}
return 0;
}

Solution -「Gym 102979E」Expected Distance的更多相关文章

  1. Solution -「Gym 102979L」 Lights On The Road

    \(\mathcal{Description}\)   Link.   给定序列 \(\{w_n\}\),选择 \(i\) 位置的代价为 \(w_i\),要求每个位置要不被选择,要不左右两个位置至少被 ...

  2. Solution -「Gym 102956F」Find the XOR

    \(\mathcal{Description}\)   Link.   给定 \(n\) 个点 \(m\) 条边的连通无向图 \(G\),边有边权.其中 \(u,v\) 的距离 \(d(u,v)\) ...

  3. Solution -「Gym 102956B」Beautiful Sequence Unraveling

    \(\mathcal{Description}\)   Link.   求长度为 \(n\),值域为 \([1,m]\) 的整数序列 \(\lang a_n\rang\) 的个数,满足 \(\not\ ...

  4. Solution -「Gym 102956F」Border Similarity Undertaking

    \(\mathcal{Description}\)   Link.   给定一张 \(n\times m\) 的表格,每个格子上写有一个小写字母.求其中长宽至少为 \(2\),且边界格子上字母相同的矩 ...

  5. Solution -「Gym 102956A」Belarusian State University

    \(\mathcal{Description}\)   Link.   给定两个不超过 \(2^n-1\) 次的多项式 \(A,B\),对于第 \(i\in[0,n)\) 个二进制位,定义任意一个二元 ...

  6. Solution -「Gym 102798I」Sean the Cuber

    \(\mathcal{Description}\)   Link.   给定两个可还原的二阶魔方,求从其中一个状态拧到另一个状态的最小步数.   数据组数 \(T\le2.5\times10^5\). ...

  7. Solution -「Gym 102798K」Tree Tweaking

    \(\mathcal{Description}\)   Link.   给定排列 \(\{p_n\}\),求任意重排 \(p_{l..r}\) 的元素后,将 \(\{p_n\}\) 依次插入二叉搜索树 ...

  8. Solution -「Gym 102798E」So Many Possibilities...

    \(\mathcal{Description}\)   Link.   给定非负整数序列 \(\{a_n\}\) 和 \(m\),每次随机在 \(\{a\}\) 中取一个非零的 \(a_i\)(保证存 ...

  9. Solution -「Gym 102759I」Query On A Tree 17

    \(\mathcal{Description}\)   Link.   给定一棵含 \(n\) 个结点的树,结点 \(1\) 为根,点 \(u\) 初始有点权 \(a_u=0\),维护 \(q\) 次 ...

随机推荐

  1. MySQL删除数据库或表(DROP DATABASE/table语句)

    DROP DATABASE [ IF EXISTS ] <数据库名> DROP table[ IF EXISTS ] <数据库表名> 语法说明如下: <数据库名>: ...

  2. PowerShell 教程

    随笔分类 - 教程 转载自:https://www.cnblogs.com/XiaoCY/category/1065141.html PowerShell 管道符之Where-Object的使用方法 ...

  3. sqlserver - 某字段数据为json串, 获取该json串里的值 的详细方法

    1.前言 某字段的数据为json 但是我想只获取里面的某一个值,该怎么操作? 2.笔记 (1)用 JSON_VALUE(参数1,参数2)函数 ,有两个参数, (2)参数1 为 列名 ,参数2 为 js ...

  4. 力扣 - 剑指 Offer 49. 丑数

    题目 剑指 Offer 49. 丑数 思路1 丑数是只包含 2.3.5 这三个质因子的数字,同时 1 也是丑数.要计算出 n 之前全部的丑数,就必须将 n 之前的每个丑数都乘以 2.3.5,选取出最小 ...

  5. winform设置所有窗体统一图标

    class WindowHookerManager { static WindowHooker hooker = new WindowHooker(); public static void SetA ...

  6. Java异常理解之Exception in thread “main“ java.lang.ArrayIndexOutOfBoundsException

    这个异常是Java中的数组越界问题 当你使用不合法的索引访问数组是会出现这种错误例如: class Solution { public static int climbStairs(int n) { ...

  7. JAVA主要类集分类

    包装类 Integer包装类 方法 返回值 功能描述 byteValue() byte 以 byte 类型返回该 Integer 的值 intValue() int 以 int 型返回此 Intege ...

  8. gin中如何自定义中间件

    package main import ( "fmt" "github.com/gin-gonic/gin" ) func main() { // 新建一个没有 ...

  9. hexo博客如何插入图片

    Hexo是一个静态的博客网站生成器,生成一个博客只需要分分钟的时间就能搞定. Hexo的博文是支持Markdown格式的,发表一篇文章只需要简简单单的几个命令. hexo new '文章'就会生成一个 ...

  10. maven 项目搭建,本地环境配置。

    一,下载地址 http://maven.apache.org/download.cgi 英文不好的,可以在浏览器下载个翻译的插件,就好了,我用的是谷歌. 二, 解压文件,安装在自己制定的目录,注意安装 ...