atcoder#ABC201E. [ABC201E] Xor Distances
[ABC201E] Xor Distances
Score : points
Problem Statement
We have a weighted tree with vertices. The -th edge connects Vertex and Vertex bidirectionally and has a weight .
For a pair of vertices , let us define as follows:
- the XOR of the weights of the edges in the shortest path from to .
Find for every pair such that , and print the sum of those values modulo .
What is $\text{ XOR }$?
The bitwise of integers and , , is defined as follows:
- When is written in base two, the digit in the 's place () is if exactly one of and is , and otherwise.
For example, we have (in base two: ).
Constraints
- The given graph is a tree.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the sum of , modulo .
3
1 2 1
1 3 3
6
We have and , for the sum of .
5
3 5 2
2 3 2
1 5 1
4 5 13
62
10
5 7 459221860242673109
6 8 248001948488076933
3 5 371922579800289138
2 5 773108338386747788
6 10 181747352791505823
1 3 803225386673329326
7 8 139939802736535485
9 10 657980865814127926
2 4 146378247587539124
241240228
Print the sum modulo .