410487: GYM104025 M Counting in Tree

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

M. Counting in Treetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

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$$$.

Input

The 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.

Output

For each query, output your answer as a single integer.

ExampleInput
2 2
1
1
2
Output
1
0

加入题单

算法标签: