logo AlgoBeat OnlineJudge
登录 注册

#217585. [ICPC 2019 Yinchuan R] XOR Tree

内存限制:512 MiB 时间限制:4000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

You are given a tree with nodes labelled from to , the root of which is the node . Each node has a given value .

We define as the number of edges in the shortest path from the node to the node , and define a multiset as . Note that here .

We define the score of any arbitrary set as the sum of squares of XORs of any two numbers. For example, the score of the set should be

where denotes the bitwise exclusive-or.

Now you are given the parameter . For each node you need to compute the score of .

输入格式

The first line of input contains two integers , the number of nodes of the tree and the parameter described above.

The second line of input contains integers, the -th number is the value of the -th node.

The third line of input contains integers, the -th number is the parent of the -th node.

输出格式

Output lines, the -th line contains a single integer, the score of . Note that the answer can be extremely large, please output it modulo instead.

样例

样例输入 1

6 1
4 3 2 4 3 1
1 1 2 2 5

样例输出 1

86
98 
0
0
4
0