One Room to Another

2.1

8 votes
GCD, Graphs, Arrays, Algorithms, Graph Representation, Basic Math
Problem

Yatin created an interesting problem for his college juniors. Can you solve it?

Given rooms, where each room has a one-way door to a room denoted by , where . Find a positive integer such that, if a person starts from room , , and continuously moves to the room it is connected to (i.e. ) , the person should end up in room  after steps;

Note:      The condition should hold for each room.If there are multiple possible values of modulo (), find the smallest one.If there is no valid value of K, output  

Constraints: 

Input Format

  • The first line of the input contains an integer , the number of testcases.
  • For each Testcase:
    • The first line contains an integer , the number of rooms.
    • The second line contains N spaced integers 

Output Format

  • Output the smallest positive integer  modulo  , satisfying the above-mentioned conditions.
  • If there is no such value, output

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Given array 

If a person starts from

Path would be  , so person took 4 steps to reach room 1

Path would be  , so person took 4 steps to reach room 1

and so on for other rooms.

Similarly, it would give a value 4 for each case, And this is the smallest valid value, so for this case

 

Editor Image

?