( Problem A ) Prime Pairs

4.8

6 votes
Algorithms, Easy, Greedy algorithm
Problem

Alice likes pairs of integers. She calls two pairs of integers (a1,b1), (a2,b2) conflicting, if the absolute differences of the numbers within the pairs are the same, i.e. |a1b1|=|a2b2|. For example pairs (7,5) and (11,13) are conflicting, while (7,5) and (2,3) are not.

Bob likes primes. So, Alice wants to give him a list of pairs of primes as a present, hand-painted on a nice paper finished with a wooden frame. She wants to only use prime numbers between l and r, inclusive, and each at most once. She does not want to get uncomfortable when painting the present, so she absolutely must not use conflicting pairs. And finally, with these constraints in mind, she wants to make the largest gift possible, i.e. use the maximum amount of primes. 

Input format:

  • First line: t (number of test cases). Description of t test cases follows.
  • Next t lines: Contains the description of t test cases. Each test case is described in a single line containing two space-separated integers l and r, where 1lr106.

Output:

For each test case, print the answer in the following format:

  • First line: n (number of prime pairs that you can obtain). 
  • Next n lines: Here, the ith line should contain the pair (ai,bi). All numbers in pairs should be distinct primes between l and r, inclusive, and all the absolute differences must be distinct.

If there are multiple solutions that maximize the number of pairs, you can print any of them.

Scoring:

In all test sets, 1t10.

For the first test set, worth 14 points, it is guaranteed that there are no more than 11 primes in the interval [l,r].

For the second test set, worth 14 points, it is guaranteed that there are no more than 21 primes in the interval [l,r].

For the third test set, worth 16 points, it is guaranteed that there are no more than 42 primes in the interval [l,r].

For the fourth test set, worth 56 points, there are no additional constraints.

Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

There are 5 primes in the range [1,11] - 2,3,5,7 and 11. Alice has made two pairs: (2,5) and (11,3). The absolute differences within the pairs are |25|=3 and |113|=8, respectively. As 38, those pairs are not conflicting. It is clear that one cannot make 3 or more pairs using 5 items, so this is clearly one of the optimal solutions.

Editor Image

?