Problem 6

0

0 votes
Combinatorics, Recursion, Recruit, Number Theory, Easy-Medium, Ready, Mathematics, Approved
Problem

There are 2 criteria to make a difficult password:

1) Choose M numbers out of N given numbers:
s1 = summation of differences between every 2 adjacent M numbers.
For example: if the M numbers are: 2,5,8:
s1=52+85=6.

2) For given (L,R):
s2 = numbers in the interval [L,R] (both inclusive) that don't have divisors in the M numbers.

Write a program to choose M numbers out of N numbers to maximise the summation
of (s1+s2).

Input Format
First line contain T, that denotes number of test cases.
For each test case:
Second line contains 2 integers (N,M).
Third line contains 2 integers (L,R).
Fourth line contains N numbers.

Output Format
For each test case print the maximum value of (s1+s2) that could be achieved.

Input Constraints
1T50
1MN14
1LR109
1ai109

Time Limit: 4
Memory Limit: 256
Source Limit:
Explanation

The Optimal solution is to choose (3,7), M=2.
Result = difference between adjacent numbers + numbers that don't have divisors in the M numbers = 4 + 6 = 10.

Numbers that don't have divisors in the M numbers = (20,22,23,25,26,29).

Editor Image

?