Problem p02804 (python)

Fixed submission:
#input
N, K = map(int, input().split())
A = list(map(int, input().split()))

#output
import math

A.sort()
mod = 10**9+7

n_ = 10**5 + 3
fun = [1]*(n_+1)
for i in range(1,n_+1):
    fun[i] = fun[i-1]*i%mod
rev = [1]*(n_+1)
rev[n_] = pow(fun[n_],mod-2,mod)
for i in range(n_-1,0,-1):
    rev[i] = rev[i+1]*(i+1)%mod
def cmb(n,r):
    if n <= 0 or r < 0 or r > n: return 0
    return fun[n]*rev[r]%mod*rev[n-r]%mod
#これだとTLE!
"""
def cmb(n, r):
    if n < r: return 0
    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
"""

M = 0
m = 0
for i, a in enumerate(A):
    M += cmb(i, K-1) * a
    m += cmb(N-i-1, K-1) * a
    M %= mod
    m %= mod

print((M-m) % mod)

Buggy submission (verdict: Runtime Error):
#input
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))

#output
import math

A = np.sort(A)
this_mod = 10**9+7

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

x = np.full(N, 0, np.int64())
for i in range(N-K+1):
    x[i] = cmb(N-i-1, N-K-i)

y = (-x + x[-1::-1]) % this_mod

print((A*y).sum() % this_mod)

Buggy submission (verdict: Runtime Error):
#input
N, K = map(int, input().split())
A = list(map(int, input().split()))

#output
import numpy as np
import math
A = np.array(A)
A = np.sort(A)

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

x = np.full(N, 0, int)
for i in range(N-K+1):
    x[i] = cmb(N-i-1, N-K-i)

y = -x + x[-1::-1]

print((A*y).sum() % (10**9 + 7))

Buggy submission (verdict: Runtime Error):
#input
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))

#output
import math
A = np.sort(A)

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

x = np.full(N, 0, int)
for i in range(N-K+1):
    x[i] = cmb(N-i-1, N-K-i)

y = -x + x[-1::-1]

print((A*y).sum() % (10**9 + 7))

Buggy submission (verdict: Runtime Error):
#input
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))

#output
import math
A = np.sort(A)

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

q = np.full(N, 0, np.int64)
for i in range(N-K+1):
    q[i] = cmb(N-i-1, N-K-i)

r = -q + q[-1::-1]

print((A*r).sum() % (10**9 + 7))

Buggy submission (verdict: Runtime Error):
#input
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))

#output
import math
A = np.sort(A)

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

q = np.full(N, 0, int)
for i in range(N-K+1):
    q[i] = cmb(N-i-1, N-K-i)

r = -q + q[-1::-1]

print((A*r).sum() % (10**9 + 7))

Buggy submission (verdict: Runtime Error):
#input
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))

#output
import math
A = np.sort(A)

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

q = np.full(N, 0)
for i in range(N-K+1):
    q[i] = cmb(N-i-1, N-K-i)

r = -q + q[-1::-1]

print(int((A*r).sum() % (10**9 + 7)))

Buggy submission (verdict: Runtime Error):
#input
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))

#output
import math
A = np.sort(A)

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

q = np.full(N, 0)
for i in range(N-K+1):
    q[i] = cmb(N-i-1, N-K-i)

r = -q + q[-1::-1]

print((A*r).sum() % (10**9 + 7))

Buggy submission (verdict: Runtime Error):
#input
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))

#output
import numpy as np
import math
A = np.sort(A)

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

q = np.full(N, 0)
for i in range(N-K+1):
    q[i] = cmb(N-i-1, N-K-i)

r = -q + q[-1::-1]

print((A*r).sum() % (10**9 + 7))

Buggy submission (verdict: Runtime Error):
#input
N, K = map(int, input().split())
A = list(map(int, input().split()))

#output
import numpy as np
import math
A = np.array(A)
A = np.sort(A)

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

q = np.full(N, 0)
for i in range(N-K+1):
    q[i] = cmb(N-i-1, N-K-i)

r = -q + np.flip(q)

print((A*r).sum() % (10**9 + 7))

Buggy submission (verdict: Runtime Error):
#input
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))

#output
import numpy as np
import math
A = np.sort(A)

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

q = np.full(N, 0)
for i in range(N-K+1):
    q[i] = cmb(N-i-1, N-K-i)

r = -q + np.flip(q)

print((A*r).sum() % (10**9 + 7))

Buggy submission (verdict: Runtime Error):
#input
import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))

#output
A = np.sort(A)

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

q = np.full(N, 0)
for i in range(N-K+1):
    q[i] = cmb(N-i-1, N-K-i)

r = -q + np.flip(q)

print((A*r).sum() % (10**9 + 7))