#!/usr/bin/python

# pascals_triangle.py
#       --copyright--                   Copyright 2007 (C) Tranzoa, Co. All rights reserved.    Warranty: You're free and on your own here. This code is not necessarily up-to-date or of public quality.
#       --url--                         http://www.tranzoa.net/tzpython/
#       --email--                       pycode is the name to send to. tranzoa.com is the place to send to.
#       --bodstamps--
#       March 9, 2005           bar
#       November 18, 2007       bar     turn on doxygen
#       November 27, 2007       bar     insert boilerplate copyright
#       May 17, 2008            bar     email adr
#       --eodstamps--
##      \file
#
#
#       Find numbers in Pascal's triangle - that is, counts of how many instances of exactly X things there are in Y buckets.
#         (permutations)
#
#
#
#       Better code is:
#
#
#           from operator import add
#
#
#           def nextrow(row):
#               thisrow = row + [0]
#               addrow  = [0] + row
#               return map(add,thisrow,addrow)
#
#           while   y >= len(values) :
#               values.append(nextrow(values[-1]))
#
#



values = [ [ 1L ], [ 1L, 1L ] ]



def count_of_x_items_of_y_possible(x, y) :
    global  values

    x = int(x)
    y = int(y)

    if  x  > y :    return(0)
    if  x == y :    return(1)

    while y >= len(values) :

        prw = values[len(values) - 1]
        row = [ 1L ]
        for i in range(1, len(prw)) :
            row.append(prw[i] + prw[i - 1])

        # print "row", len(values), row

        row.append(1L)

        values.append(row)

    return(values[y][x])



def count_of_any_x_of_y_items_of_z_possibilities(x, y, z) :
    """
        Well, this isn't what I want it to be, but whatever.
    """

    x   = int(x)
    y   = int(y)
    z   = int(z)

    cnt = 0L

    for n in range(x, y + 1) :
        cnt += count_of_x_items_of_y_possible(n, y) * count_of_x_items_of_y_possible(y - n, z - y)

    return(cnt)



if  __name__ == '__main__' :
    import  sys
    import  math

    if  len(sys.argv) == 3 :

        c = count_of_x_items_of_y_possible(sys.argv[1], sys.argv[2])
        print math.log(c) / math.log(2), c

    elif  len(sys.argv) == 4 :

        c = count_of_x_items_of_y_possible(sys.argv[2], sys.argv[3])
        print math.log(c) / math.log(2), c

        c = count_of_any_x_of_y_items_of_z_possibilities(sys.argv[1], sys.argv[2], sys.argv[3])
        print math.log(c) / math.log(2), c

    else :

        print   "Tell me an X and Y for X things in Y possible."


    pass



#
#
#
# eof

