链接: 深秋的苹果
# ms
def solve():
n, m = RI()
arr = RILST()
p = s = 0
for v in arr:
p += v*s
s += v
def ok(x):
cnt = 1
s = c =0
for v in arr:
c += v * s
s += v
if c > x:
c = 0
s = v
cnt += 1
if cnt > m:
return False
return True
print(lower_bound(0, p, key=ok))
# print(lower_bound(0, 2*10**18, key=ok))
链接: 鲜花之海
"""
x*(x+1)//2 <= k
x^2 + x - k*2<= 0
x <= (-1 +- sqrt(1 + 4 * k*2))/2
(n-1+n-1-x+1)*x//2 <= k
"""
# ms
def solve():
n, k = RI()
left = (1 + n - 1) * (n - 1) // 2
mid = n
if k <= left + mid:
x = int((-1 + sqrt(1 + 4 * k * 2)) / 2)
s = x * (x + 1) // 2
h = x + 1
if s == k:
print(h - 1, 1)
else:
h += 1
k -= s
print(k, h - k)
else:
k -= left + mid
# print(left,mid,k)
x = int((2 * n - 1 - sqrt((2 * n - 1) ** 2 - 4 * 2 * k)) / 2)
s = (n - 1 + n - 1 - x + 1) * x // 2
# print(x,s)
h = n + x + 1
if s == k:
print(n, h - n)
else:
h += 1
k -= s
# print(x,h,k)
print(2 + x + k - 1, h - (2 + x + k - 1))
链接: 斐波拉契跳跃
fib = [0, 1, 2]
while fib[-1] <= 10 ** 5:
fib.append(fib[-1] + fib[-2])
pfib = {v: i for i, v in enumerate(fib)}
# ms
def solve():
n, = RI()
a = RILST()
pos = [0] * n
for i, v in enumerate(a):
pos[v - 1] = i
@lru_cache(None)
def dfs(p, d): # 从p位置出发,上一步长d时,能否赢
for v in fib[pfib[d] + 1:]:
if v >= n: break
if p + v < n and a[p + v] > a[p]:
if not dfs(p + v, v): # 对方不能赢
return True
if p - v >= 0 and a[p - v] > a[p]:
if not dfs(p - v, v):
return True
return False
for st in range(n):
print(['Little Qiao', 'Little Lan'][dfs(st, 0)])
链接: 星石传送阵
class PrimeTable:
def __init__(self, n: int) -> None:
self.n = n
self.primes = primes = [] # 所有n以内的质数
self.min_div = min_div = [0] * (n + 1) # md[i]代表i的最小(质)因子
min_div[1] = 1
# 欧拉筛O(n),顺便求出min_div
for i in range(2, n + 1):
if not min_div[i]:
primes.append(i)
min_div[i] = i
for p in primes:
if i * p > n: break
min_div[i * p] = p
if i % p == 0:
break
def prime_factorization(self, x: int):
"""分解质因数,复杂度
1. 若x>n则需要从2模拟到sqrt(x),如果中间x降到n以下则走2;最坏情况,不含低于n的因数,则需要开方复杂度
2. 否则x质因数的个数,那么最多就是O(lgx)"""
n, min_div = self.n, self.min_div
for p in range(2, int(x ** 0.5) + 1):
if x <= n: break
if x % p == 0:
cnt = 0
while x % p == 0: cnt += 1; x //= p
yield p, cnt
while 1 < x <= n:
p, cnt = min_div[x], 0
while x % p == 0: cnt += 1; x //= p
yield p, cnt
if x >= n and x > 1:
yield x, 1
pt = PrimeTable(10 ** 6)
def solve():
n, a, b = RI()
arr = RILST()
g = [[] for _ in range(2 * n + 3)]
for i, v in enumerate(arr, start=1):
f = sum(x * y for x, y in pt.prime_factorization(v)) % n + 1
ff = f + n + 1 # 跳到虚拟节点,边权0.5,实际处理边权全部乘2
g[ff].append((i, 1))
g[i].append((ff, 1))
if f <= n: # 互跳节点
g[i].append((f, 2))
g[f].append((i, 2))
q = [(0, a)]
dis = [inf] * (2 * n + 3)
dis[a] = 0
while q:
# print(q)
c, u = heappop(q)
if c > dis[u]: continue
if u == b:
return print(c // 2)
for v, w in g[u]:
if c + w < dis[v]:
dis[v] = c + w
heappush(q, (c + w, v))
print(-1)