AtCoder Beginner Contest 456 参加記録と解答例 (A D問題)
Source: Dev.to
Problem A (Score: 100)
Statement
A 6‑faced die is rolled three times. Each face shows a number from 1 to 6.
Let (X) be an integer between 1 and 20.
The sum of the three dice ranges from 3 to 18. Print "Yes" if (X) lies within this range; otherwise print "No".
X = int(input())
if 3 <= X <= 18:
print("Yes")
else:
print("No")
Problem B (Score: 200)
Statement
Three dice are given. The (j)-th face of the (i)-th die shows an integer (A_{i,j}) ( 1 ≤ (A_{i,j}) ≤ 6 ). Each face appears with probability (\frac{1}{6}).
Compute the probability that the three dice show the numbers 4, 5, 6 in any order.
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A4 = A.count(4)
A5 = A.count(5)
A6 = A.count(6)
B4 = B.count(4)
B5 = B.count(5)
B6 = B.count(6)
C4 = C.count(4)
C5 = C.count(5)
C6 = C.count(6)
ans1 = A4 * B5 * C6 / 6**3
ans2 = A4 * B6 * C5 / 6**3
ans3 = A5 * B4 * C6 / 6**3
ans4 = A6 * B4 * C5 / 6**3
ans5 = A5 * B6 * C4 / 6**3
ans6 = A6 * B5 * C4 / 6**3
ans = ans1 + ans2 + ans3 + ans4 + ans5 + ans6
print(ans)
Problem C (Score: 300)
Statement
Given a string (S) consisting only of a, b, c ( (1 \le |S| \le 3 \times 10^5) ).
Count the number of substrings that contain no two consecutive identical characters. Output the count modulo (998244353).
S = list(input())
ans = 0
len_count = 0
prev_char = "x"
for i in range(len(S)):
if S[i] == prev_char:
len_count = 0
len_count += 1
ans += len_count
ans %= 998244353
prev_char = S[i]
print(ans)
Problem D (Score: 400)
Statement
Given a string (S) consisting only of a, b, c ( (1 \le |S| \le 3 \times 10^5) ).
Count the number of subsequences whose last character is a, b, or c. Use a DP‑like update while scanning the string, taking results modulo (998244353).
S = list(input())
A = 0
B = 0
C = 0
for i in range(len(S)):
if S[i] == "a":
A += 1 + B + C
A %= 998244353
if S[i] == "b":
B += 1 + A + C
B %= 998244353
if S[i] == "c":
C += 1 + A + B
C %= 998244353
ans = (A + B + C) % 998244353
print(ans)