結果

問題 No.1584 Stones around Circle Pond
ユーザー lam6er
提出日時 2025-04-09 20:56:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 556 ms / 2,000 ms
コード長 2,913 bytes
コンパイル時間 180 ms
コンパイル使用メモリ 82,228 KB
実行使用メモリ 98,128 KB
最終ジャッジ日時 2025-04-09 20:57:57
合計ジャッジ時間 14,094 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 58
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx]); idx +=1
    L = int(input[idx]); idx +=1
    d = list(map(int, input[idx:idx+N]))
    idx +=N
    B = list(map(int, input[idx:idx+2*N]))
    
    # Step 1: Check all pairs B_i + B_{i+N} are equal
    S = B[0] + B[N]
    for i in range(1, N):
        current = B[i] + B[i+N]
        if current != S:
            print("No")
            return
    
    # Step 2: Check S is divisible by L
    if S % L != 0:
        print("No")
        return
    T = S // L
    
    # Step 3: Compute D_i = B_i - B_{i+N}
    D = [B[i] - B[i+N] for i in range(N)]
    
    # Step 4: Build matrix A and solve AY = D
    A = []
    for i in range(N):
        row = []
        di = d[i]
        for j in range(N):
            dj = d[j]
            aji = 2 * abs(dj - di) - L
            row.append(aji)
        A.append(row)
    
    # Solve AY = D using Gaussian elimination
    # Implemented with fractions for precision
    from fractions import Fraction
    
    M = [[Fraction(x) for x in row] + [Fraction(D[i])] for i, row in enumerate(A)]
    n = len(M)
    for col in range(n):
        # Find pivot
        pivot = -1
        for row in range(col, n):
            if M[row][col] != 0:
                pivot = row
                break
        if pivot == -1:
            print("No")
            return
        # Swap pivot to current row
        M[col], M[pivot] = M[pivot], M[col]
        # Normalize the pivot row
        factor = M[col][col]
        for j in range(col, n+1):
            M[col][j] /= factor
        # Eliminate other rows
        for i in range(n):
            if i == col or M[i][col] == 0:
                continue
            factor = M[i][col]
            for j in range(col, n+1):
                M[i][j] -= factor * M[col][j]
    
    # Check if the system is consistent
    Y = []
    for i in range(n):
        # Check if leading coefficient is 1 and other terms are 0
        expected = [0]*i + [1] + [0]*(n-i-1)
        row = M[i]
        if any(row[j] != expected[j] for j in range(n)):
            if row[n] != 0:
                print("No")
                return
            else:
                # System has free variables, need integer solution
                # This is complex to handle, assuming no solution here
                print("No")
                return
        Y.append(row[n])
    
    # Check if Y consists of integers
    y = []
    for yi in Y:
        if yi.denominator != 1:
            print("No")
            return
        y.append(yi.numerator)
    
    # Step 5: Check sum |y_j| <= T and T - sum is even and >=0
    sum_abs_y = sum(abs(yj) for yj in y)
    if sum_abs_y > T:
        print("No")
        return
    diff = T - sum_abs_y
    if diff < 0 or diff % 2 != 0:
        print("No")
        return
    
    print("Yes")

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