Sponsored
Use these hints if you're stuck. Try solving on your own first.
How to build the graph of the cities?
Connect city i with all its multiples 2*i, 3*i, ...
Answer the queries using union-find data structure.
Solve with full IDE support and test cases