Problem:


Score : 400 points

Problem Statement

You are given N items.

The value of the i-th item (1 \leq i \leq N) is v_i.

Your have to select at least A and at most B of these items.

Under this condition, find the maximum possible arithmetic mean of the values of selected items.

Additionally, find the number of ways to select items so that the mean of the values of selected items is maximized.  

Constraints

1 \leq N \leq 50

1 \leq A,B \leq N

1 \leq v_i \leq 10^{15}

Each v_i is an integer.

Input

The input is given from Standard Input in the following format:

N A B
v_1
v_2
...
v_N

Output

Print two lines.

The first line should contain the maximum possible arithmetic mean of the values of selected items. The output should be considered correct if the absolute or relative error is at most 10^{-6}.

The second line should contain the number of ways to select items so that the mean of the values of selected items is maximized.

Sample Input 1

5 2 2
1 2 3 4 5

Sample Output 1

4.500000
1

The mean of the values of selected items will be maximized when selecting the fourth and fifth items. Hence, the first line of the output should contain 4.5.

There is no other way to select items so that the mean of the values will be 4.5, and thus the second line of the output should contain 1.

Sample Input 2

4 2 3
10 20 10 10

Sample Output 2

15.000000
3

There can be multiple ways to select items so that the mean of the values will be maximized.

Sample Input 3

5 1 5
1000000000000000 999999999999999 999999999999998 999999999999997 999999999999996

Sample Output 3

1000000000000000.000000
1



Sample input:
5 2 2
1 2 3 4 5


