Avoiding traps

4

5 votes
Problem

There is a cave of N cells where each cell has a trap or is safe to land. From a cell i, you can jump to cells i+1 or i+2. Also, if number i is special, then you can also jump from cell i to cell i+A where A=numberofprimesin[1,i]. A number i can be special if Air1r2.

You are given the following:

  • Some arbitrary numbers r1 and  r2
  • The number of cells N in the cave where each cell contains either '#' or '*'
  • Details of the cave

Note: Here, '#' represents an empty cell and '*' means a trapped cell.

Your task is to determine the minimum number of steps that you have to walk to reach the Nth cell. Initially, you are at cell 1.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each test case contains two integers  r1 and r2.
  • The second line of each test case contains an integer N.
  • The third line contains a string of length N representing N cells where the first character of the string represents the first cell, second character represents the second cell, and so on. Each cell is either '#' or '*'.

Output format

For each test case, print the minimum number of steps that you have to walk to reach the Nth cell. Print the output in a separate line.

If it is not possible to reach the  Nth cell, then print "Noway!" without quotes.

Constraints

1T500

1r1,r210000

1N100000

Note: For large input files, use faster input methods.

Sample Input
3
1 2
8
########
1 5
5
#***#
2 3
11
#*#*#*#*#*#
Sample Output
3
No way!
5
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

Note: 1-based indexing used below

1st Test Case:
In 3 jumps you can reach Nth cell. You can jump to index=3, then to index=5, then you could use
the special property and jump to index=8.

2nd Test Case:
There is No Way! to reach the Nth cell.

3rd Test Case:
In 5 jumps you can reach Nth cell. You can jump to index=3, then to index=5, then to index=7, then to index=9,  and then finally to index=11,

Editor Image

?