Important killers

3.8

4 votes
Combinatorics, Easy, Inclusion-Exclusion, Math
Problem

There is an infinite number of people standing in a line. Let the people be indexed by numbers from 1 and so on. Also, all people with indices less than or equal to P are important people, all others are unimportant.

Starting from the 2nd indexed person, these people start killing others in the line in a pattern. Every important person whose index is X, kills all people with indices X2,2×X2,3×X2, and so on. Every unimportant person whose index is X, kills all people with indices X,2×X,3×X, and so on.

Killing happens as follows:

1. It starts from the 2nd indexed person who kills everyone according to the rule given above.

2. The next person with the lowest index, who is yet not killed and whose index is a prime number, starts the same process again and kills everyone according to the rule above.

3. Repeat step 2.

Given an integer X, you need to find the index of Xth person alive. If there are less than X people alive, print 1.

Input:

The first line of input contains a single integer T, denoting the number of test cases. Each test case contains 2 integers P and X.

Output:

For each test case, print the index of Xth person alive. If there are less than X people alive, print 1.

Constraints:

1T5

1P50

1X32000

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

3rd person alive is the person at 3rd index and 4th person alive is the person at 5th index because the person at 4th index would be killed by the important person at index 2.

Editor Image

?