C - Remainder Minimization 2019
如果LR相差在2019内,那么遍历LR
否则可以视作为1…2019的遍历
D - Rain Flows into Dams
设解为
M
1
,
M
2
.
.
.
M
n
M_1,M_2...M_n
M1,M2...Mn
∵
M
1
+
M
2
=
2
A
1
\because M_1+M_2=2A_1
∵M1+M2=2A1
M
2
+
M
3
=
2
A
2
M_2+M_3=2A_2
M2+M3=2A2
.
.
.
...
...
左边相加可以得到
M
1
+
.
.
.
+
M
n
=
A
1
+
.
.
.
+
A
n
M_1+...+M_n=A_1+...+A_n
M1+...+Mn=A1+...+An
M的和即可求出。
剩下由于M长度为奇数,可以凑出
M
1
+
M
2
+
.
.
.
+
M
n
−
1
M_1+M_2+...+M_{n-1}
M1+M2+...+Mn−1
即求得
M
n
M_n
Mn
接着从后往前计算M的各项。
# -*- 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 = int(fp.readline())
a = list(map(int, fp.readline().split()))
sb = sum(a)
eb = 0
for i in range(0, n - 1, 2):
eb += a[i] * 2
b = [0] * n
b[-1] = sb - eb
for i in range(n - 2, -1, -1):
b[i] = 2 * a[i] - b[i + 1]
print(*b)
if __name__ == "__main__":
main()
E - Virus Tree 2
普通的乘法原理,画图想一想
根节点可以乘K
接下来的第一层子节点,第一个可以乘K-1,第二个乘K-2,依次递减
第二层子节点,第一个点可以乘K-2,第二个乘K-3,依次递减
之后的子节点和第二层相同
# -*- 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
mod = 10 ** 9 + 7
n, m = map(int, fp.readline().split())
g = [[] for _ in range(n + 1)]
for i in range(n - 1):
u, v = map(int, fp.readline().split())
g[u].append(v)
g[v].append(u)
ans = 1
def go(cur, fa):
nonlocal ans
if fa == -1:
t = m - 1
else:
t = m - 2
for child in g[cur]:
if fa == child:
continue
ans = ans * t % mod
t -= 1
for child in g[cur]:
if fa == child:
continue
go(child, cur)
go(1, -1)
ans = ans * m % mod
print(ans)
if __name__ == "__main__":
main()