wtc挑战赛#10正经全py题解()
2024-09-30 20:02:54
发布于:湖南
t1题解(非常简单,懂这仨符号的应该都能AK):
a, b = map(int, input().split())
c = a & b
d = a | b
e = a ^ b
result = c + d + e
print(result)
t2题解:
def min_steps(x, k):
steps = 0
current = 0
while current < x:
for i in range(x - current, 0, -1):
if i % k != 0:
current += i
steps += 1
break
return steps
def main():
import sys
input = sys.stdin.read
data = input().split()
t = int(data[0])
results = []
index = 1
for _ in range(t):
x = int(data[index])
k = int(data[index + 1])
results.append(min_steps(x, k))
index += 2
for result in results:
print(result)
if __name__ == "__main__":
main()
t3题解:
def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
def is_prime_power(n):
if n == 1:
return True
for p in range(2, int(n**0.5) + 1):
if is_prime(p):
power = p
while power < n:
power *= p
if power == n:
return True
return False
def main():
t = int(input())
results = []
for _ in range(t):
n = int(input())
if n == 1 or is_prime(n) or is_prime_power(n):
results.append("No")
else:
results.append("Yes")
for result in results:
print(result)
if __name__ == "__main__":
main()
t4题解:
MOD = 998244353
def factorial(n):
result = 1
for i in range(1, n + 1):
result = (result * i) % MOD
return result
def solve(n, a):
a.sort()
result = 1
for i in range(n):
result = (result * (a[i] - i)) % MOD
return result
def main():
import sys
input = sys.stdin.read
data = input().split()
T = int(data[0])
index = 1
results = []
for _ in range(T):
n = int(data[index])
index += 1
a = list(map(int, data[index:index + n]))
index += n
results.append(solve(n, a))
for result in results:
print(result)
if __name__ == "__main__":
main()
t5题解:
def main():
import sys
input = sys.stdin.read
data = input().split()
n = int(data[0])
k = int(data[1])
times = list(map(int, data[2:2+n]))
end_times = [times[i] + 1 for i in range(n)]
if k >= n:
print(n)
return
gaps = [times[i + 1] - end_times[i] for i in range(n - 1)]
gaps.sort()
total_time = end_times[-1] - times[0]
for i in range(k - 1):
total_time -= gaps[-(i + 1)]
print(total_time)
if __name__ == "__main__":
main()
t6题解:(呃呃啊啊只能AC6个测试点www我是入机过不了但还是发下吧())
from collections import defaultdict
import sys
class BIT:
def __init__(self, n):
self.n = n
self.w = [0] * (n + 1)
def add(self, x, k, mod=998244353):
k %= mod
k += mod
k %= mod
while x <= self.n:
self.w[x] += k
self.w[x] %= mod
x += x & -x
def update_range(self, x, y, k, mod=998244353):
k %= mod
k += mod
k %= mod
self.add(x, k, mod)
self.add(y + 1, -k, mod)
def query(self, x, mod=998244353):
ans = 0
while x > 0:
ans += self.w[x]
ans %= mod
x -= x & -x
return ans
def qpow(x, y, mod=998244353):
x %= mod
x += mod
x %= mod
if y == 0:
return 1
if y < 0:
return qpow(qpow(x, -y, mod), mod - 2, mod)
ans = 1
while y:
if y & 1:
ans *= x
ans %= mod
x *= x
x %= mod
y >>= 1
return ans
def dfs(u, fa, d, sz, dfn, cnt, to):
d[u] = d[fa] + 1
sz[u] = 1
dfn[u] = cnt[0]
cnt[0] += 1
for v in to[u]:
if v == fa:
continue
dfs(v, u, d, sz, dfn, cnt, to)
sz[u] += sz[v]
def main():
input = sys.stdin.read
data = input().split()
idx = 0
n, m, a = map(int, data[idx:idx+3])
idx += 3
Q = BIT(n + 1)
to = defaultdict(list)
d = [-1] + [0] * n
sz = [0] * (n + 1)
dfn = [0] * (n + 1)
cnt = [1]
for _ in range(n - 1):
u, v = map(int, data[idx:idx+2])
idx += 2
to[u].append(v)
to[v].append(u)
dfs(1, 0, d, sz, dfn, cnt, to)
for _ in range(m):
opt, v = map(int, data[idx:idx+2])
idx += 2
if opt == 1:
x = int(data[idx])
idx += 1
Q.update_range(dfn[v], dfn[v] + sz[v] - 1, qpow(a, -d[v] + x))
else:
print((Q.query(dfn[v]) * qpow(a, d[v]) % 998244353 + 998244353) % 998244353)
if __name__ == "__main__":
main()
全部评论 5
顶
2024-10-01 来自 广东
1顶
2024-10-01 来自 湖南
0顶
2024-10-01 来自 湖南
0顶
2024-10-01 来自 湖南
0有注释就更好了(
2024-10-01 来自 广东
0这好办,让GPT帮忙写注释()
2024-10-01 来自 湖南
0
有帮助,赞一个