Q53

来源:互联网 发布:淘宝主图视频尺寸要求 编辑:程序博客网 时间:2024/06/07 06:47

# coding=UTF-8
'''
There are exactly ten ways of selecting three from five, 12345:

123, 124, 125, 134, 135, 145, 234, 235, 245, and 345

In combinatorics, we use the notation, 5C3 = 10.

In general,

nCr =  n!/r!(n-r)!
,where r  n, n! = n(n1)...321, and 0! = 1.

It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066.

How many, not necessarily distinct, values of  nCr, for 1  n  100, are greater than one-million?
'''

''' 
Cn r+1 = Cn r * (n-r)/(r+1)
'''

MAX_EXCEED = 1000000
sum = 4
for n in range(24,101):
    Cnr = n
    count = 0
    for r in range(2,(n-1)/2+1):
        Cnr = Cnr * (n-(r-1)) / r
        #print Cnr
        if Cnr > MAX_EXCEED:
            count += 1
    count *= 2
    if n%2 == 0:
        Cnr = Cnr * (n/2)/(n/2+1)
        if Cnr > MAX_EXCEED:
            count += 1
    sum += count
   
print sum