#abc201e. E - Xor Distances
E - Xor Distances
Score : points
问题描述
我们有一个包含 个顶点的加权树。第 条边双向连接顶点 和顶点 ,且具有权重 。
对于一对顶点 ,我们定义 如下:
- 从 到 的最短路径上边权重的 异或 运算结果。
求出所有满足条件 的顶点对 的 值,并将这些值的和对 取模输出。
什么是 ?
整数 和 的按位 (异或)运算,记作 ,定义如下:
- 当 转换为二进制表示时,其在 位置上的数字()是 ,当且仅当 和 中恰好有一个为 ,否则为 。
例如,我们有 (以二进制表示:)。
以上为通义千问 qwen-max 翻译,仅供参考。
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 ?
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 .
Sample Input 1
3
1 2 1
1 3 3
Sample Output 1
6
We have and , for the sum of .
Sample Input 2
5
3 5 2
2 3 2
1 5 1
4 5 13
Sample Output 2
62
Sample Input 3
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
Sample Output 3
241240228
Print the sum modulo .
update @ 2024/3/10 09:13:40