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 math

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

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

cnt=v.count(v[0])
cnt2=v.count(v[A-1])
V=v[0:A]
cnt3=V.count(v[A-1])

def combinations_count(n, r):
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
        
if cnt<A:
    print(combinations_count(cnt2, cnt3))
elif cnt==A:
    print(1)
elif cnt>A:
    ans=0
    for i in range(A,min(cnt+1,B+1)):
        ans+=combinations_count(cnt,i)
    print(ans)

Submission status: Accepted

Solution code:
from math import factorial
from collections import defaultdict
N,A,B = map(int,input().split())
v = list(map(int,input().split()))
vd = defaultdict(int)
for i in range(N):
    vd[v[i]] += 1
vd=sorted(vd.items(),key=lambda x:x[0],reverse=True)
ave,i,cnt = 0,0,0
for v,c in vd:
    if A<=cnt+c:
        break
    cnt += c
    ave += v*c
    i += 1
ans = 0
if i==0:
    ave = vd[i][0]
    ub = min(B,vd[i][1])
    if vd[i][1]==A:
        for j in range(A-1,ub):
            ans += factorial(ub)//(factorial(ub-(j+1))*factorial(j+1))
    else:
        for j in range(A-1,ub):
            ans += factorial(vd[i][1])//(factorial(vd[i][1]-(j+1))*factorial(j+1))
else:
    ave += vd[i][0]*(A-cnt)
    ave /= A 
    ans = factorial(vd[i][1])//(factorial(vd[i][1]-(A-cnt))*factorial(A-cnt))
print(ave)
print(ans)

Submission status: Accepted

Solution code:
import math
#c = n! / (r! * (n - r)!)
def com(n,r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


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

v.sort(reverse=True)

anum=0
onum=0
for i in range(N):
    if v[i]==v[A-1]:
        anum+=1
    elif v[i]>v[A-1]:
        onum+=1
    if v[i]<v[A-1]:
        break
ans=0
if v[0]==v[A-1]:
    for i in range(A,min(B,anum)+1):
        ans+=com(anum,i)
#    print('aaa')
else:
    ans+=com(anum,A-onum)
    
print(sum(v[:A])/A)
print(ans)

Submission status: Accepted

Solution code:
def factrial(a):
    ans=1
    for i in range(2,a+1):
        ans*=i
    return ans

def comb(a,b):
    return factrial(a)/(factrial(a-b)*factrial(b))

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

arr=list(map(int,input().split()))
arr=sorted(arr,reverse=True)
mean_=sum(arr[:A])/A

top=arr.index(arr[A-1])
num=arr.count(arr[A-1])
ans=0
if arr[0]==arr[A-1]:
    for i in range(A,min(num,B)+1):
        ans+=comb(num,i)
else:
    ans=comb(num,A-top)

print(mean_)
print(int(ans))

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

g_comb_table ={}
def Comb(n, k):
    global g_comb_table
    if (n, k) in g_comb_table:
        return g_comb_table[(n, k)]
    if n == k or k == 0:
        g_comb_table[(n, k)] = 1
        return 1L
    elif k == 1:
        g_comb_table[(n, k)] = n
        return n
    g_comb_table[(n, k)] = Comb(n-1, k-1) + Comb(n-1, k)
    return g_comb_table[(n, k)]


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, A, B] = [long(input_raw[0]), long(input_raw[1]), long(input_raw[2])]
    input_raw = read_func().strip().split()
    v = []
    v_nums = {}
    for i in range(N):
        v.append(long(input_raw[i]))
        if v[i] in v_nums:
            v_nums[v[i]] += 1L
        else:
            v_nums[v[i]] = 1L

    v.sort(reverse = True)
