-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.py
221 lines (177 loc) · 4.28 KB
/
store.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
##divisor func.
def div(n):
res=[]
i=int(1)
while i*i<=n:
if n%i==0:
res.append(i)
if n!=i**2:
res.append(n//i)
i+=1
res.sort()
return res
##prime factors
def fac(n):
res=set()
i=int(2)
now=n
while i*i<=n:
while now%i==0:
now//=i
res.add(i)
i+=1
if now!=1:
res.add(now)
return sorted(list(res))
##create array
def make_vec(*dims):
n = len(dims) - 1
initial = dims[-1]
code = "[" * n + "{}] * {}" + " for _ in range({})]" * (n - 1)
return eval(code.format(initial, *reversed(dims[:n])))
##create graph
def graph(n):
res=[[]for _ in range(n)]
return res
##least common multiple
import math
def lcm(a,b):
return a//math.gcd(a,b)*b
#tousa suretsu sum
def tousa_sum(a,d,n):
return (a+((n-1)*d+a))*n//2
##neighbor search
for i in range(h):
for j in range(w):
for x in range(max(0,i-1),min(h,(i+1)+1)):
for y in range(max(0,j-1),min(w,(j+1)+1)):
##float to int
def toint(s):
n=len(s)
pos=0
for i in range(n):
if s[i]=='.':
pos=-(n-1-i)
s=s.replace('.','')
ans=s+' '+str(pos)
return ans
##input func
import sys
input = sys.stdin.readline
LI = lambda:list(map(int,input().split()))
LI0 = lambda:list(map((lambda x:int(x)-1),input().split()))
MI = lambda:map(int, input().split())
##random number generator
import random
rnd=random.randrange()
##string easy sort
def SORT(s):
return ''.join(sorted(s))
def REV(s):
return ''.join(reversed(s))
##Prime checker
def isprime(n):
if n>=10**6:
if len(div(n))==2:
return True
else:
return False
else:
if n==1 or sieve[n]!=n:
return False
else:
return True
##BFS on Grid
from collections import deque
def grid_bfs(field,sx,sy):
Q=deque()
Q.append((sx,sy))
dist=[[10**20]*w for _ in range(h)]
dist[sx][sy]=0
check=[[0]*w for _ in range(h)]
check[sx][sy]=1
while len(Q):
now=Q.popleft()
x,y=now[0],now[1]
for dx,dy in ((-1,0),(0,1),(1,0),(0,-1)):
if not(0<=x+dx<h) or not(0<=y+dy<w):
continue
if field[x+dx][y+dy]=='#':
continue
if check[x+dx][y+dy]:
continue
dist[x+dx][y+dy]=dist[x][y]+1
Q.append((x+dx,y+dy))
check[x+dx][y+dy]=1
return dist
#BFS on Graph
from collections import deque
def graph_bfs(graph,start):
sz=len(graph)
Q=deque()
Q.append(start)
dist=[10**20]*sz
dist[start]=0
check=set()
check.add(start)
while len(Q):
now=Q.popleft()
for nex in graph[now]:
if nex in check:
continue
dist[nex]=dist[now]+1
Q.append(nex)
check.add(nex)
return dist
#DFS
import sys
import pypyjit
sys.setrecursionlimit(300000)
pypyjit.set_param('max_unroll_recursion=-1')
def dfs(cur,pre):
#来たときの処理
check.add(cur)
for nex in g[cur]:
if nex not in check and nex!=pre:
dfs(nex,cur)
#帰ってきたときの処理
##Ternary Search
##Submit to PyPy!!!
cnt=100
l,r=0,
while cnt:
x,y=(2*l+r)//3,(l+2*r)//3
d=max(3,(r-l))
if ##value of x <= value of y:
r=r-d//3
else:
l=l+d//3
cnt-=1
#matrix power
def matrix_pow(A,x,mod=998244353):
n=len(A)
res=make_vec(n,n,0)
for i in range(n):
res[i][i]=1
while x>0:
cur=make_vec(n,n,0)
if x&1:
for i in range(n):
for j in range(n):
for k in range(n):
cur[i][j]+=res[i][k]*A[k][j]
cur[i][j]%=mod
for i in range(n):
for j in range(n):
res[i][j]=cur[i][j]
cur[i][j]=0
x//=2
for i in range(n):
for j in range(n):
for k in range(n):
cur[i][j]+=A[i][k]*A[k][j]
cur[i][j]%=mod
for i in range(n):
for j in range(n):
A[i][j]=cur[i][j]
return res