fork(3) download
  1. import sys
  2. from numbers import Rational
  3. from fractions import Fraction
  4.  
  5. def derive (p):
  6. return [p[i] * i for i in range (1, len (p))]
  7.  
  8. def mult (p, q):
  9. a = [0 for k in range (len (p) + len (q) - 1)]
  10. for i in range (len (p)):
  11. for j in range (len (q)):
  12. a[i + j] += p[i] * q[j]
  13. return a
  14.  
  15. def add (p, q):
  16. a = [0 for k in range (max (len (p), len (q)))]
  17. for i in range (len (p)):
  18. a[i] += p[i]
  19. for j in range (len (q)):
  20. a[j] += q[j]
  21. return a
  22.  
  23. C, D = [0, 1], [1, -1]
  24.  
  25. def next (p, k):
  26. return mult (C, (add (mult ([k], p), mult (derive (p), D))))
  27.  
  28. a, b = map (int, sys.stdin.readline ().split ())
  29. if b == 1:
  30. sys.stdout.write ('infinity')
  31. else:
  32. p, q = C, D
  33. for i in range (a):
  34. p, q = next (p, i + 1), mult (q, D)
  35. u = sum (Fraction (p[i], b ** i) for i in range (len (p)))
  36. v = sum (Fraction (q[j], b ** j) for j in range (len (q)))
  37. z = u / v
  38. sys.stdout.write ('%d/%d\n' % (z.numerator, z.denominator))
  39.  
Success #stdin #stdout 0.19s 12504KB
stdin
10 10
stdout
145739620510/387420489