\(\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. CODING 携手 Thoughtworks 助力老百姓大药房打造”自治、自决、自动”的敏捷文化

    老百姓大药房是中国具有影响力的药品零售连锁企业,中国药品零售企业综合竞争力百强冠军.中国服务业 500 强企业.湖南省百强企业. 自 2001 年创立以来,现已成功开发了湖南. 陕西.浙江.江苏等 * ...

  2. 【Warrior刷题笔记】143.重排链表 【线性化 || 双指针+翻转链表+链表合并】详细注释

    题目一 力扣143.重排链表 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/reorder-list/ 1.描述 给定一个单链表L的头节点he ...

  3. RichTextBox单独设置文字颜色

    richTextBox1.Select(index, "str1".Length); richTextBox1.SelectionColor = Color.Red;

  4. EF中使用事务

    using (var db = new dbEntities()) { //第一个坑,需要手动open db.Database.Connection.Open(); using (var tran = ...

  5. 【记录一个问题】铁威马nas,噪音太大了,我老婆说在客厅放了一个电饭锅

    1.硬盘转动的噪音特别大,而且还是有很大IO的长时间猛转: 2.IO的管理算法,以及做IO的进程,都有很大问题.并未做任何操作,动不动就疯了一样硬盘猛转.

  6. 配置vscode的C++环境Unexpected GDB output from command "-environment-cd

    原因 中文字符 换成D盘目录下以后.

  7. spring拦截机制中Filter(过滤器)、interceptor(拦截器)和Aspect(切面)的使用及区别

    Spring中的拦截机制,如果出现异常的话,异常的顺序是从里面到外面一步一步的进行处理,如果到了最外层都没有进行处理的话,就会由tomcat容器抛出异常. 1.过滤器:Filter :可以获得Http ...

  8. golang中循环或递归求阶乘

    package main import "fmt" func factorialFor(num int) (ret int) { // 循环求阶乘 ret = 1 for i := ...

  9. Codeforces Round #742 (Div. 2)

    A. Domino Disaster 思路 按照题意模拟即可 如果是 对应关系为R --> R L --> L U --> D D --> U AC_CODE inline v ...

  10. ApacheCN 计算机视觉译文集 20210218 更新

    新增了六个教程: OpenCV3 安卓应用编程 零.前言 一.设置 OpenCV 二.使用相机帧 三.应用图像效果 四.识别和跟踪图像 五.将图像跟踪与 3D 渲染相结合 六.通过 JNI 混合 Ja ...