Sample output:
4.500000
1

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
# mod = 10**9+7
mod = 170141183460469231731687303715884105727 
def inp(): return int(input()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls(): return list(input().split())  # a=['1','2','3',...,'n']
def inplm(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] # [[2,2,2,2],[1,1,1,1],[3,3,3,3]] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 

def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    return [w[0],w[1]]
def mod_inv(a,m):
    x = extgcd(a,m)[0]
    return x
    
def nCk(n,k):
    res = 1
    a=n-k
    b=k
    for i in range(1,a+b+1):
        res = res*i%mod
    for i in range(1,a+1):
        res = res*mod_inv(i,mod)%mod
    for i in range(1,b+1):                                 
        res = res*mod_inv(i,mod)%mod
    return res

def main():
    n,a,b = inpm()
    v = inpl()
    v.sort(reverse=True)
    sumation = 0
    for i in range(a):
        sumation += v[i]
    print(sumation/a)
    left = 0
    right = n-1
    for i in range(a-1,n-1):
        if v[i] != v[i+1]:
            right = i
            break
    for i in range(a-1,0,-1):
        if v[i] != v[i-1]:
            left = i
            break
    number = right - left + 1
    ans = 0
    if v[a-1] != v[0]:
        print(nCk(number,a-left))
        return
    if v[0] == v[a-1]:
        for e in range(a,min(b+1,number+1)):
            ans += nCk(number,e)
        print(ans)
    else:
        print(nCk(number,a-left+1))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import Counter
import math
n, a, b = map(int, input().split())
v = list(map(int, input().split()))
v.sort(reverse=True)
MAX = sum(v[:a]) / a
print(MAX)

c = Counter(v)
average_nums = []
for i in range(a, b + 1):
    t = sum(v[:i]) / i
    if t >= MAX:
        average_nums.append(Counter(v[:i]))

def nCr(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

ans = 0
for num in average_nums:
    multi = 1
    for k, v in num.items():
        multi = multi * nCr(c[k], v)
    ans += multi
print(ans)

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce
def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under
N, A, B = map(int, input().split())
v = [int(c) for c in input().split()]
v.sort(reverse=True)
m = 0
for i in range(A):
  m += v[i]
ans1 = m/A
ls = []
for j in range(A,B):
  m += v[j]
  if ans1!=m/(j+1):
    break
  ls += [j]
T = v[:A]
t = set(T)
dic1 = {}
dic2 = {}
for c in t:
  dic1[c] = T.count(c)
  dic2[c] = v.count(c)
ans2 = 1
for c in t:
  ans2 *= cmb(dic2[c],dic1[c])
for l in ls:
  dic1[v[l]] += 1
  ans2 += cmb(dic2[c],dic1[c])
print(ans1)
print(ans2)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getlist():
    return list(map(int, input().split()))
import heapq
import bisect
import copy
import math
from collections import defaultdict, Counter

def warshall_floyd(d, N):
    for k in range(N):
        for i in range(N):
            for j in range(N):
                d[i * N +j] = min(d[i * N +j], d[i * N + k] + d[k * N + j])
    return d
MOD = 10**9 + 7
INF = 10**10

def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def main():
    n,a,b = getlist()
    nums = getlist()

    cnt = Counter(nums)

    cnt = sorted(cnt.items(), key=lambda x: -x[0])
    # print(cnt)
    ans = 0
    tmp_max = 0
    res = 0
    for i in range(a, b+1):
        remain = i
        acc_val = 0
        for it in cnt:
            if it[1] <= remain:
                remain -= it[1]
                acc_val += it[1] * it[0]

            else:
                acc_val += remain * it[0]
                cmb = comb(it[1], remain)
                mean = acc_val / i
                res += comb(50, i)
                if mean == tmp_max:
                    ans += cmb
                elif mean > tmp_max:
                    tmp_max = mean
                    ans = cmb
                # print(mean, acc_val, ans, res)
                break

            if remain == 0:
                mean = acc_val / i
                if mean == tmp_max:
                    ans  += 1
                elif mean > tmp_max:
                    tmp_max = mean
                    ans = 1

                break
    print(tmp_max)
    print(ans)

    # res = 0
    # for i in range(1, 51):
    #     res += comb(50, i)
    #
    # print(res)


if __name__ == '__main__':
    main()

"""
9999
3

2916
"""

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce
from collections import Counter 
from itertools import accumulate

def comb(n, r):
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

n,a,b = map(int, input().split())
v = list(map(int, input().split()))
c = Counter(v)
keys = sorted(c.keys(), reverse=True)
acc = [0] + list(accumulate([c[k] for k in keys]))
costs = [0] + list(accumulate([c[k]*k for k in keys]))

d = []
for lim in range(a,b+1):
    for i,k in enumerate(keys):
        if acc[i+1] >= lim:
            d.append((costs[i] + (lim - acc[i]) * k, lim, comb(c[k], lim - acc[i])))
            break
e = sorted([[x[0] / x[1],x[2]] for x in d], reverse=True)
cnt = 0
mx = e[0][0]
i =0
while i < len(e) and mx <= e[i][0]:
    cnt += e[i][1]
    i += 1

print('{:.8f}'.format(mx))
print(cnt)

Submission status: Accepted

Solution code:
import math
n, a, b = map(int, input().split())
v = list(map(int, input().split()))
v.sort(reverse=True)
dict_num = dict()
def combi(x, y):
    return math.factorial(x) / (math.factorial(x - y) * math.factorial(y))
for i in v:
    if i in dict_num:
        dict_num[i] += 1
    else:
        dict_num[i] = 1
ans = 0
ans_max = 0.0
for i in range(a,b+1):
    k = v[:i]
    ans_max = max(ans_max, sum(k) / i)
for i in range(a,b+1):
    k = v[:i]
    if ans_max == (sum(k) / i) :
        p = 1
        index = 0
        while index < len(k):
            count = 1
            while index + 1 < len(k) and k[index] == k[index + 1]:
                count += 1
                index += 1
            p *= combi(dict_num[k[index]], count)
            index += 1
        ans += p
print(ans_max)
print(int(ans))

Submission status: Accepted

Solution code:
import sys
sys.stdin.readline

def main():
    N, A, B = map(int, input().split())
    V = list(map(int, input().split()))
    DP1 = [[0] * (N + 1) for _ in range((N + 1))]
    DP2 = [[0] * (N + 1) for _ in range((N + 1))]
    DP2[0][0] = 1
    for i in range(1, N+1):
        DP1[i][0] = DP1[i-1][0]
        DP2[i][0] = DP2[i-1][0]
        for j in range(1, N+1):
            if DP2[i-1][j]:
                if DP1[i-1][j] == DP1[i-1][j-1] + V[i-1]:
                    DP1[i][j] = DP1[i-1][j]
                    DP2[i][j] = DP2[i-1][j] + DP2[i-1][j-1]
                elif DP1[i-1][j] > DP1[i-1][j-1] + V[i-1]:
                    DP1[i][j] = DP1[i-1][j]
                    DP2[i][j] = DP2[i-1][j]
                else:
                    DP1[i][j] = DP1[i-1][j-1] + V[i-1]
                    DP2[i][j] = DP2[i-1][j-1]
            elif DP2[i-1][j-1]:
                DP1[i][j] = DP1[i-1][j-1] + V[i-1]
                DP2[i][j] = DP2[i-1][j-1]
    mx = max(DP1[N][n]/n for n in range(A, B+1))
    print(mx)
    print(sum(DP2[N][n] for n in range(A, B+1) if DP1[N][n]/n == mx))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline

from collections import defaultdict

N, A, B = map(int, input().split())
V = list(map(int, input().split()))
MOD = 10 ** 100 + 7

g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
 
def comb(n, r, mod):
  if r < 0 or r > n:
    return 0
  r = min(r, n - r)
  return g1[n] * g2[r] * g2[n - r] % mod
 
for i in range(2, 50 + 1):
  g1.append((g1[-1] * i) % MOD)
  inverse.append((-inverse[MOD % i] * (MOD // i) ) % MOD)
  g2.append((g2[-1] * inverse[-1]) % MOD)

V = sorted(V)[::-1]
counter = defaultdict(int)

answer_ave = sum(V[:A]) / A
print(answer_ave)

for v in V:
  if v >= V[A]:
    counter[v] += 1
  else:
    break
counter = list(counter.items())
counter = sorted(counter)[::-1]
 
if len(counter) == 1:
  answer = 0
  for i in range(A, min(B + 1, counter[0][1] + 1)):
    answer += comb(counter[0][1], i, MOD)
  print(answer)
else:
  take = 0
  for i in range(len(counter) - 1):
    take += counter[i][1]
  answer = comb(counter[-1][1], A - take, MOD)
  print(answer)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


MOD = int(1e09) + 7
INF = int(1e09)

nCr = {}

sys.setrecursionlimit(100000)


def comb(n, r):
    if r == 0 or r == n:
        return 1
    if r == 1:
        return n
    if (n, r) in nCr:
        return nCr[(n, r)]
    nCr[(n, r)] = comb(n-1, r) + comb(n-1, r-1)
    return nCr[(n, r)]


def main():
    # sys.stdin = open("sample.txt")
    N, A, B = Scanner.map_int()
    V = Scanner.map_int()
    V.sort(reverse=True)
    ans = 0
    for i in range(A):
        ans += V[i]
    ans /= A
    print(ans)

    vmin = V[A-1]
    if V[0] != V[A-1]:
        cnt = V.count(vmin)
        c = V[:A].count(vmin)
        print(comb(cnt, c))
        return

    c = V.count(vmin)
    cnt = 0
    for i in range(A, B+1):
        if V[i-1] != vmin:
            break
        cnt += comb(c, i)
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, A, B = map(int, input().split())

items = sorted(list(map(int, input().split())), reverse=True)

ans_lis = items[:A]
ans1 = sum(ans_lis)/A 
print(ans1)

i = A
ans2 = 0
while i <= B:
  if sum(items[:i])/i != ans1:
    break
  min_ = min(items[:i])
  min_in_ans = items[:i].count(min_)
  min_in_all = items.count(min_)

  nCr = {}
  def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]

  ans2 += cmb(min_in_all, min_in_ans)
  i += 1
print(ans2)

Submission status: Accepted

Solution code:
def COMB(a,b):
	if b > (a-b):
		b = a-b
	ans = 1
	for i in range(0,b):
		ans *= (a-i)
	for i in range(0,b):
		ans //= (b-i)

	return int(ans)

if __name__ == '__main__':
	N, A, B = map(int, input().split())
	v_lst = [int(p) for p in input().split()]

	v_lst.sort(reverse = True)

	count = 0

	for i in range(0,B-A+1):
		lst = v_lst[0:A+i]
		mean = sum(lst)/len(lst)
		if i == 0:
			nxt_mean = mean
		if mean == nxt_mean:
			a = v_lst.count(min(lst))
			b = lst.count(min(lst))
			count += COMB(a,b)
		else:
			break
		nxt_mean = mean

	print("{:.6f}".format(nxt_mean))
	print(count)

Submission status: Accepted

Solution code:
N, A, B = map(int, raw_input().split())
v = map(int, raw_input().split())

v.sort()
print "%.8f"%(float(sum(v[-A:]))/A)

x = v[-A]
ans = 0
for b in range(A, B+1):
  if v[-b]!=x:
    break
  if b!=A and v[-1]!=x:
    break
  n1 = v.count(x)
  n2 = v[-b:].count(x)
  a = 1
  for i in range(n2):
    a *= n1-i
  for i in range(n2):
    a /= i+1
  ans += a
print ans

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def nCr(n, r):
    return fac[n] // fac[r] // fac[n - r]

fac = [1]
for i in range(1, 51):
    fac.append(fac[-1] * i)

def main():
    n, a, b = MI()
    vv = LI()
    vv.sort(reverse=True)
    s = sum(vv[:a])
    tot = vv.count(vv[a - 1])
    l = vv[:a].count(vv[a - 1])
    if l == a:
        p = 0
        for r in range(l, min(tot,b) + 1):
            p += nCr(tot, r)
    else:
        p = nCr(tot, l)
    print(s / a)
    print(p)

main()

Submission status: Accepted

Solution code:
from math import factorial
def comb(n,r):
    return factorial(n) // factorial(r) // factorial(n - r)
N,A,B = map(int,input().split())
V = list(map(int,input().split()))
V.sort()
x = V[-1]
ans = 0
for i in range(1,A+1):
    ans += V[-i]
    x = V[-i]
ans = ans/A
print(ans)
count = 0
import bisect
y = N - bisect.bisect(V,x)
if x == V[-1]:
    n = V.count(x)
    for i in range(A,min(B,V.count(x))+1):
        z = i - y
        count += comb(n,z)
else:
    z = A - y
    n = V.count(x)
    count += comb(n,z)
print(count)

Submission status: Accepted

Solution code:
# D - Maximum Average Sets

N, A, B = map(int, input().split())
v = sorted(list(map(int, input().split())), reverse = True)

factorial_memo = [1]
for i in range(1,N+1):
    factorial_memo.append(factorial_memo[-1] * i)

def comb(n, r):
    return factorial_memo[n]//(factorial_memo[n-r] * factorial_memo[r])

print(sum(v[:A]) / A)
for i in reversed(range(A, N)):
    if v[i] == v[0]:
        print(sum([comb(i+1, x) for x in range(A, B+1)]))
        break
else:
    print(comb(v.count(v[A]), v[:A].count(v[A])))

Submission status: Accepted

Solution code:
import bisect
import math
from scipy.misc import comb

N,A,B=map(int,input().split())
List=list(map(int,input().split()))
List = sorted(List, reverse=True)

mini = List[A-1]
ans = 0

if List[A-1] != List[A]:
  ans = sum(List[:A])/A
  print(ans)
  print(1)

else:
  num = List.count(List[A-1])
  List = sorted(List)
  num2 = N - bisect.bisect_right(List,mini)
  List = sorted(List, reverse=True)
  
  if List[0] != List[A-1]:
    B = A
  for i in range(B-A+1):
    n = num
    kosuu = A + i - num2
    ans  = int(ans + comb(n, kosuu, exact=True))
  
  ave = int(sum(List[:A])/A)
  print(ave)
  print(ans)

Submission status: Accepted

Solution code:
# ABC057 D - Maximum Average Sets
from collections import defaultdict
from math import factorial
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))
def perm(n,r):
    return factorial(n)//factorial(n-r)
 
def comb(n,r):
    return perm(n,r)//factorial(r)

n,a,b=nm()
v=sorted(nl(), reverse=True)

if v[0]==v[a-1]:
    n = 0
    for vv in v:
        if vv!=v[0]: break
        n += 1

    ans = 0
    for k in range(a, min(b,n)+1):
        ans += comb(n, k)

else:
    n = k = 0
    for i,vv in enumerate(v):
        if vv==v[a-1]:
            if i<a: k += 1
            n += 1
    ans = comb(n,k)

print(sum(v[:a])/a)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N,A,B = map(int, input().split())

V = list(map(int, input().split()))

V.sort(reverse=True)

c = Counter(V)
# V[:A] は 逆順にA個取り出す
#print("V[:A]", V[:A])
max_average = sum(V[:A]) / A
#print("max_average", max_average)
# それぞれの要素の個数
c_max = Counter(V[:A])
#print("c_max", c_max)

# 組み合わせ
def nCr(n,r):
    if n < 0 or r < 0 or n < r: return 0
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n
  
    # 分子のn*(n-1)*...がr個分続くやつ
    numerator = [n-r+k+1 for k in range(r)]
    # 分母：r!=r*(r-1)*...*3*2の要素
    denominator = [k+1 for k in range(r)]
  
    # 分母の要素で割れる部分を割っていく部分
    for p in range(2, r+1):
        # 分母は1,2,3,...rのようになっており、1は意味がないのでスキップした形か
        pivot = denominator[p-1]
        if pivot > 1:
          # 分子のX番目と分母のX-offset番目が共通の約数を持つということだと思う。piv分ずれているのだから、pivの倍数というところか
            offset = (n-r) % p
            for k in range(p-1, r, p):
                # 約分できる要素について割る
                numerator[k - offset]  /= pivot
                denominator[k] /= pivot
        
    ret = 1
    for k in range(r):
        if numerator[k] > 1: ret *= int(numerator[k])
      
    return ret

# A個選んだ時の最大平均値が、全て同じ価値で構成されている時
# この場合は、全体の個数の取り方が、A〜min(B, V_maxの個数)と、複数ある
if len(c_max) == 1:
    # A個選ぶ、A+1個選ぶ、...、min(B, c[V[0]])個選ぶ　の和が答え
    ans = 0
    n = c[V[0]]
    for r in range(A, min(B, n)+1):
        ans += nCr(n,r)


# 最大平均値が複数の価値から作られる場合、価値が大きい順から全て使われる価値がいくつかと、中途半端に使われる価値が一つになる
# なので、中途半端に選ばれるものの選び方を数え上げる
# この時、全体の個数はA個固定（さらに何かを追加すると平均が減少するため）
else:
    
    # 最大平均値を構成する、最小の価値を探す
    tmp = list(c_max.keys())
    tmp.sort()
    # 最小の値
    min_num = tmp[0]
    # 最小以外の数字で何個選んでいるか
    cnt = 0
    for i in tmp[1:]:
        cnt += c_max[i]
    remaining = A - cnt

    ans = nCr(c[min_num], remaining)

print(max_average)
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result


n,a,b=map(int,input().split())
v=list(map(int,input().split()))
v.sort()
sm=sum(v[-a:])
av=sm/a
ky=v[-a]
c=v.count(ky)
co=0
for i in range(n):
    if v[-i-1]>ky:
        co+=1
ans=0
#print(v)
if co>0:
    ans=cmb(c,a-co)
else:
    for i in range(a,min(b+1,c+co+1)):
        ans+=cmb(c,i-co)
print(av)
print(ans)

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce

def cmb(n, r):
	if n < r: return 0
	if r == 0 or n - r == 0: return 1
	r = min(n - r, r)
	u = reduce(mul, range(n, n - r, -1))
	d = reduce(mul, range(1, r + 1))
	return u // d

n,a,b,*v=map(int,open(0).read().split())
v.sort(reverse=True)
print(sum(v[:a])/a)
c=v[a-1]
cnt=v.count(c)
d=sum(x>c for x in v)
if d:
	print(cmb(cnt,a-d))
else:
	print(sum(cmb(cnt,i) for i in range(a,min(cnt,b)+1)))

Submission status: Accepted

Solution code:
import math
#from scipy.special import comb
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2

N,A,B=map(int,input().split())
v=[int(i) for i in input().split()]
v.sort(reverse=True)
v=groupby(v)

if A<=v[0][1]:
    #こっちが間違ってる
    m=min(v[0][1],B)
    co=0
    for i in range(A,m+1):
        co+=combinations_count(v[0][1],i)
    print(v[0][0])
    print(co)
else:
    al=0
    l=len(v)
    C=0
    for i in range(l):
        C+=v[i][1]
        if C<=A:
            al+=(v[i][0]*v[i][1])
        else:
            C-=v[i][1]
            al+=(v[i][0]*(A-C))
            k=combinations_count(v[i][1],A-C)
            break
    print(al/A)
    print(k)

Submission status: Accepted

Solution code:
N, A, B = map(int,input().split())
V = list(map(int,input().split()))
V.sort(reverse = True)
Vs=V[:A]
print(sum(Vs)/A)
###################################
Vs = list(set(Vs))
Vs.sort(reverse = True)
###################################
com = [[0]*51 for i in range(51)]
for i in range(51):
    com[i][0] = 1
    for j in range(1, 51):
        com[i][j] = com[i-1][j-1] + com[i-1][j]
###################################
if len(Vs)!=1:
    N = V.count(Vs[-1])
    k = V[:A].count(Vs[-1])
    ans = com[N][k]
else:
    ans = 0
    N = V.count(Vs[0])
    for k in range(A,B+1):
        ans += com[N][k]
print(ans)

Submission status: Accepted

Solution code:
def cmb(n, r):
    if n - r < r: r = n - r
    if r < 0 : return 0
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result


n,a,b=map(int,input().split())
v=list(map(float,input().split()))
v=sorted(v,reverse=True)
V=0
for i in range(a):
  V=V+v[i]
ans1=V/a
print(ans1)
ans2=0
if v[0]==ans1:
  c=v.count(ans1)
  for i in range(a,b+1):
    ans2=ans2+cmb(c,i)
  print(ans2)
else:
  d=0
  e=0
  for i in range(a):
    if v[i]==v[a]:
      d=d+1
  for i in range(n):
    if v[i]==v[a]:
      e=e+1
  ans2=cmb(e,d)
  print(ans2)

Submission status: Accepted

Solution code:
nab=list(map(int,input().split()))
v=list(map(int,input().split()))
v.sort(reverse=True)
vmin=v[:nab[1]]
print(sum(vmin)/len(vmin))
mini=1
if nab[1]>1:
    for i in range(nab[1]-1):
        if vmin[-2-i]==vmin[-1]:
            mini+=1
        else:
            break
ex=0
for i in range(nab[0]-nab[1]):
    if v[nab[1]+i]==vmin[-1]:
        ex+=1
    else:
        break
def f(n):
    out=1
    for i in range(1,n+1):
        out*=i
    return out
ans=0
if vmin[0]!=vmin[-1]:
    ans=f(mini+ex)//(f(mini)*f(ex))
    print(ans)
else:
    m=min(nab[2]-nab[1],ex)
    for i in range(m+1):
        ans+=f(mini+ex)//(f(mini+i)*f(ex-i))
    print(ans)

Submission status: Accepted

Solution code:
#  Copyright: this code was written at 2020. for AtCoder by Silviase


def make_pascal_tri(n):
    array = [[1 for i in range(n + 1)] for i in range(n + 1)]
    for i in range(1, n + 1):
        for j in range(n + 1):
            if j > i:
                array[i][j] = 0
            elif j == 0 or j == i:
                array[i][j] = 1
            else:
                array[i][j] = array[i - 1][j] + array[i - 1][j - 1]
    return array


ar = make_pascal_tri(100)
n, a, b = map(int, input().split())
val = sorted(map(int, input().split()), reverse=True)
res_ave = 0
for i in range(a):
    res_ave += val[i]
res_ave /= a
grat_cnt = 0
same_cnt = 0
for i in range(n):
    if val[i] > val[a - 1]:
        grat_cnt += 1
    elif val[i] == val[a - 1]:
        same_cnt += 1
    else:
        break
result = 0

if grat_cnt == 0:
    for i in range(a, b + 1):
        result += ar[same_cnt][i]
else:
    result = ar[same_cnt][a - grat_cnt]

print(res_ave)
print(result)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def nCr(n, r):
    """ 事前テーブルなし組み合わせ簡易版 """
    from math import factorial

    if n < r: return 0
    # 10C7 = 10C3
    r = min(r, n-r)
    return factorial(n) // (factorial(r) * factorial(n-r))

N, L, R = MAP()
A = LIST()

A.sort(reverse=1)
C = Counter(A)
mx = cnt = 0
for i in range(L, R+1):
    A2 = A[:i]
    avg = sum(A2) / i
    # 平均がまだ最大なら続ける
    if avg >= mx:
        mx = avg
    else:
        break
    C2 = Counter(A2)
    # 最後の要素が複数あるなら交換可
    a = A2[-1]
    # 全体から必要な数を選ぶ通り数
    cnt += nCr(C[a], C2[a])
print(mx)
print(cnt)

Submission status: Accepted

Solution code:
N,A,B=map(int,input().split())
X=[int(x) for x in input().split()]
X.sort(reverse=True)
ave=sum(X[:A])/A
p=X[A-1]
c=0
o=0
for i in range(N):
  if X[i]==p:
    c +=1
  elif X[i]>p:
    o +=1
    

def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n
 
    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]
 
    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
 
    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])
 
    return result
