Created
December 17, 2011 04:13
-
-
Save azalea/1489148 to your computer and use it in GitHub Desktop.
Facebook Hacker Cup 2011 Round 1A - Wine Tasting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
def fac(n): | |
'''n!''' | |
if n==0 or n==1: | |
return 1 | |
else: | |
return n*fac(n-1) | |
def facFrac(n,k): | |
'''n!/k!''' | |
res = 1 | |
for i in range(k+1,n+1): | |
res *= i | |
return res | |
def choose(n,k): | |
return facFrac(n,k)/fac(n-k) | |
def M(n): | |
res = 0 | |
for i in range(2,n+1): | |
if i % 2: | |
res -= facFrac(n,i) | |
else: | |
res += facFrac(n,i) | |
return res | |
def rightArrange(n,k): | |
total = fac(n) | |
# print total | |
for i in range(k): | |
total -= choose(n,i)*M(n-i) | |
# print n,i,M(n-i),total | |
return total | |
infile = sys.argv[1] | |
f = open(infile) | |
N = int(f.readline().strip()) | |
for i in range(N): | |
n, k = map(int,f.readline().strip().split(' ')) | |
print rightArrange(n,k) % 1051962371 | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment