3181.Furukawa Nagisa's Tree

Time Limit: 1s Memory Limit: 256MB

One day, Okazaki Tomoya has bought a tree for Furukawa Nagisa's birthday. The tree is so strange that every node of the tree has a value. The value of the i-th node is vi. Now Furukawa Nagisa and Okazaki Tomoya want to play a game on the tree.

Let (s,e) be the path from node s to node e, we can write down the sequence of the values of nodes on path (s,e), and denote this sequence as S(s,e). We define the value of the sequence G(S(s,e)) as follows. Suppose that the sequence is z0,z1...zl-1, where l is the length of the sequence. We define G(S(s,e))=z0 \times k0+z1 \times k1+...+zl-1 \times kl-1. If the path (s,e) satisfies 3181_1.png, then the path (s,e) belongs to Furukawa Nagisa, otherwise it belongs to Okazaki Tomoya.

Calculating who has more paths is too easy, so they want to play something more difficult. Furukawa Nagisa thinks that if paths (p1,p2) and (p2,p3) belong to her, then path (p1,p3) belongs to her as well. Also, she thinks that if paths (p1,p2) and (p2,p3) belong to Okazaki Tomoya, then path (p1,p3) belongs to Okazaki Tomoya as well. But in fact, this conclusion isn't always right. So now Furukawa Nagisa wants to know how many triplets (p1,p2,p3) are correct for the conclusion, and this is your task.

Input Format(From the terminal/stdin)

The first line contains four integers n, y, k and x(1 \le n \le 105;2 \le y \le 109;1 \le k \lt y;0 \le x \lt y) - n being the number of nodes on the tree. It is guaranteed that y is a prime number.

The second line contains n integers, the i-th integer is vi(0 \le vi \lt y).

Then follow n-1 lines, each line contains two integers, denoting an edge of the tree. The nodes of the tree are numbered from 1 to n.

Output Format(To the terminal/stdout)

Output a single integer - the number of triplets that are correct for Furukawa Nagisa's conclusion.

Sample Input 1

Copy
1 2 1 0
1
 · · · \n
 \n

Sample Output 1

Copy
1
 \n

Sample Input 2

Copy
3 5 2 1
4 3 1
1 2
2 3
 · · · \n
 · · \n
 · \n
 · \n

Sample Output 2

Copy
14
  \n

Sample Input 3

Copy
8 13 8 12
0 12 7 4 12 0 8 12
1 8
8 4
4 6
6 2
2 3
8 5
2 7
 ·  · ·  \n
 ·  · · ·  · · ·  \n
 · \n
 · \n
 · \n
 · \n
 · \n
 · \n
 · \n

Sample Output 3

Copy
341
   \n

Submit

请先 登录

© 2025 FAQs