fork download
  1. N = 2
  2. M = 1
  3. K = 3
  4. maxValue = M*K
  5.  
  6. count = [0] * (maxValue+1)
  7. prev = [0] * (maxValue+1)
  8.  
  9. count[0] = 1 # empty set
  10.  
  11. for i in range(K):
  12. # move count to prev
  13. for index in range(maxValue+1):
  14. prev[index] = count[index]
  15. count[index] = 0
  16.  
  17. rollingSum = 0
  18.  
  19. # calculate new counts
  20. for Sum in range(maxValue+1):
  21. rollingSum += prev[Sum]
  22. if (Sum > M):
  23. rollingSum -= prev[Sum - (M + 1)]
  24. count[Sum] = rollingSum
  25.  
  26.  
  27. # add all counts of sets whose sum is >= N
  28. ans = sum(count[N:]) / pow(M+1,K)
  29. print(ans)
  30.  
Success #stdin #stdout 0.02s 9144KB
stdin
Standard input is empty
stdout
0.5