Problem p02804 (python)

Fixed submission:
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A.sort()
res = 0
MOD = 10**9+7
nCk_2 = dict()
fac, finv, inv = [0]*(N+1), [0]*(N+1), [0]*(N+1)
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
inv[1] = 1
for i in range(2,N+1):
    fac[i] = fac[i-1]*i%MOD
    inv[i] = MOD-inv[MOD%i]*(MOD//i)%MOD
    finv[i] = finv[i-1]*inv[i]%MOD
def comb(n, k):
    return (fac[n]*((finv[k]*finv[n-k])%MOD))%MOD
s = [1,-1]
for m in range(2):
    for i in range(K-1,N):
        res = (res + s[m]*int((A[i])*comb(i,K-1)))%MOD
    A = sorted(A, reverse=True)
print(res)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A.sort()
res = 0
MOD = 10**9+7
nCk_2 = dict()
def fact(n, k=1):
    if n == 0:
        return 1
    if n <= k:
        return 1
    return n*fact(n-1)%MOD
fact_t = []
for n in range(N):
    fact_t.append(fact(n))
def comb(n, k):
    return (fact_t[n]//(fact_t[n-k]*fact_t[k]))%MOD
s = [1,-1]
for m in range(2):
    for i in range(K-1,N):
        res = (res + s[m]*int((A[i])*comb(i,K-1)))%MOD
    A = sorted(A, reverse=True)
print(res)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A.sort()
res = 0
MOD = 10**9+7
nCk_2 = dict()
def fact(n, k=1):
    if n == 0:
        return 1
    if n <= k:
        return 1
    return n*fact(n-1)%MOD
def comb(n, k):
    return (fact(n)//(fact(n-k)*fact(k)))%MOD
s = [1,-1]
for m in range(2):
    for i in range(K-1,N):
        res = (res + s[m]*int((A[i])*comb(i,K-1)))%MOD
    A = sorted(A, reverse=True)
print(res)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A.sort()
res = 0
MOD = 10**9+7
nCk_2 = dict()
def fact(n, k=1):
    if n == 0:
        return 1
    if n <= k:
        return 1
    return n*fact(n-1)
def comb(n, k):
    return (fact(n)/(fact(n-k)*fact(k)))%MOD
s = [1,-1]
for m in range(2):
    for i in range(K-1,N):
        res = (res + s[m]*int((A[i])*comb(i,K-1)))%MOD
    A = sorted(A, reverse=True)
print(res)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A.sort()
res = 0
MOD = 10**9+7
nCk_2 = dict()
def fact(n, k=1):
    if n == 0:
        return 1
    if n <= k:
        return 1
    return n*fact(n-1)
def comb(n, k):
    return (fact(n)/(fact(n-k)*fact(k)))%MOD
s = [1,-1]
for m in range(2):
    for i in range(K-1,N):
        res += s[m]*int((A[i])*comb(i,K-1))
    A = sorted(A, reverse=True)
print(res)

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A.sort()
res = 0
MOD = 10**9+7
nCk_2 = dict()
def fact(n, k=1):
    if n == 0:
        return 1
    if n <= k:
        return 1
    return n*fact(n-1)
def comb(n, k):
    if n-k > k:
        return fact(n,k)/fact(n-k)
    else:
        return fact(n,n-k)/fact(k)
for k in range(K-2,N):
    nCk_2[k] = int(comb(k, K-2))%MOD
for i in range(N):
    for j in range(N):
        if N-i-j >= K:
            res += int((A[N-1-j]-A[i])*nCk_2[N-j-2-i]%MOD)
        if N-1-j < i:
            break
print(res)

Buggy submission (verdict: Runtime Error):
from scipy.special import comb
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A.sort()
res = 0
MOD = 10**9+7
nCk_2 = dict()
def fact(n, k=1):
    if n == 0:
        return 1
    if n <= k:
        return 1
    return n*fact(n-1)
def comb(n, k):
    if n-k > k:
        return fact(n,k)/fact(n-k)
    else:
        return fact(n,n-k)/fact(k)
for k in range(K-2,N):
    nCk_2[k] = int(comb(k, K-2))%MOD
for i in range(N):
    for j in range(N):
        if N-i-j >= K:
            res += int((A[N-1-j]-A[i])*nCk_2[N-j-2-i]%MOD)
        if N-1-j < i:
            break
print(res)

Buggy submission (verdict: Runtime Error):
from scipy.special import comb
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A.sort()
res = 0
MOD = 10**9+7
nCk_2 = dict()
for k in range(K-2,N):
    nCk_2[k] = int(comb(k, K-2))%MOD
for i in range(N):
    for j in range(N):
        if N-i-j >= K:
            res += int((A[N-1-j]-A[i])*nCk_2[N-j-2-i]%MOD)
        if N-1-j < i:
            break
print(res)

Buggy submission (verdict: Runtime Error):
from scipy.special import comb
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A.sort()
res = 0
MOD = 10**9+7
for i in range(N):
    for j in range(N):
        if N-i-j >= K:
            res += int((A[N-1-j]-A[i])*comb(N-j-2-i,K-2)%MOD)
        if N-1-j < i:
            break
print(res)