All Tracks Algorithms Problem

Tree and Coprime Queries
Tag(s):

Algorithms, Data Structures, Hard, Prime Factorization, Trees

Problem
Editorial
Analytics

You are given a tree with $$N$$ nodes. Each node $$x$$ has a value $$V_x$$ associated with it.

You will also be given $$Q$$ queries of two types:

  • 0 a x : Set $$V_a$$ to $$x$$
  • 1 a b c : Find number of nodes on the unique path from $$a$$ to $$b$$ with value $$v$$ such that $$gcd(v, c) = 1$$

Input Format:

The first line contains two integers, $$N$$ and $$Q$$

The second line contains $$N$$ space separated integers, where the $$i^{th}$$ of them represents the value of node $$i$$.

Next $$N - 1$$ lines contain two integers $$a$$ and $$b$$, denoting that there is an undirected edge between nodes $$a$$ and $$b$$.

Next $$Q$$ lines contains a query in either of the above two formats.

Constraints:

$$1 \le N, Q \le 10^5$$

$$1 \le v \le 5000$$ where v is the value of any node at any given time

For query of type 0, $$1 \le a \le N$$ and $$1 \le x \le 5000$$

For query of type 1, $$1 \le a, b \le N$$ and $$1 \le c \le 5000$$

Output Format:

For each query of the second type, output as answer a single integer denoting the answer to that query.

Note: It is recommended to use faster I/O methods.

SAMPLE INPUT
5 5
1 2 3 4 5
1 2
2 3
3 4
2 5
1 1 3 2
1 1 4 2
0 1 2
1 1 3 2
1 1 4 2
SAMPLE OUTPUT
2
2
1
1
Explanation

For the first query, the values on the path are 1, 2, 3. Here 1, 3 have gcd equal to 1 with 2.

The third query updates node 1's value to 2.

For the fourth query, the values now are 2, 2, 3. Here only 3 has gcd equal to 1 with 2.

Time Limit: 6.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

HackerEarth

Challenge Name

February Clash '17

OTHER PROBLEMS OF THIS CHALLENGE
Notifications
View All Notifications