C Green Bin
map计数
D Summer Vacation
想贪心,但没贪成功
应该从后往前考虑,按天计算,维护一个当前可以取到的最大堆
# -*- coding: utf-8 -*-
# @time : 2023/6/2 13:30
# @author : yhdu@tongwoo.cn
# @desc :
# @file : atcoder.py
# @software : PyCharm
import bisect
import copy
import sys
from sortedcontainers import SortedList
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(100010)
def main():
items = sys.version.split()
if items[0] == '3.10.6':
fp = open("in.txt")
else:
fp = sys.stdin
n, k = map(int, fp.readline().split())
items = []
for i in range(n):
a, b = map(int, fp.readline().split())
items.append([a, b])
# 从截止日期小到大排序
# 例如当前的截止日期是1天,那么把1天内可以运行完的都放入堆中
items.sort(key=lambda x: x[0])
qu = []
i = 0
vis = [0] * n
ans = 0
for day in range(1, k + 1):
while i < n and items[i][0] <= day:
heapq.heappush(qu, (-items[i][1], i))
i += 1
# lazy更新,每一个item进出队列一次
while len(qu) > 0:
top = heapq.heappop(qu)
v, idx = top
if vis[idx] == 0:
v = -v
ans += v
vis[idx] = 1
break
print(ans)
if __name__ == "__main__":
main()
E Coins Respawn
很不错的图论水 题
首先易见每条边都减一次cost。
由于有环,只需要用bellman-ford计算最短路
问题是如何判断松弛点对于最后的结果有影响。答案是该松弛点应该在从1到N的路径上。
因此用两个dfs判断每个点是否会被1访问,被N反向访问。
# -*- coding: utf-8 -*-
# @time : 2023/6/2 13:30
# @author : yhdu@tongwoo.cn
# @desc :
# @file : atcoder.py
# @software : PyCharm
import bisect
import copy
import sys
from sortedcontainers import SortedList
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(100010)
def main():
items = sys.version.split()
if items[0] == '3.10.6':
fp = open("in.txt")
else:
fp = sys.stdin
n, m, p = map(int, fp.readline().split())
g = []
wg = [[] for _ in range(n)]
rg = [[] for _ in range(n)]
dist = [-10 ** 10] * n
dist[0] = 0
reach_n = [0] * n
reach_1 = [0] * n
def dfs(node):
reach_n[node] = 1
for nxt in rg[node]:
if reach_n[nxt] == 0:
dfs(nxt)
def dfs1(node):
reach_1[node] = 1
for nxt in wg[node]:
if reach_1[nxt] == 0:
dfs1(nxt)
for i in range(m):
u, v, w = map(int, fp.readline().split())
u, v, w = u - 1, v - 1, w - p
g.append([u, v, w])
rg[v].append(u)
wg[u].append(v)
dfs1(0)
dfs(n - 1)
for _ in range(n - 1):
for i in range(m):
u, v, w = g[i]
if dist[v] < dist[u] + w:
dist[v] = dist[u] + w
change = 0
for i in range(m):
u, v, w = g[i]
if dist[v] < dist[u] + w and reach_n[v] and reach_1[v]:
change = 1
if change:
print(-1)
else:
if dist[n - 1] < 0:
print(0)
else:
print(dist[n - 1])
if __name__ == "__main__":
main()
F - Polynomial Construction
费尔马小定理的灵活应用。但是我不会。
已知
a
p
−
1
≡
1
(
m
o
d
p
)
a^{p-1}\equiv1 (mod \space p)
ap−1≡1(mod p)
则对于任意
x
,
p
x,p
x,p,
有
1
−
(
x
−
d
)
p
−
1
≡
1
x
=
d
1-(x-d)^{p-1} \equiv 1 \space x=d
1−(x−d)p−1≡1 x=d
与
1
−
(
x
−
d
)
p
−
1
≡
0
x
≠
d
1-(x-d)^{p-1} \equiv 0 \space x \neq d
1−(x−d)p−1≡0 x=d
记上述式子为
f
(
x
,
d
)
f(x,d)
f(x,d)
该式的意义在于,假如我们构造一个多项式
F
(
x
)
=
∑
d
=
1
p
−
1
f
(
x
,
d
)
F(x)=\sum_{d=1}^{p-1}f(x,d)
F(x)=d=1∑p−1f(x,d)只有当x取到d时,多项式
f
(
x
,
d
)
f(x,d)
f(x,d)才模1,其余情况下多项式
f
(
x
,
d
)
f(x,d)
f(x,d)均为0,也就是整个
F
(
x
)
F(x)
F(x)和为1。
所以我们遍历
a
[
i
]
a[i]
a[i],当
a
[
i
]
=
1
a[i]=1
a[i]=1时,将
f
(
x
,
d
)
f(x,d)
f(x,d)加入到多项式中。
# -*- coding: utf-8 -*-
# @time : 2023/6/2 13:30
# @author : yhdu@tongwoo.cn
# @desc :
# @file : atcoder.py
# @software : PyCharm
import bisect
import copy
import sys
from sortedcontainers import SortedList
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(100010)
def main():
items = sys.version.split()
if items[0] == '3.10.6':
fp = open("in.txt")
else:
fp = sys.stdin
p = int(fp.readline())
a = list(map(int, fp.readline().split()))
b = [0] * p
cmb = [[0] * (p + 1) for _ in range(p + 1)]
for j in range(p + 1):
cmb[0][j] = 1
for i in range(p + 1):
cmb[i][0] = 1
for j in range(1, p + 1):
cmb[i][j] = (cmb[i - 1][j - 1] + cmb[i - 1][j]) % p
for i in range(p):
if a[i] == 1:
np = 1
for j in range(p):
t = -cmb[p - 1][j] * np
b[p - 1 - j] = (b[p - 1 - j] + t) % p
np = (np * -i) % p
b[0] = (b[0] + 1) % p
print(*b)
if __name__ == "__main__":
main()