##    s =[0 for i in range(N)]
##    s[0] = v[0]
##    for i in range(1, N):
##        s[i] = s[i -1] + v[i]

    max_ave_s = 0
    max_count = 0L
    sum_count = 0
    max_n = 0

    len_count = 0
    for n in range(A, B + 1):
        if v[n - 1] == v[0]:
            len_count+= 1

    if len_count == 0:
        len_count = 1

    s = 0L
    for i in range(A):
        s += v[i]
    max_ave = float(s)/A


    for n in range(A, A + len_count):
        last_num = -1
        last_num_count = 0L
        for i in range(n):
            if last_num != v[i]:
                last_num = v[i]
                last_num_count = 1L
            else:
                last_num_count += 1L
        max_count += Comb(v_nums[last_num], last_num_count)
##        if abs(ave - max_ave) <= 1e-8:
##            max_count += 1
##        elif max_ave < ave:
##            max_count = 1
##            max_ave = ave

    print "%.6f" %(max_ave)
    print max_count


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections
import math
N, A, B = map(int, input().split())
value = list(map(int, input().split()))
value.sort()
value.reverse()
valuetot = 0
for i in range(A):
    valuetot += value[i]
valueave = valuetot/A
value.append(0)
v = value[A-1]
C = collections.Counter(value)
vcount = C[v]

for i in range(A):
    if value[i] == v:
        vindex = i
        break

def permutation(n,r):
    r = min(r, n-r)
    numerator = 1
    for i in range(n, n-r, -1):
        numerator *= i
    denominator = 1
    for i in range(r, 1, -1):
        denominator *= i
    return numerator // denominator


def pattern(a):
    c = collections.Counter(value[:a])
    return permutation(vcount, c[v])

v0 = value[0]
v0len = 0
for i in range(N):
    if v0 == value[i]:
        v0len = i + 1
    else:
        break


if v0len >= A:
    ans = 0
    for i in range(A-1, B):
        if v0 == value[i]:
            ans += permutation(v0len, i+1)


elif value[A-1] != value[A]:
    ans = 1

else:
    ans = pattern(A)

print(valueave)
print(ans)

Submission status: Accepted

Solution code:
import math
N,A,B=map(int,input().split())
lst=list(map(int,input().split()))
lst.sort()
a=lst[-A:]
print(sum(a)/A)
b=lst.count(a[0])
c=a.count(a[0])
if c!=A:
    print(int(math.factorial(b)/(math.factorial(c)*math.factorial(b-c))))
else:
    l=[]
    i=min([b-c,B-A])
    for k in range(0,i+1):
        l.append(math.factorial(b)/(math.factorial(c+k)*math.factorial(b-c-k)))
    print(int(sum(l)))

Submission status: Accepted

Solution code:
import math
N,A,B = map(int,input().split())
v = list(map(int,input().split()))
d = {}
for value in v:
    if value not in d:
        d[value] = 1
    else:
        d[value] += 1
sortedDict = list(reversed(sorted(d.items(), key=lambda x: x[0])))
i = 0
s = 0
number = 0
while True:
    value,count = sortedDict[i]
    if A > count:
        A -= count
        B -= count
        i += 1
        s += count*value
        number += count
    else:
        s += A*value
        number += A
        break
        # A<=countが保証される
