fork download
  1. from collections import Counter
  2. from math import factorial
  3. from fractions import gcd
  4.  
  5. def solution(w, h, s):
  6. # Your code here
  7. grid=0
  8. for cpw in partition_cycles(w):
  9. for cph in partition_cycles(h):
  10. m=count_cycles(cpw, w)*count_cycles(cph, h)
  11. grid+=m*(s**sum([sum([gcd(i, j) for i in cpw]) for j in cph]))
  12.  
  13. return grid//(factorial(w)*factorial(h))
  14.  
  15. def count_cycles(c, n):
  16. num_cycles = factorial(n)
  17. for a, b in Counter(c).items():
  18. num_cycles //= (a**b) * factorial(b)
  19. return num_cycles
  20.  
  21. def partition_cycles(n, i=1):
  22. yield [n]
  23. for i in range(i, n // 2 + 1):
  24. for p in partition_cycles(n - i, i):
  25. yield [i] + p
  26.  
  27. print(solution(2,3,4))
Success #stdin #stdout #stderr 0.03s 10560KB
stdin
Standard input is empty
stdout
430
stderr
./prog:11: DeprecationWarning: fractions.gcd() is deprecated. Use math.gcd() instead.