Playing with Numbers

All submissions for this problem are available.
### Read problem statements in [Hindi](http://www.codechef.com/download/translated/APRIL19/hindi/SJ1.pdf), [Bengali](http://www.codechef.com/download/translated/APRIL19/bengali/SJ1.pdf), [Mandarin Chinese](http://www.codechef.com/download/translated/APRIL19/mandarin/SJ1.pdf), [Russian](http://www.codechef.com/download/translated/APRIL19/russian/SJ1.pdf), and [Vietnamese](http://www.codechef.com/download/translated/APRIL19/vietnamese/SJ1.pdf) as well. You are given a rooted tree with $N$ nodes (numbered $1$ through $N$); node $1$ is the root. For each valid $i$, the $i$th node has a value $v_i$ and another parameter $m_i$. A leaf is a node without sons. Let's denote the number of leaf nodes in the tree by $L$ and their numbers by $l_1, l_2, \ldots, l_L$, in increasing order. Then, for each valid $i$, let's define the *answer* for the leaf $l_i$ in the following way:  Consider the path from the root to $l_i$. For each node on this path (including the root and this leaf), choose a nonnegative integer and multiply the value of this node by it.  Sum up the resulting values of all nodes on this path.  The answer $a_i$ for this leaf is defined as the maximum possible remainder of this sum modulo $m_{l_i}$. Find the answers for all leaves. ### Input  The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.  The first line of each test case contains a single integer $N$.  Each of the following $N1$ lines contains two spaceseparated integers $x$ and $y$ denoting that nodes $x$ and $y$ are connected by an edge.  The next line contains $N$ spaceseparated integers $v_1, v_2, \ldots, v_N$.  The last line contains $N$ spaceseparated integers $m_1, m_2, \ldots, m_N$. ### Output For each test case, print a single line containing $L$ spaceseparated integers $a_1, a_2, \ldots, a_L$. ### Constraints  $1 \le T \le 8$  $2 \le N \le 10^5$  $1 \le x, y \le N$  $1 \le v_i \le 10^{18}$ for each valid $i$  $1 \le m_i \le 10^{18}$ for each valid $i$  the graph described on the input is a tree ### Subtasks **Subtask #1 (100 points):** original constraints ### Example Input ``` 1 5 1 2 2 5 1 3 3 4 2 3 4 6 7 1 2 3 2 10 ``` ### Example Output ``` 0 9 ``` ### Explanation **Example case 1:** There are only two leaves, $l_1 = 4$ and $l_2 = 5$. The answers for them are $a_1 = 0$ and $a_2 = 9$.Author:  sj1397 
Editorial  https://discuss.codechef.com/problems/SJ1 
Tags  april19, easy, gcd, numbertheory, sj1397 
Date Added:  19032019 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, R, COB, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions