def test_all(f):
for i, data in enumerate(eval(f'test_data{f.__name__[0]}')):
exp = data["out"]
ans = f(*data["in"])
result = "AC" if exp == ans else "WA"
print(f"{i+1} {result}: expected: {exp}, output: {ans}")
def C(N, M, uv):
dsu = [-1] * (N + 1)
def leader(i):
if dsu[i] < 0: return i
dsu[i] = leader(dsu[i])
return dsu[i]
def merge(i, j):
'''サイズの大きい方(x)に小さい方(y)をマージ'''
if i == j: return
x = leader(i)
y = leader(j)
if x == y: return
if dsu[x] > dsu[y]: x, y = y, x
dsu[x] += dsu[y]
dsu[y] = x
for u, v in uv:
merge(u, v)
ans = 0
for v in dsu[1:]:
if v < 0: ans += 1
return anstest_all(C)
from collections import defaultdict
class DSU():
def __init__(self, n=None):
self.dsu = defaultdict(lambda: -1)
if n:
for i in range(n): self.dsu[i]
def leader(self, i):
if self.dsu[i] < 0: return i
self.dsu[i] = self.leader(self.dsu[i])
return self.dsu[i]
def merge(self, i, j):
'''サイズの大きい方(x)に小さい方(y)をマージ'''
if i == j: return
x = self.leader(i)
y = self.leader(j)
if x == y: return
if self.dsu[x] > self.dsu[y]: x, y = y, x
self.dsu[x] += self.dsu[y]
self.dsu[y] = x
def members(self, i):
l = self.leader(i)
return [k for k, v in self.dsu.items() if self.leader(k) == l]
def __len__(self):
return len([1 for v in self.dsu.values() if v < 0])
def C(N, M, uv):
dsu = DSU(N + 1)
for u, v in uv: dsu.merge(u, v)
return len(dsu) - 1
test_all(C)
MAX = int((9 * 10 ** 18) ** (1/3) + 1)
MAX = 100000 # ブラウザ環境用に上限設定
prime = [True] * (MAX + 1)
for i in range(2, MAX + 1):
if not i: continue
# prime[i * 2::i] = [False] * ((MAX - i * 2) // i + 1)
for j in range(i * 2, MAX + 1, i):
prime[j] = False
primes = [i for i in range(2, MAX + 1) if prime[i]]
def D(N):
for p in primes:
if N % p == 0:
break
a = N // p
if a % p == 0:
print(p, a // p)
else:
print(int(a ** (1/2)), p)D(2023) # 17 7
D(63) # 3 7
D(1059872604593911) # 104149 97711
import numpy as np
MAX = int((9 * 10 ** 18) ** (1/3) + 1)
MAX = 100000 # ブラウザ環境用に上限設定
prime = np.ones(MAX + 1, dtype=np.bool_)
for i in range(2, MAX + 1):
prime[i * 2::i] = False
primes = [i for i in range(2, MAX + 1) if prime[i]]
def D(N):
for p in primes:
if N % p == 0:
break
a = N // p
if a % p == 0:
print(p, a // p)
else:
print(int(a ** (1/2)), p)D(2023) # 17 7
D(63) # 3 7
D(1059872604593911) # 104149 97711
def z_algorithm(S):
z = [0] * len(S)
z[0] = len(S)
i = 1
j = 0
while i < len(S):
while i + j < len(S) and S[j] == S[i + j]: j += 1
z[i] = j
if j == 0:
i += 1
continue
k = 1
while i + k < len(S) and k + z[k] < j:
z[i + k] = z[k]
k += 1
i += k
j -= k
return zdef F(N, T):
a = z_algorithm(T[:N] + T[N:][::-1])
b = z_algorithm(T[N:][::-1] + T[:N])
a += [0]
for i in range(0, N):
if a[2 * N - i] >= i and b[N + i] >= N - i:
print(T[:i] + T[N + i:])
print(i)
break
else:
print(-1)F(3, "abcbac")
# abc
# 2F(4, "abababab")
# abab
# 1F(3, "agccga")
# cga
# 0F(4, "atcodeer")
# -1