Skip to content

Commit 7b98e7e

Browse files
committed
2021.02.11
1 parent 6c664db commit 7b98e7e

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

python/plat2/1067.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from cmath import exp, pi
2+
import sys
3+
input = sys.stdin.readline
4+
5+
n = int(input())
6+
N = 1
7+
while N<2*n:N<<=1
8+
a = list(map(int,input().split()))+[0]*(N-n)
9+
b = list(map(int,input().split()))
10+
b.reverse()
11+
b = b+[0]*(N-2*n)+b
12+
# def fft(a):
13+
# N = len(a)
14+
# if N == 1: return a
15+
# A_even = fft(a[0::2])
16+
# A_odd = fft(a[1::2])
17+
# w_N = [exp(2j*pi*n/N) for n in range(N//2)]
18+
# return [A_even[n] + w_N[n] * A_odd[n] for n in range(N//2)] + \
19+
# [A_even[n] - w_N[n] * A_odd[n] for n in range(N//2)]
20+
21+
def fft(x, inverse=False):
22+
N, e = 1,0
23+
while N < len(x):
24+
N<<=1
25+
e+=1
26+
for i in range(N):
27+
tmp = i
28+
j = 0
29+
for _ in range(e):
30+
j=j<<1 | tmp&1
31+
tmp>>=1
32+
if i<j:x[i],x[j]=x[j],x[i]
33+
n=2
34+
while n<=N:
35+
unit = exp(2j*pi/n * (1 if inverse else -1))
36+
for i in range(0,N,n):
37+
W = 1
38+
for k in range(i, i+n//2):
39+
tmp = x[k+n//2]*W
40+
x[k+n//2]=x[k]-tmp
41+
x[k]+=tmp
42+
W*=unit
43+
n<<=1
44+
if inverse:
45+
for i in range(N):x[i]/=N
46+
47+
fft(a)
48+
fft(b)
49+
s = [i*j for i, j in zip(a,b)]
50+
fft(s,True)
51+
print(max(int(round(x.real)) for x in s))

python/plat2/11495.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import sys
2+
from collections import defaultdict,deque
3+
input = sys.stdin.readline
4+
5+
d = [(0,1),(0,-1),(1,0),(-1,0)]
6+
7+
T = int(input())
8+
while T:
9+
T-=1
10+
n, m = map(int,input().split())
11+
arr = []
12+
for i in range(n):
13+
arr+=[*map(int,input().split())]
14+
max_n = n*m+2
15+
cap = [[0]*max_n for _ in range(max_n)]
16+
fl = [[0]*max_n for _ in range(max_n)]
17+
adj = [[] for _ in range(max_n)]
18+
S = max_n-2;E = max_n-1
19+
for y in range(n):
20+
for x in range(m):
21+
now = y*m+x
22+
if (y%2) ^ (x%2):
23+
adj[now].append(E)
24+
adj[E].append(now)
25+
cap[now][E]=arr[now]
26+
else:
27+
adj[now].append(S)
28+
adj[S].append(now)
29+
cap[S][now]=arr[now]
30+
for dy,dx in d:
31+
ny,nx= y+dy,x+dx
32+
if 0<=ny<n and 0<=nx<m:
33+
to = ny*m+nx
34+
adj[now].append(to)
35+
adj[to].append(now)
36+
cap[now][to]=arr[now]
37+
ans = sum(arr)
38+
while True:
39+
visited = [-1]*max_n
40+
q = deque()
41+
q.append(S)
42+
while q and visited[E]==-1:
43+
n = q.popleft()
44+
for nx in adj[n]:
45+
if visited[nx]==-1 and cap[n][nx]>fl[n][nx]:
46+
visited[nx]=n
47+
q.append(nx)
48+
if nx==E:break
49+
if visited[E]==-1:break
50+
flow = 1e9
51+
now = E
52+
while now!=S:
53+
flow = min(flow,cap[visited[now]][now] - fl[visited[now]][now])
54+
now = visited[now]
55+
now = E
56+
while now!=S:
57+
fl[visited[now]][now]+=flow
58+
fl[now][visited[now]]-=flow
59+
now = visited[now]
60+
ans-=flow
61+
print(ans)
62+

0 commit comments

Comments
 (0)