直接子树加是困难的,不妨从斐波那契入手。
首先有通项公式:,其中 。
接着我们只需要分别计算 和 的和即可。
在模 意义下 没有二次剩余,于是扩域。
扩域
这里简单说一下如何扩域。
将所有数写成 之形式,简记为 。
扩域下的运算:
加法:。
减法:。
乘法:。
逆元:。
注意逆元那里的 不为 ,否则和 是二次非剩余矛盾。
关于本题中的询问,我们记 为根到 的和,那么 到 的和是经典容斥 。
考虑求解 。注意到影响 的只有 的祖先,如果 的祖先 有一个操作一,参数为 ,其贡献(以 为例)为 。
记 的祖先集合为 ,对 进行操作一 的集合为 ,那么 的贡献为:
类似地, 的贡献为:
然后线段树维护这个就行了,维护四个值,操作一的时候分别子树加 。
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
// const int V = ;
const int mod = 1e9 + 7;
typedef unsigned us;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef vector <int> vi;
typedef vector <pii> vpi;
typedef vector <ll> vl;
template <class T> using pq = priority_queue <T>;
template <class T> using pqg = priority_queue <T, vector <T>, greater <T> >;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define repr(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define perr(i, a, b) for (int i = (a); i > (b); --i)
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
template <class T1, class T2> inline void ckmn(T1 &a, T2 b) { (a > b) && (a = b, 0); }
template <class T1, class T2> inline void ckmx(T1 &a, T2 b) { (a < b) && (a = b, 0); }
namespace IO {
// char buf[1 << 23], *p1 = buf, *p2 = buf;
// #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
template <class T> void rd(T &a, unsigned c = 0) {
while (c = getchar(), c < 48 || c > 57);
for (a = 0; c >= 48 && c <= 57; c = getchar()) a = (a << 3) + (a << 1) + (c ^ 48);
}
template <class T> void wrt(T x) { if (x > 9) wrt(x / 10); putchar(x % 10 ^ 48); }
} using IO::rd; using IO::wrt;
int qpw(int a, int b) {
int R = 1;
for (; b; b >>= 1) {
if (b & 1) R = 1ll * R * a % mod;
a = 1ll * a * a % mod;
}
return R;
}
struct cpl {
int a, b;
cpl() { a = b = 0; }
template <class T> cpl(T x) { a = (x % mod + mod) % mod, b = 0; }
template <class T1, class T2> cpl(T1 x, T2 y) { a = (x % mod + mod) % mod, b = (y % mod + mod) % mod; }
cpl inv();
template <class T> cpl pow(T b);
};
cpl operator+ (cpl a, cpl b) { return {(a.a + b.a) % mod, (a.b + b.b) % mod}; }
cpl& operator+= (cpl &a, cpl b) { return a = a + b; }
cpl operator- (cpl a) { return {(mod - a.a) % mod, (mod - a.b) % mod}; }
cpl operator- (cpl a, cpl b) { return a + (-b); }
cpl& operator-= (cpl &a, cpl b) { return a += -b; }
cpl operator* (cpl a, cpl b) { return {(1ll * a.a * b.a + 5ll * a.b * b.b) % mod, (1ll * a.a * b.b + 1ll * a.b * b.a) % mod}; }
cpl& operator*= (cpl &a, cpl b) { return a = a * b; }
cpl cpl::inv() {
auto [a, b] = *this;
return cpl(a, -b) * qpw((1ll * a * a - 5ll * b * b % mod + mod) % mod, mod - 2);
}
cpl operator/ (cpl a, cpl b) { return a * b.inv(); }
cpl& operator/= (cpl &a, cpl b) { return a *= b.inv(); }
template <class T> cpl cpl::pow(T b) {
cpl a = *this, R = 1;
if (b < 0) {
a = a.inv();
b = -b;
}
for (; b; b >>= 1) {
if (b & 1) R *= a;
a *= a;
}
return R;
}
cpl phi = {500000004, 500000004}, psi = {500000004, 500000003};
int n, m, u, v, op;
ll k;
vi T[N];
int dep[N], anc[N][20], df[N], ed[N], ind;
struct Seg {
int l, r;
cpl tg1, tg2, tg3, tg4;
#define l(p) tr[p].l
#define r(p) tr[p].r
#define tg1(p) tr[p].tg1
#define tg2(p) tr[p].tg2
#define tg3(p) tr[p].tg3
#define tg4(p) tr[p].tg4
} tr[N << 2];
#define M (l(p) + r(p) >> 1)
#define ls (p << 1)
#define rs (p << 1 | 1)
void dfs(int u, int fa) {
dep[u] = dep[fa] + 1, anc[u][0] = fa, df[u] = ++ind;
for (auto v : T[u]) if (v != fa) dfs(v, u);
ed[u] = ind;
}
inline void pushdown(int p) {
#define SB(name) name(ls) += name(p), name(rs) += name(p), name(p) = 0
SB(tg1), SB(tg2), SB(tg3), SB(tg4);
#undef SB
}
void build(int p, int l, int r) {
l(p) = l, r(p) = r;
if (l == r) return;
build(ls, l, M), build(rs, M + 1, r);
}
void maketag(int p, int l, int r, cpl a, cpl b, cpl c, cpl d) {
if (l <= l(p) && r >= r(p)) {
tg1(p) += a, tg2(p) += b, tg3(p) += c, tg4(p) += d;
return;
}
pushdown(p);
if (l <= M) maketag(ls, l, r, a, b, c, d);
if (r > M) maketag(rs, l, r, a, b, c, d);
}
tuple <cpl, cpl, cpl, cpl> query(int p, int x) {
if (l(p) == r(p)) return {tg1(p), tg2(p), tg3(p), tg4(p)};
pushdown(p);
return query(x <= M ? ls : rs, x);
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
per(i, 18, 0) if (dep[anc[u][i]] >= dep[v]) u = anc[u][i];
if (u == v) return u;
per(i, 18, 0) if (anc[u][i] != anc[v][i]) u = anc[u][i], v = anc[v][i];
return anc[u][0];
}
int qry(int u) {
if (!u) return 0;
auto [A, B, C, D] = query(1, df[u]);
// cout << u << " " << A.a << " " << A.b << " " << B.a << " " << B.b << " " << C.a << " " << C.b << " " << D.a << " " << D.b << endl;
// cout << " " << (phi.pow(1 - dep[u])).a << " " << (phi.pow(1 - dep[u])).b << " " << (psi.pow(1 - dep[u])).a << " " << (psi.pow(1 - dep[u])).b << endl;
cpl E = (phi.pow(dep[u] + 1) * B - A) / (phi - 1);
cpl F = (psi.pow(dep[u] + 1) * D - C) / (psi - 1);
return ((E - F) / cpl(0, 1)).a;
}
void solve() {
rd(n), rd(m);
repr(i, 1, n) rd(u), rd(v), T[u].pb(v), T[v].pb(u);
dfs(1, 0);
rep(j, 1, 18) rep(i, 1, n) anc[i][j] = anc[anc[i][j - 1]][j - 1];
build(1, 1, n);
while (m--) {
while (isspace(op = getchar()));
if (op == 'U') {
rd(u), rd(k);
maketag(1, df[u], ed[u], phi.pow(k), phi.pow(k - dep[u]), psi.pow(k), psi.pow(k - dep[u]));
} else {
rd(u), rd(v);
int lc = lca(u, v);
wrt((0ll + qry(u) + qry(v) - qry(lc) - qry(anc[lc][0]) + mod + mod) % mod), putchar(10);
}
}
}
int main() {
int T = 1;
if (0) rd(T);
while (T--) solve();
}