Advertising Agency (Codeforces 1475E)
Tags: Math, combinatorics

Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of $$$n$$$ different bloggers. Blogger numbered $$$i$$$ has $$$a_i$$$ followers.Since Masha has a limited budget, she can only sign a contract with $$$k$$$ different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.Help her, find the number of ways to select $$$k$$$ bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).For example, if $$$n=4$$$, $$$k=3$$$, $$$a=[1, 3, 1, 2]$$$, then Masha has two ways to select $$$3$$$ bloggers with the maximum total number of followers:   conclude contracts with bloggers with numbers $$$1$$$, $$$2$$$ and $$$4$$$. In this case, the number of followers will be equal to $$$a_1 + a_2 + a_4 = 6$$$.  conclude contracts with bloggers with numbers $$$2$$$, $$$3$$$ and $$$4$$$. In this case, the number of followers will be equal to $$$a_2 + a_3 + a_4 = 6$$$. Since the answer can be quite large, output it modulo $$$10^9+7$$$.

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow.The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 1000$$$) — the number of bloggers and how many of them you can sign a contract with.The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots a_n$$$ ($$$1 \le a_i \le n$$$) — the number of followers of each blogger.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$.

Output: For each test case, on a separate line output one integer — the number of ways to select $$$k$$$ bloggers so that the total number of their followers is maximum possible.

Note: The test case is explained in the statements.In the second test case, the following ways are valid:   conclude contracts with bloggers with numbers $$$1$$$ and $$$2$$$. In this case, the number of followers will be equal to $$$a_1 + a_2 = 2$$$;  conclude contracts with bloggers with numbers $$$1$$$ and $$$3$$$. In this case, the number of followers will be equal to $$$a_1 + a_3 = 2$$$;  conclude contracts with bloggers with numbers $$$1$$$ and $$$4$$$. In this case, the number of followers will be equal to $$$a_1 + a_4 = 2$$$;  conclude contracts with bloggers with numbers $$$2$$$ and $$$3$$$. In this case, the number of followers will be equal to $$$a_2 + a_3 = 2$$$;  conclude contracts with bloggers with numbers $$$2$$$ and $$$4$$$. In this case, the number of followers will be equal to $$$a_2 + a_4 = 2$$$;  conclude contracts with bloggers with numbers $$$3$$$ and $$$4$$$. In this case, the number of followers will be equal to $$$a_3 + a_4 = 2$$$. In the third test case, the following ways are valid:   concludes a contract with a blogger with the number $$$2$$$. In this case, the number of followers will be equal to $$$a_2 = 2$$$. 

Sample Inputs:
3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2

Sample Outputs:
2
6
1

Solution (Python):
import sys
from math import factorial
sys.setrecursionlimit(10 ** 6)

def ii():
    return int(sys.stdin.readline().strip())

def iil():
    return list(map(int, sys.stdin.readline().strip().split()))

def ist():
    return sys.stdin.readline().strip()

def istl():
    return sys.stdin.readline().strip().split()

def idigit():
    return [int(i) for i in list(sys.stdin.readline().strip())]

def ichar():
    return list(sys.stdin.readline().strip())
testcases = ii()
mod = pow(10, 9) + 7
dp = {}

def factorial(n):
    if n in dp:
        return dp[n]
    if n == 0:
        return 1
    dp[n] = n * factorial(n - 1)
    return dp[n]
for _ in range(testcases):
    (n, k) = iil()
    nums = iil()
    nums.sort(reverse=True)
    total_count = 0
    before_k_count = 0
    for (i, num) in enumerate(nums):
        if num == nums[k - 1]:
            if i <= k - 1:
                before_k_count += 1
            total_count += 1
    n = total_count
    r = before_k_count
    nf = factorial(n)
    rf = factorial(r)
    n_rf = factorial(n - r)
    print(nf // (rf * n_rf) % mod)

Solution (Python):
def ip():
    return input()

def ip_int():
    return int(input())

def ip_arr():
    return list(input().split())

def ip_int_arr():
    return list(map(int, input().split()))

def ip_int_tuple():
    return map(int, input().split())

def ip_tuple():
    return input().split()
from collections import defaultdict
import math
from heapq import *
mod = int(1000000000.0) + 7

def solve():
    (n, k) = ip_int_tuple()
    a = ip_int_arr()
    tot_freq = defaultdict(int)
    max_freq = defaultdict(int)
    max_heap = []
    for num in a:
        heappush(max_heap, -num)
        tot_freq[num] += 1
    max_sum = 0
    for _ in range(k):
        max_sum += -max_heap[0]
        max_freq[-max_heap[0]] += 1
        heappop(max_heap)
    ans = 1
    for (k, v) in max_freq.items():
        cur_max_freq = v
        cur_tot_freq = tot_freq[k]
        ans = ans * math.comb(cur_tot_freq, cur_max_freq) % mod % mod
    print(ans)
t = ip_int()
for _ in range(t):
    solve()

Solution (Python):
import math

def run():
    (n, k) = [int(x) for x in input().split()]
    a = [int(x) for x in input().split()]
    a.sort(reverse=True)
    last = a[k - 1]
    hi = len([x for x in a if x > last])
    curr = len([x for x in a if x == last])
    ans = math.comb(curr, k - hi)
    print(ans % (10 ** 9 + 7))
t = int(input())
for tt in range(t):
    run()