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:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A, B, V):
    V.sort(reverse=True)
    mean = sum(V[:A])/A

    print(mean)

    if A == N:
        return 1

    c = V[:A].count(V[A-1])
    d = V[A:].count(V[A-1])

    if d == 0:
        return 1

    @lru_cache(maxsize=None)
    def comb2(n, r):
        if n < 0 or r < 0 or n < r:
            return 0
        elif n == 0 or r == 0:
            return 1
        return comb2(n-1, r-1)+comb2(n-1, r)

    if V[0] == V[A-1]:
        return sum([comb2(c+d, i) for i in range(A, B+1)])

    return comb2(c+d, c)


def main():
    N, A, B = read_int_n()
    V = read_int_n()

    print(slv(N, A, B, V))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# nの逆元のリスト
def inv_mod(n:int, mod:int) -> list:
    inv = [-1,1]
    for i in range(2,n+1):
        inv.append(mod - ((mod//i)*inv[mod%i]) % mod)
    return inv
       
# nの階乗のリスト
def fact(n:int, mod:int) -> list:
    fac = [1,1]
    res = 1
    for i in range(2,n+1):
        res = res*i%mod
        fac.append(res)
    return fac

# nの階乗の逆元のリスト
def fact_inv(n:int, inv:list, mod:int) -> list:
    facInv = [1,1]
    for i in range(2,n+1):
        facInv.append(facInv[i-1]*inv[i] % mod)
    return facInv
        
# 二項係数
def nCr(n:int, r:int, mod:int, fac:list, facInv:list) -> int:
    if not (0<=r and r<=n):
        return 0
    
    return ((fac[n]*facInv[r]) % mod) * facInv[n-r] % mod

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

# 二項係数の準備
MOD = 9007199254740997
inv = inv_mod(N,MOD)
fac = fact(N,MOD)
facInv = fact_inv(N,inv,MOD)

V.sort(reverse=True)

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

ans = 0
if V[0] == V[A-1]:
    v = V.count(V[A-1])
    u = min(v,B)
    for i in range(A,u+1):
        ans += nCr(v,i,MOD,fac,facInv)

else:
    v_all = V.count(V[A-1])
    v_ch = V[:A].count(V[A-1])
    ans = nCr(v_all,v_ch,MOD,fac,facInv)

print(ans)

Submission status: Accepted

Solution code:
from copy import deepcopy
from math import factorial as fact
def combi(n, r):
    return fact(n)//(fact(n-r)*fact(r))

n, a, b = list(map(int, input().split()))
v = list(map(int, input().split()))
v_num = {}
for i in range(n):
    if v[i] not in v_num.keys():
        v_num[v[i]] = 1
    else:
        v_num[v[i]] += 1

v = sorted(v)
ans1 = v[-a:]
print(sum(ans1)/a)

answers = [ans1]
for i in range(a+1, b+1):
    if sum(v[-i:])/i == sum(ans1)/a:
        answers.append(v[-i:])
    else:
        break
# print(answers)

ans2 = 0
for ans in answers:
    ans_num = {}
    for i in range(len(ans)):
        if ans[i] not in ans_num.keys():
            ans_num[ans[i]] = 1
        else:
            ans_num[ans[i]] += 1
        
    a2 = 1
    for k in ans_num.keys():
        a2 *= combi(v_num[k], ans_num[k])
    ans2 += a2
    
print(ans2)

Submission status: Accepted

Solution code:
from collections import Counter

from scipy.misc import comb

N, A, B = map(int, input().split())
v = [int(x) for x in input().split()]

v = sorted(v, reverse=True)
m = sum(v[:A]) / A

c = Counter(v)
w = sorted(list(set(v[:A])), reverse=True)
if len(w) > 1:
    n = 0
    for x in w[:-1]:
        n += c[x]
    a = A - n
    b = A - n
    p = c[w[-1]]
else:
    a = A
    b = min(B, c[w[0]])
    p = c[w[0]]

ans = 0
for i in range(a, b + 1):
    ans += comb(p, i, exact=True)

print('{:f}'.format(m))
print(ans)

Submission status: Accepted

Solution code:
n,a,b = list(map(int,input().split()))

v = list(map(int,input().split()))
v.sort()
v.reverse()
print(sum(v[:a])/a)


def combination(n,r):
    a = 1
    b = 1
    for i in range(1, r+1):
        a *=n-i+1
        b *=i
    return a//b


c = v.count(v[a-1])
d = v[:a].count(v[a-1])
ans = 0
if v[0] != v[a-1]:
    print(combination(c,d))
else:
    for i in range(d,min(c,b-a+d)+1):
        ans += combination(c,i)
    print(ans)

Submission status: Accepted

Solution code:
from math import factorial
N, A, B = map(int, input().split())
v = [int(vi) for vi in input().split()]

def combination(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

v = sorted(v, reverse=True)
basket = v[:A]
max_basket = basket[0]
min_basket = basket[-1]
print(sum(basket) / len(basket))

n_selected = 0
n_candidate = 0
for vi in v:
    if vi > min_basket:
        n_selected += 1
    elif vi == min_basket:
        n_candidate += 1

if min_basket != max_basket:
    print(combination(n_candidate, A - n_selected))
else:
    count = 0
    for i in range(A, min(B, n_candidate) + 1):
        count += combination(n_candidate, i)
    print(count)

Submission status: Accepted

Solution code:
import math
import functools
N, A, B = map(int, input().split())
V = sorted([int(x) for x in input().split()], reverse=True)
print(sum(V[:A]) / A)
threshold = V[:A][-1]
fixed = 0
chosen = 0
for v in V:
  if v > threshold:
    fixed += 1
  elif v == threshold:
    chosen += 1
def nCn(a, b):
  return math.factorial(a) // math.factorial(b) // math.factorial(a - b)
if fixed:
  print(nCn(chosen, A - fixed))
else:
  count = 0
  for i in range(A, min(B, chosen) + 1):
    count += nCn(chosen, i)
  print(count)

Submission status: Accepted

Solution code:
N,A,B=[int(x) for x in str(input()).split()]
C=[int(s) for s in str(input()).split()]
j=C
v=C
v.sort()
ma=[]
mi=[]
for i in range(B):
  ma.append(v[-1])
  v=v[:len(v)-1]

for i in range(A):
  mi.append(j[-1])
  j=j[:len(j)-1]

mi.sort()
ma.sort()
ava=0
for i in ma:
  ava+=i

ava/=len(ma)


count=B
while count>A and ma[0]<ava:

  ava=(len(ma)*ava-ma[0])/(len(ma)-1)
  ma=ma[1:]
  count-=1


C1=0
for i in C:
  if i==ma[0]:
    C1+=1
mi1=0
for i in mi:
  if i==ma[0]:
    mi1+=1
ma1=0
for i in ma:
  if i==ma[0]:
    ma1+=1


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


ans=0
for i in range(mi1,ma1+1):
  ans+=factorial[C1]//factorial[C1-i]//factorial[i]
print(ava)
print(ans)

Submission status: Accepted

Solution code:
def combination(n, r):
    com = [[0 for j in range(r+1)] for i in range(n+1)]
    for i in range(n+1):
        com[i][0] = 1

    for i in range(1, n+1):
        for j in range(1, r+1):
            com[i][j] = com[i-1][j] + com[i-1][j-1]

    return com[n][r]


def main():
    n, a, b = map(int, input().split())
    v = list(map(int, input().split()))

    v.sort(reverse=True)

    num, s, i = [], [], 0
    while i < n:
        cnt = 1
        while i < n-1 and v[i] == v[i+1]:
            cnt += 1
            i += 1
        num.append(cnt)
        s.append(v[i])
        i += 1

    if num[0] >= a:
        com = 0
        for i in range(a, min(num[0]+1, b+1)):
            com += combination(num[0], i)
        print(v[0])
    else:
        c, idx = 0, 0
        while c < a:
            c += num[idx]
            idx += 1

        w, m = 0, 0
        for i in range(idx-1):
            w += v[i] * num[i]
            m += num[i]
        w += v[idx-1] * (a-m)

        print(w/a)

        com = combination(num[idx-1], a-m)

    print(com)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect
N, A, B = map(int, input().split())
V = list(map(int, input().split()))

V.sort()
s = sum(V[-A:])
k = V[-A]
l = bisect.bisect_left(V, k)
r = bisect.bisect_right(V, k)
p = N-A


def f(n):
    y = 1
    for i in range(2, n+1):
        y *= i
    return y


print(s/A)
if r == N:
    a = 0
    for i in range(A, min(B, N-l)+1):
        a += f(N-l)//f(i)//f(N-l-i)
    print(a)
else:
    print(f(r-l)//f(r-p)//f(p-l))

Submission status: Accepted

Solution code:
import math
def f(a,b):
    return math.factorial(a) // (math.factorial(a - b) * math.factorial(b))

n,a,b=map(int, input().split())
v=list(map(int, input().split()))
v.sort(reverse=True)
c=0
if v[0]==v[a-1]:
    for i in range(a,min(b,v.count(v[0]))+1):
        c+=f(v.count(v[a-1]),i)
    print(sum(v[:a])/a)
    print(c)
else: 
    print(sum(v[:a])/a)
    print(f(v.count(v[a-1]),v[:a].count(v[a-1])) )

Submission status: Accepted

Solution code:
from collections import Counter
from math import factorial as F

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

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

C = Counter(V)

#最低A個を獲得したときの平均測定
SUM = 0
LN = [0,0]
for i in range(A):
    if V[i] == LN[0]:
        LN[1] += 1
    else:
        LN[0] = V[i]
        LN[1] = 1
    SUM += V[i]
    
AVE = SUM/A

comb = 0
n = C[LN[0]]
if LN[1] == A: #AVEと同じものを取る
    for r in range(A,min(n,B)+1):
        comb += F(n) // (F(n-r)*F(r))
else:
    comb = F(n) // (F(n-LN[1])*F(LN[1]))
    
print("{0:.6f}".format(AVE))
print(comb)

Submission status: Accepted

Solution code:
def nCr(n, r):
    r = min(r, n-r)
    x, y = 1, 1
    for i in range(n, n-r, -1):
        x *= i
    for i in range(2, r+1):
        y *= i
    return x//y

n, a, b = map(int, input().split())
v = sorted(list(map(int, input().split())), reverse = True)
print(sum(v[:a])/a)
if(v[0] != v[a-1]):
    print(nCr(v.count(v[a-1]), v[:a].count(v[a-1])))
else:
    c = v.count(v[0])
    m = min(b, c)
    ans = 0
    for i in range(a, m+1):
        ans += nCr(c, i)
    print(ans)

Submission status: Accepted

Solution code:
from math import factorial as f

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

v.sort()


sum_v = 0
for i in range(N-A+1, N):
    sum_v += v[i]

i_max = N+1
count_max = 1
sum_max = 0

max_nums = []
if A == 1:
    current_num = -1
    current_count = 0
else:
    current_num = v[N-A+1]
    current_count = v[N-A+1:].count(v[N-A+1])
for i in range(N-A, N-B-1, -1):
    if v[i] == current_num:
        current_count += 1
    else:
        current_num = v[i]
        current_count = 1
    count = N-i
    sum_v += v[i]
    if sum_v * count_max > sum_max * count:
        count_max = count
        sum_max = sum_v
        max_nums = [(v[i], current_count)]
    elif sum_v * count_max == sum_max * count:
        if v[i] not in max_nums:
            max_nums.append((v[i], current_count))

def count(vi):
    return v.count(vi)

def c(n, k):
    return f(n)//(f(k)*f(n-k))

ans = 0
for vi, cc in max_nums:
    ans = ans + c(count(vi), cc)
    # print(vi, count(vi), cc)

print(sum_max/count_max)
print(ans)

Submission status: Accepted

Solution code:
from math import factorial

n, a, b = map(int, input().split())
v = list(map(int, input().split()))
v.sort(reverse=True)

n1 = v.count(v[a - 1])
n2 = v[:a].count(v[:a][a - 1])

print(sum(v[:a]) / a)
if len(set(v[:a])) != 1:
	if n1 == n2:
		print(1)
	else:
		print(int(factorial(n1) / (factorial(n2) * factorial(n1 - n2))))
else:
	ans = 0
	if n1 == 1:
		print(1)
	else:
		for i in range(a, min(n1 + 1, b + 1)):
			ans += int(factorial(n1) / (factorial(i) * factorial(n1 - i)))
		print(int(ans))

Submission status: Accepted

Solution code:
from math import factorial

n, a, b = map(int, input().split())
v = list(map(int, input().split()))
v.sort(reverse=True)

n1 = v.count(v[a - 1])
n2 = v[:a].count(v[:a][a - 1])

print(sum(v[:a]) / a)
if len(set(v[:a])) != 1:
	if n1 == n2:
		print(1)
	else:
		print(int(factorial(n1) / (factorial(n2) * factorial(n1 - n2))))
else:
	ans = 0
	if n1 == 1:
		print(1)
	else:
		for i in range(a, min(n1 + 1, b + 1)):
			ans += int(factorial(n1) / (factorial(i) * factorial(n1 - i)))
		print(int(ans))

Submission status: Accepted

Solution code:
N,A,B=map(int,input().split())
V=[int(x) for x in input().split()]
V.sort(reverse=True)
val=0
mi=10**15+1
cnt=0
for i in range(A):
  val+=V[i]
  if V[i]==mi:
    cnt+=1
  else:
    mi=V[i]
    cnt=1

#階乗計算
factorial=[1]
for i in range(1,N+1):
    factorial.append(factorial[i-1]*i)
    
def nCr(n,r):
  return int(factorial[n]/(factorial[r]*factorial[n-r]))    

ave=val/A
if ave == V[0]:
  for i in range(A,N):
    if V[i]==V[0]:
      cnt+=1
    else:
      break
  ans=0
  for i in range(A,min(B+1,cnt+1)):
    ans+=nCr(cnt,i)
  print(ave)
  print(ans)
else:
  res=cnt
  for i in range(A,N):
    if V[i]==V[A-1]:
      res+=1
  print(ave)
  print(nCr(res,cnt))

Submission status: Accepted

Solution code:
from math import factorial
n,a,b = (int(i) for i in input().split())
v = [int(i) for i in input().split()]
v = sorted(v)[::-1]
print(1.0*sum(v[:a])/a)
x,v2,num = v[a-1],v[:a][::-1],0
for i in v2:
	if i==x: num+=1
	else: break
y = v.count(x)
if num==a:
	answer,y2 = 0,factorial(y)
	for i in range(a,min(y,b)+1):
		answer += y2//(factorial(y-i)*factorial(i))
	print(answer)
else:
	print(factorial(y)//(factorial(y-num)*factorial(num)))

Submission status: Accepted

Solution code:
import numpy as np
import math
def ncm(n,m):
    return math.factorial(n)/(math.factorial(m) * math.factorial(n-m))

N, A, B = map(int, input().split())
v = list(map(int, input().split()))
v.sort(reverse=True)
v = np.array(v)
##print(v)
ansarray = v[0:A]
##print(ansarray)
ave = np.mean(ansarray)
print(float(ave))
mi = min(ansarray)
ma = max(ansarray)
biggerarray = v[v>mi]
m = len(biggerarray)
numminarray = v[v==mi]
a = len(numminarray)
##print(nummin)
##print(num_kouho)
ans = 0
if ma == mi:
    if m+a <= B:
        for i in range(A-m,a+1):
            ans+=ncm(a,i)
    else:
        for i in range(A-m,B-m+1):
            ans+=ncm(a,i)

else:
    ans = ncm(a,A-m)
#print('a=',a)
#print('m=',m)
print(int(ans))

Submission status: Accepted

Solution code:
import math

N, a, b = map(int, input().split())
product_list = sorted(map(int, input().split()), reverse = True)

def nCm(n, m):
    return math.factorial(n) / (math.factorial(m) * (math.factorial(n - m)))

# average_list
average_list = []
average_idx = []
for i in range(a, b+1):
    average = sum(product_list[:i]) / i
    average_list.append(average)
    average_idx.append(i)

max_average = max(average_list)
max_average_idx = [average_idx[i] for i in range(len(average_list)) if average_list[i] == max_average]

pattern_num = int(0)

for i in max_average_idx:
    choice_num = product_list[:i].count(product_list[i-1])
    all_num = product_list.count(product_list[i-1])

    pattern_num += int(nCm(all_num, choice_num))

print(float(max_average), pattern_num)

Submission status: Accepted

Solution code:
import scipy.misc as ms

n,a,b = map(int, raw_input().split())
v = map(int, raw_input().split())
v = sorted(v)[::-1]
sum = 0.0
for i in range(a): sum += v[i]
print "%.6f" % (sum / a)

m = v[a-1]
c = 0
al = 0
for i in range(a):
	if v[i] == m: c += 1
for i in range(n):
	if v[i] == m: al += 1

ans = 0
if int(sum / a) != m:
	print ms.comb(al, c, 1)
	exit(0)
for i in range(c, min(al, b) + 1):
	ans += ms.comb(al, i, 1)
print ans

Submission status: Accepted

Solution code:
def C(x,y):
    M=1
    for i in range(1,x+y+2):
        M=M*i
    for i in range(1,x+2):
        M=M//i
    for i in range(1,y+1):
        M=M//i
    return M


N, A, B = map(int, input().split())
v = [int(i) for i in input().split()]
v.sort(reverse=True)
v.append(0)
S=0
p=0
for i in range(A,B+1):
    T=sum(v[:i])/i
    if S<T:
        S=T
        p=0
        x=0
        y=0
        for j in range(i-1):
            if v[i-j-2]==v[i]:
                x+=1
            else:
                break
        for j in v[i:]:
            if j==v[i-1]:
                y+=1
            else:
                break
        #print(i,x,y,"a")
        p+=C(x,y)
    elif S==T:
        x=0
        y=0
        for j in range(i-1):
            if v[i-j-2]==v[i]:
                x+=1
            else:
                break
        for j in v[i:]:
            if j==v[i-1]:
                y+=1
            else:
                break
        #print(i,x,y,"b")
        p+=C(x,y)
print(S)
print(p)

Submission status: Accepted

Solution code:
import heapq
import copy
from collections import Counter

N,A,B=map(int, raw_input().split())
v=map(int, raw_input().split())
v.sort()
v.reverse()
c=Counter(v)

def C(n,r):
	bunshi=1
	bunbo=1
	for i in range(n,n-r,-1):
		bunshi*=i
	for i in range(1,r+1):
		bunbo*=i
	return bunshi/bunbo

ans=0
if A<=c[v[0]]:
	high=c[v[0]]
	for i in range(A,B+1):
		if i<=high:
			n=c[v[0]]
			r=i
			ans+=C(n,r)
	print v[0]
	print ans
	quit()
else:
	cnt=0
	t_sum=0
	cur=-1
	for i in v:
		cnt+=c[i]
		t_sum+=i*c[i]
		if cnt==A:
			n=A
			r=A
			break
		if cnt>A:
			n=c[i]
			r=A-(cnt-c[i])
			t_sum=t_sum-i*c[i]+i*r
			break

	print float(t_sum)/float(A)
	print C(n,r)

Submission status: Accepted

Solution code:
def comb(left, right):
    top = 1
    for i in range(left, left - right, -1):
        top *= i

    bottom = 1
    for i in range(1, right + 1):
        bottom *= i

    return top // bottom

n, a, b = map(int, input().split())
v = list(map(int, input().split()))

v.sort()

min_set = v[-a:]
print(sum(min_set) / len(min_set))
if len(set(min_set)) == 1:
    cnt = v.count(min_set[0])

    result = 0
    for i in range(a, min(cnt, b) + 1):
        result += comb(cnt, i)
    print(result)
else:
    print(comb(v.count(min_set[0]), min_set.count(min_set[0])))

Submission status: Accepted

Solution code:
from scipy.misc import comb
from decimal import *
import math
import copy

def nClr(n,l,r):
	ans = 0
	pat = 1
	if l == 0:
		ans += 1
		l += 1

	for i in range(l-1):
		pat *= ((n-i)/(i+1))

	for i in range(l-1,r):
		pat *= ((n-i)/(i+1))
		ans +=  pat

	return int(ans)

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

v.sort(reverse=True)

mean =float(sum(v[0:A]))/float(A)
whole = {}

for i in range(A):
	if v[i] in whole:
		whole[v[i]] += 1
	else:
		whole[v[i]] = 1

ran_A = copy.deepcopy(whole)

for i in range(A,N):
	if v[i] in whole:
		whole[v[i]] += 1
	else:
		whole[v[i]] = 1


if len(ran_A) == 1:
	n = min(B,whole[v[0]])
	num = nClr(whole[v[0]],A,n)
	print(mean)
	print(num)
	exit(0)


num = 1
for key,val in ran_A.items():
	if ran_A[key] != whole[key]:
		num *= comb(whole[key],ran_A[key])


print(mean)
print(int(round(num,0)))

Submission status: Accepted

Solution code:
def d_MaximumAverageSets(N, A, B, V):
    # パスカルの三角形でコンビネーション計算
    C = [[0] * (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]

    # 平均値の最大値を計算
    import decimal
    v = sorted(V, reverse=True)
    max_ave = decimal.Decimal(sum(v[:A]) / A)

    count = 0
    if v[0] != v[A - 1]:
        X = Y = 0
        for i in range(N):
            if v[A - 1] == v[i]:
                X += 1
                if i < A:
                    Y += 1
        count = C[X][Y]
    else:
        X = 0
        for i in range(N):
            if v[0] == v[i]:
                X += 1
        for i in range(A, B + 1):
            count += C[X][i]
    ans = '{}\n{}'.format(max_ave, count)
    return ans

N,A,B = [int(i) for i in input().split()]
V = [int(i) for i in input().split()]
print(d_MaximumAverageSets(N, A, B, V))

Submission status: Accepted

Solution code:
N, A, B = map(int, input().split())
v = list(map(int, input().split()))
v.sort(reverse=True)
ave = lambda a: sum(a) / len(a)
a = []
INF = int(1e9)

def C(n, m):
    ret = 1
    for i in range(m):
        ret *= n - i
    for i in range(2, m+1):
        ret //= i
    return ret

for i in range(A, B+1):
    a.append((ave(v[:i]), i))
ans1, i = max(a)
ans2 = 0
for b, j in a:
    if b != ans1:
        continue
    c1 = v[:j].count(v[j-1])
    c2 = v.count(v[j-1])
    ans2 += C(c2, c1)
print(ans1)
print(ans2)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))
from collections import Counter
from math import factorial

N, A, B = inpl()
V = sorted(inpl(), reverse=True)
C = Counter(V)

S = [sum(V[:i]) / i for i in range(1, N+1)]
M = max(S[A-1:B])
print(M)
T = [M==s if (A-1<=i)*(i<B) else False for i, s in enumerate(S)]

res = 0
for i in range(N):
    if T[i]:
        x, y = V.count(V[i]), V[:i+1].count(V[i])
        res += factorial(x) // factorial(y) // factorial(x-y)
print(res)

Submission status: Accepted

Solution code:
from math import factorial
from functools import reduce
def combinations(x, y):
    a = reduce(lambda x, y: x*y , [x - i for i in range(y)])
    b = factorial(y)
    return a//b
N, A, B = map(int,input().split())
v = sorted([int(vi) for vi in input().split()],reverse=True)
ans1 = sum(v[:A])/A

if v[0] != v[A-1]:
    X = sum([v[A-1] == v[i] for i in range(N)])
    Y = sum([v[A-1] == v[i] for i in range(A)])
    ans2 = combinations(X, Y)
else:
    X = sum([v[0] == v[i] for i in range(N)])
    ans2 = sum([combinations(X,i) for i in range(A, B+1)])

print(ans1)
print(ans2)

Submission status: Accepted

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

# main関数
if __name__ == '__main__':

    n, a, b = map(int,input().split())
    l = map(int, input().split())

    items = {}
    for i in l :
        if i in items:
            items[i] = items[i] + 1
        else:
            items[i] = 1

    oitems = sorted(items.items(), key=lambda x: x[0], reverse=True)

    num = 0
    sum = 0
    ans = 0
    for i in oitems :
        if num + i[1] > a:
            if num == 0 or sum / num == i[0] :
                print(i[0])
                for j in range (a - num, min(b - num, i[1])+1) :
                    ans += comb(i[1], j)
                print(int(ans))
                break 
            else: 
                print("{:.6f}".format((sum + (a - num) * i[0]) / a))
                print(int(comb(i[1],a - num)))
                break 
        else :
            num += i[1]
            sum += i[0] * i[1]

Submission status: Accepted

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

vs.sort(reverse=True)
print(sum(vs[:A]) / A)

iVA = vs.index(vs[A - 1])
numVA = vs.count(vs[A - 1])

comb = [0] * (numVA + 1)
comb[0] = 1
for k in range(1, numVA + 1):
    comb[k] = comb[k - 1] * (numVA - k + 1) // k

if vs[0] != vs[A - 1]:
    print(comb[A - iVA])
else:
    ans = 0
    for k in range(A - iVA, min(B, numVA) + 1):
        ans += comb[k]
    print(ans)

Submission status: Accepted

Solution code:
from itertools import combinations
N, A, B = [int(n) for n in input().split()]
v = [int(n) for n in input().split()]

v.sort(reverse=True)
s = sum(v[:A])
avg = s/A

print(s/A)

fac = [1]

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


def C(n, k):
  return fac[n]//(fac[k] * fac[n-k])


if v[0] != v[A-1]:
  cnt_a_1 = len([t for t in v if t == v[A-1]])
  cnt_a_1_in = len([t for t in v[:A] if t == v[A-1]])
  print(C(cnt_a_1, cnt_a_1_in))
if v[0] == v[A-1]:
  cnt_a_1 = len([t for t in v if t == v[A-1]])
  res = 0
  for i in range(A, B+1):
    res += C(cnt_a_1, i)
  print(res)

Submission status: Accepted

Solution code:
from collections import Counter
N,A,B = map(int,input().split())
src = list(map(int,input().split()))
src.sort(reverse=True)

ave = sum(src[:A]) / A
print(ave)

counter = Counter(src)
cnt = counter[src[0]]

ncr = [[1]]
for n in range(1,N+1):
    ncr.append([1])
    for r in range(1,n):
        ncr[n].append(ncr[n-1][r-1] + ncr[n-1][r])
    ncr[n].append(1)

if cnt < A:
    last = src[A-1]
    n = counter[last]
    r = src[:A].count(last)
    print(ncr[n][r])
elif cnt == A:
    print(1)
else:
    ans = 0
    for r in range(A, min(B,cnt)+1):
        ans += ncr[cnt][r]
    print(ans)

Submission status: Accepted

Solution code:
import math
from collections import Counter

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

v.sort(reverse = True)
cnt_v = Counter(v)
ave = []

for i in range(A, B+1):
    ave.append(sum(v[0:i]) / i)

max_ave = max(ave)
ans = 0

for j in range(B-A+1):
    temp = 1
    if ave[j] == max_ave:
        #print(j)
        l = v[:A+j]
        cnt_l = Counter(l)
        for key, values in cnt_l.items():
            temp *= (math.factorial(cnt_v[key]) // math.factorial(values) // math.factorial(cnt_v[key] - values))
        ans += temp

print(max_ave)
print(ans)

Submission status: Accepted

Solution code:
n,a,b=[int(x) for x in input().split()]
v=sorted([int(x) for x in input().split()],reverse=True)
from collections import Counter
c=Counter(v)
aveMax=0
avelist=[]
for i in range(a,b+1):
    avetmp=sum(v[:i])/i
    if avetmp>=aveMax:
        aveMax=avetmp
        avelist.append(Counter(v[:i]))
from math import factorial
def nCm(n,m):
    return factorial(n)//factorial(m)//factorial(n-m)
ans=0    
for i in avelist:
    multi=1
    for j in i:
        multi=multi*nCm(c[j],i[j])
    ans+=multi
print("{:.6f}".format(aveMax))
print(ans)

Submission status: Accepted

Solution code:
def makePascal(n):  # l[m][k] で mCk を取り出せるリストを返す
  res = [[1]]

  for m in range(1, n+1):
    l = []
    for k in range(m + 1):
      if k == 0 or k == m:
        l.append(1)
      else:
        tmp = res[m-1][k-1] + res[m-1][k]
        l.append(tmp)
    res.append(l)

  return res


comb = makePascal(50)
N, A, B = map(int, input().split())
v = list(map(int, input().split()))

v.sort(reverse = True)

ans1 = sum(v[:A]) / A
print(ans1)

if len(set(v[:A])) == 1:
  maxs = v.count(v[0])
  B = min(B, maxs)
  ans = 0
  for i in (range(A, B+1)):
    ans += comb[maxs][i]
  print(ans)
else:
  mini = min(v[:A])
  minis = v[:A].count(mini)
  ans = comb[v.count(mini)][minis]
  print(ans)

Submission status: Accepted

Solution code:
import scipy.misc as scm

def aCb(n, r):
    ans = 1
    for x in range(1, n + 1):
        ans *= x
    for x in range(1, n - r + 1):
        ans //= x
    for x in range(1, r + 1):
        ans //= x
    return ans

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

sorted_v = sorted(v)[::-1]

ans= sum(sorted_v[:A])/A

print(ans)


n = sorted_v.count(sorted_v[A-1]) 


if ans != sorted_v[A-1]:
    r = sorted_v[:A].count(sorted_v[A-1])
    print(int(aCb(n, r)))
    
else:
    C = 0
    for num in range(A, B+1):
#         print(num)
        if num <= n:
            C += (aCb(n, num))
        else :
            break
    print(int(C))

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import Counter

def aCb(n, r):
    ans = 1
    for x in range(1, n + 1):
        ans *= x
    for x in range(1, n - r + 1):
        ans //= x
    for x in range(1, r + 1):
        ans //= x
    return ans


def main():
    N, A, B = map(int, input().split())
    v_list = list(sorted(list(map(int, input().split()))))
    c_entire = Counter(v_list)
    total = sum(v_list[-A:])

    ans = 0
    for n in range(A, B + 1):
        p = v_list[-n:]
        if sum(p) * A == total * n:
            c = Counter(p)
            t = 1
            for k, v in c.items():
                t *= aCb(c_entire[k], v)
            ans += t

    print("{0:.7f}".format(total / A))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import factorial

n, a, b = map(int, input().split(" "));
v = list(map(int, input().split(" ")))

def product(iterable):
    prod = 1
    for n in iterable:
        prod *= n
    return prod

def npr(n, r):
    return product(range(n - r + 1, n + 1))

def ncr(n, r):
    if r > n // 2:
        r = n - r
    return npr(n, r) // factorial(r)

v.sort();
revv=list(reversed(v))
print(sum(v[-a:])/a)
revpos=revv.index(v[-a]);
num=v.count(v[-a]);
ans=0
if v[-1] == v[-a] == v[-a-1]:
    for i in range(a-revpos, b-revpos+1):
        if i > num:
            continue;
        ans+=ncr(num, i);
    print(ans);    
else:
    print(ncr(num,a-revpos));

Submission status: Accepted

Solution code:
import sys
from collections import Counter

# sys.stdin = open('d1.in')


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


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


def solve(n, a, b, v):
    c = Counter(v)
    res = 0
    m = max(v)
    if c[m] >= a:
        for k in range(a, b + 1):
            res += coef[c[m]][k]
        mean = m
        return mean, res
    else:
        values = sorted(set(v), reverse=True)
        s = 0
        r = a
        for value in values:
            if c[value] < r:
                r -= c[value]
                s += value * c[value]
            else:
                res = coef[c[value]][r]
                s += value * r
                break
        mean = s / a
        return mean, res


coef = [[0] * 51 for i in range(51)]
for i in range(0, 51):
    coef[i][0] = 1
    for k in range(1, i + 1):
        coef[i][k] = coef[i - 1][k - 1] + coef[i - 1][k]

n, a, b = read_int_list()
v = read_int_list()
mean, res = solve(n, a, b, v)
print(mean)
print(res)

Submission status: Accepted

Solution code:
N, A, B = map(int, raw_input().split())
V = map(float, raw_input().split())
V.sort(reverse=True)

idx = V.index(V[A-1])
k = V.count(V[A-1])
avr = sum(V[:A]) / A
print "%f" % avr

def fact(n):
    if n < 1:
        return 1
    else:
        return n * fact(n-1)

if idx > 0:
    y = A - idx
    print fact(k) / fact(y) / fact(k-y)
else:
    cnt = 0
    for i in range(A, B+1):
        cnt += fact(k) / fact(i) / fact(k-i)
    print cnt