Movement in Arrays

2.5

2 votes
Medium, Recursion, Recruit, Matrix Exponentiation, Approved, Backtracking
Problem

Let's consider an array A of size N. You start from the index 0 and your goal is to reach the N1 index in exactly M moves. At any index, you can move forward or backward by a number of steps equal to a prime divisor of the value which exists at that index. You can't go beyond the array while going forward or backward.

You have to output YES if it's possible to reach N1 index in exactly M moves, otherwise you have to output NO.

enter image description here

You can see in the example above that the value at current index is 6, prime divisors of which are 2 and 3. You can go forward or backward by 2 or 3 steps, as long as you are within the boundaries. For example, you can't go forward by 3 steps.

Input Format:
First line contains T which is the number of test cases.
Each test case contains 3 lines.
First line of every test case contains an integer N where N is the size of the array.
Second line of every test case contains the N values [Xii,Xj,...] which represents the array. Third line of each test case contains the number of moves M to reach the last index.

Output Format:
For each test case, output a line containing YES or NO depending on whether it is possible to reach in M moves.

Input Constraints:
1T10
2N40
1X106
1M106

Scoring according to difficulty of input constraints:
2N5, 1X10, 1M10: 30 pts
2N20, 1X103, 1M103: 30 pts
Original Constraints: 40 pts

Time Limit: 1
Memory Limit: 256
Source Limit:
Editor Image

?