結果

問題 No.1501 酔歩
ユーザー gew1fw
提出日時 2025-06-12 16:40:36
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,283 bytes
コンパイル時間 220 ms
コンパイル使用メモリ 82,364 KB
実行使用メモリ 84,504 KB
最終ジャッジ日時 2025-06-12 16:40:47
合計ジャッジ時間 5,567 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38 WA * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import math

def main():
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    K = int(input[idx])
    idx += 1
    A = list(map(int, input[idx:idx+N]))
    
    if K == 1:
        print(0)
        return
    if K == N:
        print(1)
        return
    
    A1 = A[0]
    A2 = A[1]
    
    sum_terms = [ (0, 1) ] * (N + 1)
    sum_terms[2] = (0, 1)
    
    for j in range(3, N+1):
        numerator = A1 * A2
        denominator = A[j-2] * A[j-1]
        
        a_prev, b_prev = sum_terms[j-1]
        new_num = a_prev * denominator + numerator * b_prev
        new_den = b_prev * denominator
        
        g = math.gcd(new_num, new_den)
        if g != 0:
            new_num //= g
            new_den //= g
        
        sum_terms[j] = (new_num, new_den)
    
    a, b = sum_terms[N]
    S_num = b + a
    S_den = b
    
    a_p, b_p = sum_terms[K]
    numerator_p = b_p + a_p
    denominator_p = b_p
    
    numerator = numerator_p * S_den
    denominator = denominator_p * S_num
    
    g = math.gcd(numerator, denominator)
    p = numerator // g
    q = denominator // g
    
    if q == 0:
        print(0)
    elif q == 1:
        print(p)
    else:
        print(f"{p}/{q}")

if __name__ == "__main__":
    main()
0