PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000041. Tổ tiên chung nhanh với truy vấn sinh tự động (Fast Lowest Common Ancestor)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổ tiên chung nhanh với truy vấn sinh tự động (Fast Lowest Common Ancestor)

Fast Lowest Common Ancestor

Source: VNOJ

Version: Phuoc Hung OJ Extended

Problem Statement

An undirected tree with nn vertices is rooted at vertex 11. Answer qq LCA queries, where the query vertices are generated from six parameters to reduce input size.

Initially (u1,v1)=(ustart,vstart)(u_1,v_1)=(u_{start},v_{start}). For i>1i>1, ui=(ui−1⋅ujump+uadd) mod n+1u_i=(u_{i-1}\cdot u_{jump}+u_{add})\bmod n+1 and vi=(vi−1⋅vjump+vadd) mod n+1v_i=(v_{i-1}\cdot v_{jump}+v_{add})\bmod n+1. Write xi=LCA(ui,vi)x_i=\mathrm{LCA}(u_i,v_i). Output only x1⊕x2⊕⋯⊕xqx_1\oplus x_2\oplus\cdots\oplus x_q, where ⊕\oplus denotes bitwise XOR.

Do not output individual query answers; qq can be as large as 10710^7.

Input

  • First line: n,qn,q.
  • Next n−1n-1 lines: undirected edges u,vu,v.
  • Last line: six values $u_{start},v_{start},u_{jump},v_{jump},u_{add},v_{add}$.

Output

Print one integer: the XOR of all query LCA results.

Subtasks

General constraints (all subtasks): 1≤n≤1051\le n\le 10^5; 1≤q≤1071\le q\le 10^7; 1≤ustart,vstart≤n1\le u_{start},v_{start}\le n; 0≤ujump,vjump,uadd,vadd≤1090\le u_{jump},v_{jump},u_{add},v_{add}\le 10^9.

  • Subtask 1 (20 points): 1≤n≤301\le n\le 30; 1≤q≤501\le q\le 50; jump and add parameters do not exceed 10910^9.
  • Subtask 2 (30 points): 1≤n≤10001\le n\le 1000; 1≤q≤1000001\le q\le 100000; jump and add parameters do not exceed 10910^9.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤q≤100000001\le q\le 10000000; jump and add parameters do not exceed 10910^9.

Examples

Input

3 2
1 2
1 3
3 3 1 1 1 1

Output

1

Explanation

The first query is (3,3)(3,3) with LCA 33. The second is (2,2)(2,2) with LCA 22. Their XOR is 3⊕2=13\oplus2=1.