CF434E Furukawa Nagisa's Tree

CF434E Furukawa Nagisa's Tree

洛谷:CF434E Furukawa Nagisa's Tree

Codeforces:CF434E Furukawa Nagisa's Tree

Problem

冈崎朋也要送古河渚一棵点带点权的树。给定常数 \(k, x, y\),其中保证 \(y \le 10^9\)\(y\) 是质数,\(0 \le x < y\)\(1 \le k < y\)

\(S(s, e)\) 表示从 \(s\) 出发到 \(e\) 的简单路径,并定义 \(G(S(s, e)) = \sum\limits_{i = 0}^{|S(s, e)| - 1}k^{i}v_{i}\)

如果 \(G(S(s, e)) \equiv x \pmod{y}\),则路径 \(S(s, e)\) 属于小渚,否则属于朋也。

由于小渚旷了一学期的课,她以为如果路径 \(S(p_1, p_2)\)\(S(p_2, p_3)\) 都属于某个人,则 \(S(p_1, p_3)\) 也属于那个人。

很显然这是不一定正确的。请你求出有多少个三元组 \((p_1, p_2, p_3)\) 满足小渚的预测。

Solution

此题最快的解法(雾)

\(f(s, t)\) 表示 \([G(S(s, t)) \equiv x \pmod{y}]\),则答案可以直接写成:

\[\begin{aligned} ans &= \sum\limits_{p_1 = 1}^{n}\sum\limits_{p_2 = 1}^{n}\sum\limits_{p_3 = 1}^{n}[f(p_{1}, p_{2}) = f(p_{2}, p_{3}) = f(p_{1}, p_{3})] \\ &= \sum\limits_{p_1 = 1}^{n}\sum\limits_{p_2 = 1}^{n}\sum\limits_{p_3 = 1}^{n}([f(p_{1}, p_{2}) = f(p_{2}, p_{3}) = f(p_{1}, p_{3}) = 1] + [f(p_{1}, p_{2}) = f(p_{2}, p_{3}) = f(p_{1}, p_{3}) = 0]) \\ &= \sum\limits_{p_1 = 1}^{n}\sum\limits_{p_2 = 1}^{n}\sum\limits_{p_3 = 1}^{n}( f(p_{1}, p_{2})f(p_{2}, p_{3})f(p_{1}, p_{3}) + (1 - f(p_{1}, p_{2}))(1 - f(p_{2}, p_{3}))(1 - f(p_{1}, p_{3}))) \\ &= \sum\limits_{p_1 = 1}^{n}\sum\limits_{p_2 = 1}^{n}\sum\limits_{p_3 = 1}^{n}(1 - f(p_{1}, p_{2}) - f(p_{2}, p_{3}) - f(p_{1}, p_{3}) + f(p_{1}, p_{2})f(p_{2}, p_{3}) + f(p_{1}, p_{2})f(p_{1}, p_{3}) + f(p_{2}, p_{3})f(p_{1}, p_{3})) \\ \end{aligned} \]

\(a_{u} = \sum\limits_{v = 1}^{n}f(v, u)\)\(b_{u} = \sum\limits_{v = 1}^{n}f(u, v)\)(实际上有 \(\sum\limits_{u = 1}^{n}a_{u} = \sum\limits_{u = 1}^{n}b_{u}\))。则答案可以表示为:

\[\begin{aligned} ans = n^{3} - 3n\sum\limits_{u = 1}^{n}a_{u} + \sum\limits_{u = 1}^{n}(a_{u}b_{u} + a_{u}^{2} + b_{u}^{2}) \\ \end{aligned} \]

于是现在只需要求出所有 \(a_{u}, b_{u}\)

这是容易用点分治解决的,用 map 存一下即可。

时间复杂度 \(O(n\log^{2}{n})\)

code CF434E

posted @ 2023-12-24 23:55  Schucking_Sattin  阅读(6)  评论(0编辑  收藏  举报