Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. # bynary indexed tree
  2. class BIT:
  3. def __init__(self, n):
  4. self.n = n
  5. self.N = 1 << n.bit_length()
  6. self.data = [0] * (self.N +1)
  7.  
  8. def add(self, i, x):
  9. while i <= self.N:
  10. self.data[i] += x
  11. i += i & -i
  12.  
  13. def sum(self, i):
  14. total = 0
  15. while i > 0:
  16. total += self.data[i]
  17. i -= i&-i
  18. return total
  19.  
  20. def search(self, w):
  21. if w <= 0:
  22. return 0
  23. x = 0
  24. k = self.N >> 1
  25. while k > 0:
  26. if x+k <= self.N and self.data[x+k] < w:
  27. w -= self.data[x+k]
  28. x += k
  29. k = k >> 1
  30. return x+1
  31.  
  32. def len(self):
  33. return self.data[self.N]
  34.  
  35. # fast nCr
  36. def frac(n, r):
  37. if r < 0 or r > n:
  38. return 0
  39. r = min(r, n - r)
  40. return g1[n] * g2[r] * g2[n - r] % mod
  41.  
  42.  
  43. mod = 10 ** 9 + 7
  44. N = 10 ** 6
  45. g1 = [1, 1]
  46. g2 = [1, 1]
  47. inverse = [0, 1]
  48.  
  49. for i in range(2, N + 1):
  50. g1.append((g1[-1] * i) % mod)
  51. inverse.append((-inverse[mod % i] * (mod // i)) % mod)
  52. g2.append((g2[-1] * inverse[-1]) % mod)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement