410487: GYM104025 M Counting in Tree
Description
You are given a tree with nodes labeled from $$$1 \sim n$$$, and the root of the tree is vertex $$$1$$$. There are multiple queries, each of which gives one node $$$x$$$, and you need to find $$$$$$ \displaystyle\sum_{u\in T(x)}\displaystyle\sum_{\begin{subarray}{l}v\in T(x)\\ v<u\end{subarray}}[\gcd(u,v)=1] $$$$$$ where $$$T(x)$$$ denotes the subtree with root $$$x$$$.
$$$\gcd(u,v)$$$ denotes the greatest common divisor of $$$u$$$ and $$$v$$$.
The value $$$[\ast]$$$ is $$$1$$$ if $$$\ast$$$ is true, otherwise it is $$$0$$$.
InputThe first line contains two integers $$$n,q\ (1\leq n,q\leq 10^5)$$$.
The next line contains $$$n-1$$$ integers, the $$$i$$$-th of which shows the parent node of node $$$i+1$$$.
Each of the next $$$q$$$ lines contains an integer $$$x$$$, which shows the node $$$x$$$ to be queried.
OutputFor each query, output your answer as a single integer.
ExampleInput2 2 1 1 2Output
1 0