answer=0
if not o==0:
  answer=cmb(c,A-o)
else:
  for i in range(A,min(c,B)+1):
    answer += cmb(c,i)
print(ave)
print(answer)

Submission status: Accepted

Solution code:
def comb(n, k):
    x = y = 1
    for i in range(k):
        x *= n-i
        y *= i+1
    return x//y

N, A, B = list(map(int, input().split()))
V = sorted(list(map(int, input().split())), reverse=True)

if V[0]==V[A-1]:
    n = 0
    for v in V:
        if v!=V[0]: break
        n += 1

    ans = 0
    for k in range(A, min(B,n)+1):
        ans += comb(n, k)

else:
    n = k = 0
    for i,v in enumerate(V):
        if v==V[A-1]:
            if i<A: k += 1
            n += 1
    ans = comb(n,k)

print(sum(V[:A])/A)
print(ans)

Submission status: Accepted

Solution code:
import statistics
import itertools

n, a, b = map(int, input().split())
v = sorted(list(map(int, input().split())), reverse=True)
mean = statistics.mean(v[:a])


# 組み合わせ数を数え上げる
def count_table(n):
    c = [[0 for _ in range(n+1)] for _ in range(n+1)]
    for i in range(n+1):
        for j in range(i + 1):
            if j == 0 or j == i:
                c[i][j] = 1
            else:
                c[i][j] = c[i-1][j-1] + c[i-1][j]
    return c


