-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1.Template.py
123 lines (113 loc) · 2.81 KB
/
1.Template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from __future__ import division, print_function
# import threading
# threading.stack_size(2**27)
# import sys
# sys.setrecursionlimit(10**7)
# sys.stdin = open('inpy.txt', 'r')
# sys.stdout = open('outpy.txt', 'w')
from sys import stdin, stdout
import bisect #c++ upperbound
import math
import heapq
i_m=9223372036854775807
def inin():
return int(input())
def stin():
return input()
def spin():
return map(int,stin().split())
def lin(): #takes array as input
return list(map(int,stin().split()))
def matrix(n):
#matrix input
return [list(map(int,input().split()))for i in range(n)]
################################################
def string2intlist(s):
return list(map(int, s))
def calculate_sum(a, N): #sum of a to N
# Number of multiples
m = N / a
# sum of first m natural numbers
sum = m * (m + 1) / 2
# sum of multiples
ans = a * sum
return ans
def series(N):
return (N*(N+1))//2
def count2Dmatrix(i,list):
return sum(c.count(i) for c in list)
def modinv(n,p):
return pow(n, p - 2, p)
def nCr(n, r):
i = 1
while i < r:
n *= (n - i)
i += 1
return n // math.factorial(r)
def GCD(x, y):
x=abs(x)
y=abs(y)
if(min(x,y)==0):
return max(x,y)
while(y):
x, y = y, x % y
return x
def LCM (x, y):
return (x * y) // GCD(x, y)
def Divisors(n) :
l = []
for i in range(1, int(math.sqrt(n) + 1)) :
if (n % i == 0) :
if (n // i == i) :
l.append(i)
else :
l.append(i)
l.append(n//i)
return l
def isprime(n):
for i in range(2, int(math.sqrt(n))+1):
if n%i==0:
return False
return True
prime=[]
def SieveOfEratosthenes(n):
global prime
prime = [True for i in range(n+1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * p, n+1, p):
prime[i] = False
p += 1
f=[]
for p in range(2, n):
if prime[p]:
f.append(p)
return f
q=[]
def dfs(n,d,v,c):
global q
v[n]=1
x=d[n]
q.append(n)
j=c
for i in x:
if i not in v:
f=dfs(i,d,v,c+1)
j=max(j,f)
# print(f)
return j
# d = {}
def knapSack(W, wt, val, n):
K = [[0 for x in range(W + 1)] for x in range(n + 1)]
# Build table K[][] in bottom up manner
for i in range(n + 1):
for w in range(W + 1):
if i == 0 or w == 0:
K[i][w] = 0
elif wt[i-1] <= w:
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w])
else:
K[i][w] = K[i-1][w]
return K[n][W]
"""*******************************************************"""