luogu#P5138. fibonacci
fibonacci
Background
Wolfycz really likes the Fibonacci sequence (just kidding).
Problem Description
Wolfycz is keen on studying the Fibonacci sequence. He defines as the -th term of the Fibonacci sequence, and defines . For any , it satisfies .
Wolfycz also likes planting trees. One day he planted a “funny tree” (that is, the tree in OI: nodes, edges, rooted at ). Initially, none of the nodes on the tree had any funny fruits. Then Wolfycz spent a lot of money to buy fertilizer. Each time he fertilizes a node on the tree with grams, node and its entire subtree will grow a bunch of funny fruits. Specifically, if a node in the subtree of has distance to , then this node will grow funny fruits.
Wolfycz thinks there are already enough funny fruits on the tree, so he wants to play a game, and he will occasionally ask you how many funny fruits there are on the path between two nodes.
Input Format
The first line contains two integers , representing the number of nodes in the tree and the number of operations.
Lines to each contain two integers , indicating that there is an edge between nodes and .
Next come lines, each in the form U x k or Q x y.
U x k: For each node in the subtree of , if its distance to is , then it grows funny fruits. Node also grows fruits.Q x y: Query the sum of funny fruits on all nodes along the path from to (including and ). Output the answer modulo .
Output Format
For each Q x y query, output the answer.
5 10
2 1
1 3
2 4
5 2
Q 1 5
U 1 1
Q 1 1
Q 1 2
Q 1 3
Q 1 4
Q 1 5
U 2 2
Q 2 3
Q 4 5
0
1
2
2
4
4
4
10
Hint
For of the testdata, .
For of the testdata, , , .
Translated by ChatGPT 5