count_v_a = 0
pos_v_a = 0
for i in range(n):
    if v[i] == v[a-1]:
        count_v_a += 1
        if i < a:
            pos_v_a += 1

c = count_table(n)
count = 0
if pos_v_a == a:
    for pos in range(a, b + 1):
        count += c[count_v_a][pos]
else:
    count += c[count_v_a][pos_v_a]

print(mean)
print(count)

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

N,A,B=map(int,input().split())
V=list(map(int,input().split()))
V.sort()
av_max=0
count=0

for i in range(A,B+1):
    if sum(V[N-i:N])/i>av_max:
        av_max=sum(V[N-i:N])/i
        count=cmb(V.count(V[N-i]),V[N-i:N].count(V[N-i]))
    elif sum(V[N-i:N])/i==av_max:
        count+=cmb(V.count(V[N-i]),V[N-i:N].count(V[N-i]))

print(av_max)
print(count)

Submission status: Accepted

Solution code:
import decimal
def ncr(x,y):
	ans=1
	q=x-y
	while x>y:
		ans*=x
		x=x-1
	
	while q>1:
		ans=ans/q
		q=q-1
	
	return ans

x=input().split()
n=int(x[0])
a=int(x[1])
b=int(x[2])
v=[0]*n
x = input().split()
ind=0
M={1:0}
M2={1:0}
for i in x:
	v[ind]=int(i)
	if int(i) in M:
		M[v[ind]]+=1
	else:
		M[v[ind]]=1
	ind+=1
v.sort(reverse=True)

cnt=0
ind=0
m=0
while cnt<a:
	cnt+=1
	m=decimal.Decimal((cnt-1)*m+v[ind])/decimal.Decimal(cnt)
	if v[ind] in M2:
		M2[v[ind]]+=1
	else:
		M2[v[ind]]=1
	ind+=1
while cnt<b:
	if (cnt*m+v[ind])/(cnt+1)<m:
		break
	else:
		cnt+=1;
		if v[ind] in M2:
			M2[v[ind]]+=1
		else:
			M2[v[ind]]=1
		ind+=1
print(m)
maxw=cnt;
ways=0;
for i in range(int(cnt),a-1,-1):
	if v[ind-1] not in M2:
			M2[v[ind]]=0
	if v[ind-1] not in M:
			M[v[ind-1]]=0
	#print(M[v[ind-1]],M2[v[ind-1]])
	ways+=ncr(M[v[ind-1]],M2[v[ind-1]])
	
	M2[v[ind-1]]-=1

print(round(ways))