結果

問題 No.187 中華風 (Hard)
ユーザー lam6er
提出日時 2025-03-20 21:09:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 111 ms / 3,000 ms
コード長 875 bytes
コンパイル時間 236 ms
コンパイル使用メモリ 82,356 KB
実行使用メモリ 76,064 KB
最終ジャッジ日時 2025-03-20 21:09:52
合計ジャッジ時間 3,153 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

n = int(input())
current_a = 0
current_m = 1
valid = True

for _ in range(n):
    x, y = map(int, input().split())
    if not valid:
        continue  # Skip processing once invalid
    
    d = x - current_a
    g = math.gcd(current_m, y)
    
    if d % g != 0:
        valid = False
        continue
    
    # Compute the new parameters
    m1 = current_m // g
    y1 = y // g
    d_div_g = d // g
    
    # Find the modular inverse of m1 mod y1
    try:
        inv = pow(m1, -1, y1)
    except ValueError:
        valid = False
        continue
    
    k0 = (d_div_g * inv) % y1
    new_a = current_a + current_m * k0
    new_m = current_m * y // g
    
    current_a = new_a
    current_m = new_m

if not valid:
    print(-1)
else:
    if current_a == 0:
        ans = current_m
    else:
        ans = current_a
    mod = 10**9 + 7
    print(ans % mod)
0