#!/usr/bin/env python def coin(moneylst,countlst,d): tmp[0] = 1 tmp[1::1] = d * [0] for i in range(len(moneylst)): for k in range(d,-1,-1): if tmp[k] > 0: for j in range(1, countlst[i] + 1): if (k + j * moneylst[i]) > d : break tmp[k + j * moneylst[i]] += tmp[k]; return tmp[d] def main(): money = [1,2,5] count = [3,3,1] d = 7 print coin(money,count,d) if __name__=="__main__": main() |
''' The number of the integer solutions of a_1*x_1+a_2*x_2+...+a_V*V=N ''' try: import psyco psyco.full() except ImportError: pass def P(V,N): if N< =1 or V==2: return N/2+1 elif V==1: return 1 elif N<V: return P(N-1,N)+1 else: return sum([P(V-1,N-V*x) for x in xrange(N/V+1)]) print P(99,100) |
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |