Codeforces Round 264 (Div. 2)
E
Caisa and Tree
Codeforces
2100
原题
← D→
翻译中…

Caisa is now at home and his son has a simple task for him.

翻译中…

Given a rooted tree with $*n*$ vertices, numbered from $1$ to $*n*$ (vertex $1$ is the root). Each vertex of the tree has a value. You should answer $*q*$ queries. Each query is one of the following:

翻译中…
  • Format of the query is "1 $*v*$". Let's write out the sequence of vertices along the path from the root to vertex $*v*$: $*u*_{1}, *u*_{2}, ..., *u*_{*k*}$ $(*u*_{1} = 1; *u*_{*k*} = *v*)$. You need to output such a vertex $*u*_{*i*}$ that $*gcd*(*value* *of* *u*_{*i*}, *value* *of* *v*) > 1$ and $*i* < *k*$. If there are several possible vertices $*u*_{*i*}$ pick the one with maximum value of $*i*$. If there is no such vertex output -1.
  • Format of the query is "2 $*v*$ $*w*$". You must change the value of vertex $*v*$ to $*w*$.
翻译中…

You are given all the queries, help Caisa to solve the problem.

翻译中…

Input

翻译中…

The first line contains two space-separated integers $*n*$, $*q*$ $(1 ≤ *n*, *q* ≤ 10^{5})$.

翻译中…

The second line contains $*n*$ integers $*a*_{1}, *a*_{2}, ..., *a*_{*n*}$ $(1 ≤ *a*_{*i*} ≤ 2·10^{6})$, where $*a*_{*i*}$ represent the value of node $*i*$.

翻译中…

Each of the next $*n* - 1$ lines contains two integers $*x*_{*i*}$ and $*y*_{*i*}$ $(1 ≤ *x*_{*i*}, *y*_{*i*} ≤ *n*; *x*_{*i*} ≠ *y*_{*i*})$, denoting the edge of the tree between vertices $*x*_{*i*}$ and $*y*_{*i*}$.

翻译中…

Each of the next $*q*$ lines contains a query in the format that is given above. For each query the following inequalities hold: $1 ≤ *v* ≤ *n*$ and $1 ≤ *w* ≤ 2·10^{6}$. Note that: there are no more than $50$ queries that changes the value of a vertex.

翻译中…

Output

翻译中…

For each query of the first type output the result of the query.

翻译中…

Note

翻译中…

$*gcd*(*x*, *y*)$ is greatest common divisor of two integers $*x*$ and $*y*$.

翻译中…

样例

Input #1
4 6
10 8 4 3
1 2
2 3
3 4
1 1
1 2
1 3
1 4
2 1 9
1 4
Output #1
-1
1
2
-1
1
InputCopy OutputCopy
题目信息
题号
463E
时间限制
10 s
内存限制
256 MB
标签
暴力
DFS 及类似
数学
数论
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。