PYTHON LIBRARY

 

  1. import sys
  2. input = sys.stdin.readline
  3. read = lambda: map(int, input().split())
  4. # from bisect import bisect_left as lower_bound;
  5. # from bisect import bisect_right as upper_bound;
  6. # from math import ceil, factorial;
  7.  
  8. def ceil(x):
  9. if x != int(x):
  10. x = int(x) + 1
  11. return x
  12. def factorial(x, m):
  13. val = 1
  14. while x>0:
  15. val = (val * x) % m
  16. x -= 1
  17. return val
  18.  
  19. def fact(x):
  20. val = 1
  21. while x > 0:
  22. val *= x
  23. x -= 1
  24. return val
  25. # swap_array function
  26. def swaparr(arr, a,b):
  27. temp = arr[a];
  28. arr[a] = arr[b];
  29. arr[b] = temp;
  30. ## gcd function
  31. def gcd(a,b):
  32. if b == 0:
  33. return a;
  34. return gcd(b, a % b);
  35.  
  36. ## lcm function
  37. def lcm(a, b):
  38. return (a * b) // math.gcd(a, b)
  39. ## nCr function efficient using Binomial Cofficient
  40. def nCr(n, k):
  41. if k > n:
  42. return 0
  43. if(k > n - k):
  44. k = n - k
  45. res = 1
  46. for i in range(k):
  47. res = res * (n - i)
  48. res = res / (i + 1)
  49. return int(res)
  50. ## upper bound function code -- such that e in a[:i] e < x;
  51.  
  52. ## prime factorization
  53. def primefs(n):
  54. ## if n == 1 ## calculating primes
  55. primes = {}
  56. while(n%2 == 0 and n > 0):
  57. primes[2] = primes.get(2, 0) + 1
  58. n = n//2
  59. for i in range(3, int(n**0.5)+2, 2):
  60. while(n%i == 0 and n > 0):
  61. primes[i] = primes.get(i, 0) + 1
  62. n = n//i
  63. if n > 2:
  64. primes[n] = primes.get(n, 0) + 1
  65. ## prime factoriazation of n is stored in dictionary
  66. ## primes and can be accesed. O(sqrt n)
  67. return primes
  68. ## MODULAR EXPONENTIATION FUNCTION
  69. def power(x, y, p):
  70. res = 1
  71. x = x % p
  72. if (x == 0) :
  73. return 0
  74. while (y > 0) :
  75. if ((y & 1) == 1) :
  76. res = (res * x) % p
  77. y = y >> 1
  78. x = (x * x) % p
  79. return res
  80. ## DISJOINT SET UNINON FUNCTIONS
  81. def swap(a,b):
  82. temp = a
  83. a = b
  84. b = temp
  85. return a,b;
  86. # find function with path compression included (recursive)
  87. # def find(x, link):
  88. # if link[x] == x:
  89. # return x
  90. # link[x] = find(link[x], link);
  91. # return link[x];
  92. # find function with path compression (ITERATIVE)
  93. def find(x, link):
  94. p = x;
  95. while( p != link[p]):
  96. p = link[p];
  97. while( x != p):
  98. nex = link[x];
  99. link[x] = p;
  100. x = nex;
  101. return p;
  102. # the union function which makes union(x,y)
  103. # of two nodes x and y
  104. def union(x, y, link, size):
  105. x = find(x, link)
  106. y = find(y, link)
  107. if size[x] < size[y]:
  108. x,y = swap(x,y)
  109. if x != y:
  110. size[x] += size[y]
  111. link[y] = x
  112. ## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
  113. def sieve(n):
  114. prime = [True for i in range(n+1)]
  115. prime[0], prime[1] = False, False
  116. p = 2
  117. while (p * p <= n):
  118. if (prime[p] == True):
  119. for i in range(p * p, n+1, p):
  120. prime[i] = False
  121. p += 1
  122. return prime
  123.  
  124. # Euler's Toitent Function phi
  125. def phi(n) :
  126. result = n
  127. p = 2
  128. while(p * p<= n) :
  129. if (n % p == 0) :
  130. while (n % p == 0) :
  131. n = n // p
  132. result = result * (1.0 - (1.0 / (float) (p)))
  133. p = p + 1
  134. if (n > 1) :
  135. result = result * (1.0 - (1.0 / (float)(n)))
  136. return (int)(result)
  137.  
  138. def is_prime(n):
  139. if n == 0:
  140. return False
  141. if n == 1:
  142. return True
  143. for i in range(2, int(n ** (1 / 2)) + 1):
  144. if not n % i:
  145. return False
  146. return True
  147. #### PRIME FACTORIZATION IN O(log n) using Sieve ####
  148. MAXN = int(1e5 + 5)
  149. def spf_sieve():
  150. spf[1] = 1;
  151. for i in range(2, MAXN):
  152. spf[i] = i;
  153. for i in range(4, MAXN, 2):
  154. spf[i] = 2;
  155. for i in range(3, ceil(MAXN ** 0.5), 2):
  156. if spf[i] == i:
  157. for j in range(i*i, MAXN, i):
  158. if spf[j] == j:
  159. spf[j] = i;
  160. ## function for storing smallest prime factors (spf) in the array
  161. ################## un-comment below 2 lines when using factorization #################
  162. spf = [0 for i in range(MAXN)]
  163. # spf_sieve();
  164. def factoriazation(x):
  165. res = []
  166. for i in range(2, int(x ** 0.5) + 1):
  167. while x % i == 0:
  168. res.append(i)
  169. x //= i
  170. if x != 1:
  171. res.append(x)
  172. return res
  173. ## this function is useful for multiple queries only, o/w use
  174. ## primefs function above. complexity O(log n)
  175.  
  176. def factors(n):
  177. res = []
  178. for i in range(1, int(n ** 0.5) + 1):
  179. if n % i == 0:
  180. res.append(i)
  181. res.append(n // i)
  182. return list(set(res))
  183. ## taking integer array input
  184. def int_array():
  185. return list(map(int, input().strip().split()));
  186. def float_array():
  187. return list(map(float, input().strip().split()));
  188. ## taking string array input
  189. def str_array():
  190. return input().strip().split();
  191.  
  192. def binary_search(low, high, w, h, n):
  193. while low < high:
  194. mid = low + (high - low) // 2
  195. # print(low, mid, high)
  196. if check(mid, w, h, n):
  197. low = mid + 1
  198. else:
  199. high = mid
  200. return low
  201.  
  202. ## for checking any conditions
  203. def check(councils, a, k):
  204. summ = 0
  205. for x in a:
  206. summ += min(x, councils)
  207. return summ // councils >= k
  208.  
  209. #defining a couple constants
  210. MOD = int(1e9)+7;
  211. CMOD = 998244353;
  212. INF = float('inf'); NINF = -float('inf');
  213. ################### ---------------- TEMPLATE ENDS HERE ---------------- ###################
  214. from itertools import permutations
  215. import math
  216. import bisect as bis
  217. import random
  218. import sys

Comments

Popular Posts