ans = 0
def combination(n,k):
    return (math.factorial(n)//math.factorial(k)//math.factorial(n-k))
if i == 0:
    upper = min(count,B)
    for j in range(A,upper+1):
        ans += combination(count,j)
else:
    ans = combination(count,A)
average = s/number
print(average)
print(ans)

Submission status: Accepted

Solution code:
def fact(n):
    if n == 0:
        return 1
    else:
        return n*fact(n-1)
def conb(n,r):
    return fact(n)//(fact(r)*fact(n-r))

N,A,B = map(int,input().split())
v = list(map(int,input().split()))
v.sort()
v.reverse()
d_all = {}
for i in range(N):
    if v[i] not in d_all:
        d_all[v[i]] = 0
    d_all[v[i]] += 1
import numpy as np
avg = np.cumsum(np.array(v))
avg = [0]*N
avg[0]=v[0]
for i in range(N-1):
    avg[i+1]=avg[i]+v[i+1]
for i in range(N):
    avg[i]=avg[i]/(i+1)
    
avg = avg[A-1:B]
ans = max(avg)
print(ans)
avg=np.array(avg)
indices = np.where(avg==ans)[0]
ans = 0
for index in indices:
    d_use = {}
    use = A + index
    v_use = v[:use]
    choose=1
    for item in v_use:
        if item not in d_use:
            d_use[item] = 0
        d_use[item] += 1
    for item in d_use:
        choose *= conb(d_all[item],d_use[item])
    ans += choose

print(ans)

Submission status: Accepted

Solution code:
def inpl(): return [int(i) for i in input().split()]
def comb(a,b):
    return math.factorial(a)//math.factorial(a-b)//math.factorial(b)

N, A, B = inpl()
V = inpl()
V = list(reversed(sorted(V)))
x1 = N-1
x2 = 0
for i in range(N):
    if V[i] == V[A]:
        x1 = min(x1,i)
        x2 = max(x2,i)

print(sum(V[0:A])/A)
import math
ans = 0
if x1 == 0:
    for l in range(A-1,min(B,x2+1)):
        ans += comb(x2-x1+1,l-x1+1)
    print(ans)
else:
    print(comb(x2-x1+1,A-x1))

Submission status: Accepted

Solution code:
n,a,b=map(int,input().split())
v=list(map(int,input().split()))
v.sort()
v=v[::-1]
print(sum(v[:a])/a)
def com(x,y):
    a=1
    b=1
    y=min(y,x-y)
    for i in range(x,x-y,-1):
        a*=i
    for i in range(1,y+1):
        b*=i
    return a//b
ans=0
x=v.count(v[a-1])
y=v[:a].count(v[a-1])
if v[0]!=v[a]:
    ans+=com(x,y)
else:
    for j in range(a,min(b,x)+1):
        ans+=com(x,j)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
from scipy.misc import comb

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

v.sort(reverse=True)
v_cum = []
num = 0
for i in range(n):
    num += v[i]
    v_cum.append(num)

print(v_cum[a-1]/a)

if a == n:
    print(1)
else:
    if v[a-1] == v[a]:
        num = v[a-1]
        cf = Counter(v[:a])
        cb = Counter(v[a:])
        n_cf = cf[num]
        n_cb = cb[num]
        if n_cf != a:
            print(comb(n_cf+n_cb, n_cf, exact=True))
        else:
            ans = 0
            for k in range(a, min(b+1, a+n_cb+1)):
                ans += comb(a+n_cb, k, exact=True)
            print(ans)
    else:
        print(1)

Submission status: Accepted

Solution code:
# ABC 057 D
import math
N, A, B = map(int,input().split())
v = sorted(list(map(int,input().split())))[::-1]

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

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

s = 0
for k in range(N):
    if v[A-1] == v[k]:
        s += 1
t = 0
for k in range(A):
    if v[A-1] == v[k]:
        t += 1
u = 0
for k in range(A-1,B):
    if v[A-1] == v[k]:
        u += 1

if a != v[A-1]:
    print(nCr(s,t))
else:
    c = 0
    for k in range(A,A+u):
        c += nCr(s,k)
    print(c)

Submission status: Accepted

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

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

x = v.count(v[A - 1])
y = v[:A].count(v[A - 1])
if v[0] != v[A - 1]:
    u = 1
    d = 1
    for i in range(x, x - y, -1):
        u *= i
    for i in range(y, 0, -1):
        d *= i
    print(u // d)
else:
    ans = 0
    for i in range(A, min(B, x) + 1):
        u = 1
        d = 1
        for j in range(x, x - i, -1):
            u *= j
        for j in range(i, 0, -1):
            d *= j
        ans += u // d
    print(ans)

Submission status: Accepted

Solution code:
def main():
    S = list(input())

    checklist = [0 for i in range(26)]

    for i in S:
        checklist[ord(i) - ord("a")] += 1

    ans = True
    for i in checklist:
        if i > 1:
            ans = False

    if not ans:
        print(-1)
        return 0
    else:
        ans = False
        for i in range(len(checklist)):
            if checklist[i] == 0:
                for i in S:
                    print(i, end="")
                print(chr(i + ord("a")), end = "")
                print()
                ans = True
                break
        if not ans:
            new_S = []
            for i in S:
                new_S.append(ord(i))
            for i in range(len(new_S) - 1):
                if new_S[i] > new_S[i + 1]:
                    for j in range(i - 1):
                        print(S[j], end = "")
                    print(chr(new_S[i - 1] + 1), end = "")
                    print()
                    ans = True
                    break
            if not ans:
                print(-1)
#main()

def ABC069D():

    H, W = map(int, input().split())
    N = int(input())
    a = list(map(int, input().split()))
    ans = []
    for i in range(N):
        for j in range(a[i]):
            ans.append(i + 1)
    printlist = [[0 for i in range(W)] for j in range(H)]
    k = 0
    for i in range(H):
        for j in range(W):
            printlist[i][j] = ans[k]
            k += 1
        if i % 2 != 0:
            printlist[i] = printlist[i][::-1]

    for i in range(H):
        for j in range(W):
            print(printlist[i][j], end = "")
            if j != W:
                print(" ", end = "")
        print()
    print()
#ABC069D()

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


def ABC057D():

    N, A, B = map(int, input().split())
    v = list(map(int, input().split()))
    v = sorted(v)[::-1]

    anslist = []
    for i in range(A, B + 1):

        anslist.append([sum(v[0:i]) / i, i, v[i - 1]])
    anslist.sort()
    anslist = anslist[::-1]
    ans = anslist[0][0]
    key = anslist[0][2]
    long = anslist[0][1]
    print(ans)
    for i in range(1, len(anslist)):
        if anslist[i][0] == anslist[i - 1][0]:
            ans = anslist[i][0]
            key = anslist[i][2]
            long = anslist[i][1]
        else:
            break
    #print(anslist)
    #print(ans, key, long)
    cnt = 0
    for i in v:
        if i == key:
            cnt += 1

    c = 0
    for i in v[:long]:
        #print(i)
        if i == key:
            c += 1
    #print(c, long)
    ans = 0
    if c == long:
        for i in range(A,  min(B + 1, cnt + 1)):
            ans += cc(cnt, i)
    else:
        ans = cc(cnt, c)
    #print(anslist)
    #print(cnt, c, long)
    print(ans)
ABC057D()

Submission status: Accepted

Solution code:
import math
def cc(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


def ABC057D():

    N, A, B = map(int, input().split())
    v = list(map(int, input().split()))
    v = sorted(v)[::-1]

    anslist = []
    for i in range(A, B + 1):

        anslist.append([sum(v[0:i]) / i, i, v[i - 1]])
    anslist.sort()
    anslist = anslist[::-1]
    ans = anslist[0][0]
    key = anslist[0][2]
    long = anslist[0][1]
    print(ans)
    for i in range(1, len(anslist)):
        if anslist[i][0] == anslist[i - 1][0]:
            ans = anslist[i][0]
            key = anslist[i][2]
            long = anslist[i][1]
        else:
            break
    #print(anslist)
    #print(ans, key, long)
    cnt = 0
    for i in v:
        if i == key:
            cnt += 1

    c = 0
    for i in v[:long]:
        #print(i)
        if i == key:
            c += 1
    #print(c, long)
    ans = 0
    if c == long:
        for i in range(A,  min(B + 1, cnt + 1)):
            ans += cc(cnt, i)
    else:
        ans = cc(cnt, c)
    #print(anslist)
    #print(cnt, c, long)
    print(ans)
ABC057D()

Submission status: Accepted

Solution code:
def Comb(i, j, memo):
    if i == j:
        return 1
    elif j == 1:
        return i
    try:
        return memo[(i, j)]
    except KeyError:
        ans = Comb(i-1, j-1, memo) + Comb(i-1, j, memo)
        memo[(i,j)] = ans
        return ans

N, A, B = map(int, input().split())
V = [int(i) for i in input().split()]
V.sort(reverse = True)
av = 0
for i in range(A):
    av += V[i]
av = round(av/A, 6)

memo = {}
if V[0] == V[A-1]:
    lim = min(V.count(V[0]), B)
    ans = 0
    for j in range(A, lim+1):
        ans += Comb(V.count(V[0]), j, memo)
else:
    ans = Comb(V.count(V[A-1]), V[:A].count(V[A-1]), memo)

print(av)
print(ans)

Submission status: Accepted

Solution code:
import sys
import math

input = sys.stdin.readline
n, a, b = map(int, input().split())
v_list = list(map(int, input().split()))
v_list.sort(reverse=True)
mean = sum(v_list[:a]) / a
check = v_list[a-1]
before = 0
after = 0
before_list = v_list[:a-1]
before_list.sort()
after_list = v_list[a:]
for num in before_list:
    if check == num:
        before += 1
    else:
        break
for num in after_list:
    if check == num:
        after += 1
    else:
        break
ans = 0
comb_n = before + after + 1
if a - 1 == before:
    for i in range(min(after+1, b-a+1)):
        comb_r = before + i + 1
        ans += math.factorial(comb_n) // (math.factorial(comb_n - comb_r) * math.factorial(comb_r))
else:
    comb_r = before + 1
    ans += math.factorial(comb_n) // (math.factorial(comb_n - comb_r) * math.factorial(comb_r))
print(mean)
print(ans)

Submission status: Accepted

Solution code:
from math import factorial
from collections import Counter
def combination(a,b):
    if a < b:
        a, b = b, a
    comb = 1
    for i in range(b):
        comb *= a-i
    comb //= factorial(b)
    return comb

N, A, B = map( int, input().split())
V = list( map( int, input().split()))
V.sort(reverse = True)
print( sum(V[:A])/A)
S = list( set(V))
S.sort(reverse = True)
CV = Counter(V)
ans = 0
K = CV[S[0]]
if A <= K:
    for i in range(A, min(K, B)+1):
        ans += combination(K, i)
else:
    cnt = A
    for x in S:
        if cnt <= CV[x]:
            ans += combination(CV[x],cnt)
            break
        else:
            cnt -= CV[x]
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from statistics import mean
from math import factorial as fact

n,a,b = map(int, input().split())
v = sorted([int(x) for x in input().split()], reverse=True)
sel = v[:a]
print(mean(sel))

vn = v.count(sel[-1])
na = sel.count(sel[-1])
nb = min(b-a, vn-na)
# print(na,nb,vn,len(sel),sel[-1])

if na!=len(sel):
    print(fact(vn)//(fact(na)*fact(vn-na)))
elif nb==0:
    print(1)
else:
    ret = 0
    for bi in range(na,na+nb+1):
        ret += fact(vn)//(fact(bi)*fact(vn-bi))
    print(ret)

Submission status: Accepted

Solution code:
import math

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

n, a, b = map(int, input().split())
v = sorted(list(map(int, input().split())), reverse=True)
ans = 0
ma = sum(v[:a]) / a
for i in range(a, b + 1):
    ave = sum(v[:i]) / i
    if ave == ma:
        p = 1
        for j in set(v[:i]):
            p *= nCr(v.count(j), v[:i].count(j))
        ans += p
print(ma)
print(ans)

Submission status: Accepted

Solution code:
import bisect
n,a,b = map(int,input().split())
v = list(map(int,input().split()))	
v.sort()
v = v[::-1]

ans = 0
for i in range(a):
    ans += v[i]
print(ans/a)
v = v[::-1]
k = n-bisect.bisect_left(v,v[n-a])
l = n-bisect.bisect_right(v,v[n-a])
ww = k
k = l
l = ww
#print(k,l)
jkl = [1]
for i in range(1,l+1):
    jkl.append(jkl[-1]*i)

ass = 0

if l -k==0:
    print(1)
elif k != 0:
    ass += jkl[l-k]//jkl[a-k]//jkl[l-a]
    print(ass)
else:
    for i in range(a,min(b,l)+1):
        ass += jkl[l]//jkl[i]//jkl[l-i]
    print(ass)

Submission status: Accepted

Solution code:
N,A,B = map(int,input().split())
v = list(map(int,input().split()))
v.sort()
v.reverse()
print(sum(v[:A]) / A)

def fuct(n):
	ret = 1
	while n > 1:
		ret *= n
		n -= 1
	return ret

def P(n,k):
	return fuct(n) // fuct(n-k)

def C(n,k):
	return P(n,k) // fuct(k)

if v[0] != v[A-1]:
	num = v.count(v[A-1])
	pick = 0
	for i in range(A):
		if v[i] == v[A-1]:
			pick += 1
	print(C(num, pick))
else:
	num = v.count(v[A-1])
	ans = 0
	for i in range(A, min(num+1, B+1)):
		ans += C(num, i)
	print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def nCr(n: int, r: int):
    dp = [0] * (n + 1)
    for i in range(n + 1):
        pre = 1
        for j in range(i + 1):
            if j == 0 or j == i:
                dp[j] = 1
            else:
                temp, dp[j] = dp[j], dp[j] + pre
                pre = temp

    return dp[r]


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

    use = V[:A]

    c1 = Counter(V)
    c2 = Counter(use)
    ave = sum(use) / A
    print("{0:.7f}".format(ave))

    if len(set(use)) == 1:
        ans = 0
        num = c1[use[0]]
        for n in range(A, min(B, num) + 1):
            ans += nCr(num, n)
        print(ans)

    else:
        num_total = c1[V[A]]
        num_use = c2[V[A]]
        print(nCr(num_total, num_use))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

# nの逆元のリスト
def inv_mod(n:int, mod:int) -> list:
    inv = [0,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

from itertools import accumulate
from collections import Counter

n,a,b = li()
v = li()

v.sort(reverse=True)
v_cum = list(accumulate(v))

ans_cand = []

# 貪欲に最大値を選ぶ
for i in range(a,b+1):
    ans_cand.append(v_cum[i-1] / i)
    
ans = max(ans_cand)
opt_i = a + ans_cand.index(ans) - 1

cnt_v = Counter(v)
opt_set = v[:opt_i+1]

# 貪欲最大が何通りできるか考える
MOD = 46116646144580591
inv = inv_mod(n, MOD)
fac = fact(n, MOD)
facInv = fact_inv(n,inv, MOD)

pat = 0

if v[0] == v[opt_i]:
    for i in range(a, min(b, cnt_v[v[0]]) + 1):
        pat += nCr(cnt_v[v[0]], i, MOD, fac, facInv)

else:
    cnt_opt = Counter(opt_set)
    pat = nCr(cnt_v[v[opt_i]], cnt_opt[v[opt_i]], MOD, fac, facInv)


        
print(ans)
print(pat)

Submission status: Accepted

Solution code:
n,a,b = map(int,input().split())
vs = sorted([int(v) for v in input().split()],reverse=True)
print(sum(vs[:a])/a)
binom = [1]
for k in range(vs.count(vs[a-1])):
    binom = [x+y for (x, y) in zip(binom+[0],[0]+binom)]
if vs[a-1] == vs[0]:
    print(sum(binom[a:b+1]))
else:
    print(binom[a-vs.index(vs[a-1])])

Submission status: Accepted

Solution code:
from functools import lru_cache
from collections import Counter

@lru_cache()
def comb(a,b):
  if not a>=b>=0:
    return 0
  if b==1:
    return a
  elif b==0:
    return 1
  return comb(a-1,b)+comb(a-1,b-1)

n,a,b=map(int,input().split())
v=sorted(map(int,input().split()))[::-1]

tg=v[:a]
avg=sum(tg)/a
ans=0

e=Counter(v)
for i in range(a,b+1):
  tmp=1
  tg=v[:i]
  d=Counter(tg)
  if sum(tg)/i!=avg:break
  for i in d.keys():
    tmp*=comb(e[i],d[i])
  ans+=tmp

print(avg)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Sep  2 17:29:52 2018
ABC057D
@author: maezawa
"""
import math

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

vave = 0
mi = 0
for i in range(a, b+1):
    vsumi = sum(v[:i])
    vavei = vsumi/(i)
    if vave <= vavei:
        vave = vavei
        ci = v.count(v[i-1])
        cin = v[:i].count(v[i-1])
        m = math.factorial(ci)//math.factorial(cin)//math.factorial(ci-cin)
        mi += m
        #print(i,m)

print(vave)
print(mi)

Submission status: Accepted

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


def comb(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


# --------------------------------------------

dp = None


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

    V.sort(reverse=True)

    maxavg = sum(V[:A]) / A
    V_ = V[:A]

    if V_[0] == V_[-1]:
        n = V.count(V_[0])

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

        print(maxavg)
        print(int(ans))

    else:
        n = V.count(V_[-1])
        r = V_.count(V_[-1])

        ans = comb(n, r)

        print(maxavg)
        print(int(ans))


main()

Submission status: Accepted

Solution code:
from itertools import accumulate


def pascal(arr, n):
    for i in range(n+1):
        for j in range(i+1):
            if j == 0 or j == i:
                arr[i][j] = 1
            else:
                arr[i][j] = arr[i-1][j-1]+arr[i-1][j]
    return arr


N, A, B = map(int, input().split())
v = sorted([int(x) for x in input().split()], reverse=True)
ans_c = 0
ans_v = [x for x in accumulate(v)]
tmp = v[0]
lft = 0
rgt = N-1
comb = [[0 for _ in range(N+1)] for _ in range(N+1)]
pascal(comb, N)
for i in range(N):
    if v[i] != tmp:
        if i >= A:
            rgt = i-1
            break
        else:
            tmp = v[i]
            lft = i
if lft == 0 and rgt != 0:
    for i in range(A, min(rgt+1, B)+1):
        ans_c += comb[rgt+1][i]
else:
    ans_c = comb[rgt-lft+1][A-lft]
print(ans_v[A-1]/A)
print(ans_c)

Submission status: Accepted

Solution code:
import math
n,a,b =map(int,input().split())
v = list(map(int,input().split()))
v = sorted(v,key=lambda x:-x)
need = v[:a]
ti = min(need)
no = need.count(ti)
try:
  ona = v[a:].count(ti)
except:
  ona = 0
if ona == 0:
  print(sum(need)/a)
  print(1)
elif no == a:
  ans = 0
  for i in range(b+1-a):
    if ona == -1:
      break
    ans += math.factorial(ona+no)/math.factorial(ona)/math.factorial(no)
    ona -= 1
    no += 1
  print(sum(need)/a)
  print(int(ans))
else:
  print(sum(need)/a)
  print(int(math.factorial(ona+no)/math.factorial(ona)/math.factorial(no)))

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())

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

def C(n, r):
	return P(n, r)//math.factorial(r)

N,A,B = inpl()
vv = inpl()
vv.sort()

ave = sum(vv[N-A:])/A
print(ave)

MIN = vv[N-A]
SUM = vv.count(MIN)
used = vv[N-A:].count(MIN)

if vv[-1] != vv[N-A]:
	used = vv[N-A:].count(MIN)
	print(C(SUM,used))
else:
	ans = 0
	for i in range(A,min(SUM,B)+1):
		ans += C(SUM,i)
	print(ans)

Submission status: Accepted

Solution code:
def factorial(n):
    if n == 1 or n == 0:
        return int(1)
    return int(n*factorial(n-1))

def nck(n,k):
    return int(factorial(n)/(factorial(n-k)*factorial(k)))

ans = 0
ans2 = 0
N,A,B = map(int,input().split())
v = list(map(int,input().split()))
v.sort()
for i in range(A,B+1):
    ans = max(ans,sum(v[N-i:N])/i)
print("{:.7f}".format(ans))

for i in range(A,B+1):
    if ans == sum(v[N-i:N])/i:
        n = v.count(v[N-i])
        k = i - len([p for p in v if p > v[N-i]])
        ans2 += nck(n,k)
print(int(ans2))