結果
| 問題 |
No.1501 酔歩
|
| コンテスト | |
| ユーザー |
qwewe
|
| 提出日時 | 2025-05-14 13:19:19 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 205 ms / 2,000 ms |
| コード長 | 5,509 bytes |
| コンパイル時間 | 182 ms |
| コンパイル使用メモリ | 82,836 KB |
| 実行使用メモリ | 101,936 KB |
| 最終ジャッジ日時 | 2025-05-14 13:20:23 |
| 合計ジャッジ時間 | 12,999 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 53 |
ソースコード
import sys
# Import the Fraction class for precise rational number arithmetic
from fractions import Fraction
def solve():
# Read N (number of squares) and K (starting square) from input
N, K = map(int, sys.stdin.readline().split())
# Handle the edge case where there is only one square (N=1).
if N == 1:
# If N=1, K must also be 1. The piece starts at square 1, which is also square N.
# It has already reached N. The probability is 1.
print("1/1")
return
# Read the array A containing integers for each square.
# This line is executed only if N > 1.
# The problem guarantees N integers are provided.
A = list(map(int, sys.stdin.readline().split()))
# Handle the trivial cases where the piece starts at an absorbing square (1 or N).
if K == 1:
# If the piece starts at square 1, it is absorbed immediately.
# It cannot reach square N (unless N=1, which was handled above).
# The probability of reaching N is 0.
print("0")
return
if K == N:
# If the piece starts at square N, it is absorbed immediately.
# The probability of reaching N is 1.
print("1/1")
return
# The general case: the piece starts at square K where 1 < K < N, and N >= 2.
# Use 0-based indexing for the list A for convenience in Python.
# A_list[i] corresponds to the value A_{i+1} in the problem statement.
A_list = A
# Initialize the running sum for calculating the denominator of the final probability.
# Use Fraction for precise arithmetic with rational numbers.
current_sum = Fraction(0)
# Initialize sum_K, which will store the partial sum needed for the numerator.
sum_K = Fraction(0)
# Check if A_list has at least 2 elements. Since N >= 2 in this code path,
# len(A_list) is N >= 2, so this check is implicitly satisfied.
# However, for robust code, consider explicitly checking N >= 2 if N=1 case wasn't handled.
# Ensure A_list has elements at index 0 and 1.
if len(A_list) < 2:
# This block should technically not be reached due to N>=2 condition.
# If it were possible, an error or default handling would be needed.
# For this problem's constraints, it's safe to assume len(A_list) >= 2.
pass
# Precompute the numerator term A_1 * A_2.
# A_1 is A_list[0], A_2 is A_list[1].
# Constraints state A_i >= 1, so A_list[0] and A_list[1] are valid indices and values are >= 1.
A1A2_num = A_list[0] * A_list[1]
# Calculate the first term in the sum, C'_2.
# The formula for C'_i is (A_1 * A_2) / (A_{i-1} * A_i).
# For i=2, C'_2 = (A_1 * A_2) / (A_1 * A_2).
# Since A_i >= 1, A_1 * A_2 >= 1, so C'_2 is always 1.
C_prime_2 = Fraction(1)
# Add C'_2 to the running sum.
current_sum += C_prime_2
# If the starting position K is 2, the sum required for the numerator (sum_K) is just C'_2.
if K == 2:
sum_K = current_sum
# Iteratively calculate the remaining terms C'_i for i from 3 to N.
for i in range(3, N + 1):
# Calculate the denominator term A_{i-1} * A_i.
# Using 0-based indexing: A_list[i-2] * A_list[i-1].
# Check if indices i-2 and i-1 are valid for A_list. Given the loop range and N >= 2, these indices are valid.
if i-2 >= len(A_list) or i-1 >= len(A_list):
# This situation implies N doesn't match the length of A, or an off-by-one error.
# Given problem constraints, this check is mainly defensive.
pass
Ai_minus_1 = A_list[i-2]
Ai = A_list[i-1]
# Calculate the value of the denominator. A_i >= 1 guarantees denom_val >= 1.
denom_val = Ai_minus_1 * Ai
# Check for division by zero, although constraints A_i >= 1 make this impossible.
if denom_val == 0:
# This case is impossible under problem constraints.
# If it could happen, proper error handling would be needed.
pass
# Compute C'_i = (A_1 * A_2) / (A_{i-1} * A_i) using Fraction.
# Pass integer numerator and denominator to the Fraction constructor for accuracy.
C_prime_i = Fraction(A1A2_num, denom_val)
# Add the computed term C'_i to the running sum.
current_sum += C_prime_i
# If the current index i matches the starting position K,
# store the current partial sum. This value represents \sum_{j=2}^K C'_j.
if i == K:
sum_K = current_sum
# After the loop completes, current_sum holds the total sum \sum_{j=2}^N C'_j.
sum_N = current_sum
# Check if the total sum sum_N is zero. This check is mostly defensive,
# as sum_N >= C'_2 = 1 because all terms C'_i are non-negative (actually positive due to A_i >= 1).
if sum_N == 0:
# This case indicates an issue, likely impossible under constraints.
pass # It's guaranteed sum_N >= 1.
# Calculate the final probability p_K = sum_K / sum_N.
# sum_K represents \sum_{j=2}^K C'_j and sum_N represents \sum_{j=2}^N C'_j.
final_prob = sum_K / sum_N
# Print the final probability as an irreducible fraction p/q.
# The Fraction class automatically simplifies the fraction and ensures the denominator is positive.
print(f"{final_prob.numerator}/{final_prob.denominator}")
# Execute the solve function to run the program logic
solve()
qwewe