結果

問題 No.1096 Range Sums
ユーザー ThetaTheta
提出日時 2022-10-20 13:32:19
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 1,169 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 10,760 KB
実行使用メモリ 23,568 KB
最終ジャッジ日時 2023-09-12 18:55:47
合計ジャッジ時間 2,111 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,820 KB
testcase_01 AC 15 ms
7,824 KB
testcase_02 AC 15 ms
7,780 KB
testcase_03 AC 14 ms
7,828 KB
testcase_04 AC 14 ms
7,832 KB
testcase_05 AC 15 ms
7,832 KB
testcase_06 AC 14 ms
7,824 KB
testcase_07 AC 15 ms
7,824 KB
testcase_08 AC 15 ms
7,880 KB
testcase_09 AC 15 ms
7,896 KB
testcase_10 AC 130 ms
23,540 KB
testcase_11 AC 128 ms
23,524 KB
testcase_12 AC 129 ms
23,552 KB
testcase_13 AC 127 ms
23,480 KB
testcase_14 AC 126 ms
23,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def accumulate_sum(start: int, end: int) -> int:
    if start > end:
        return 0
    return (end * (end + 1) - start * (start - 1)) // 2


def main():
    N = int(input())
    A = list(map(int, input().split()))

    if N == 1:
        print(A[0])
        return

    coefficients = []
    if N % 2 == 0:
        for t in range(1, N//2 + 1):
            coefficients.append(2*(N//2 - t)*t + 2*accumulate_sum(1, t))

        coeff_rev = reversed(coefficients)
        coefficients.extend(coeff_rev)
    else:
        for t in range(1, (N+1)//2):
            coefficients.append(t*(N - 2*(t-1)) + 2*accumulate_sum(1, t-1))
        coeff_rev = reversed(coefficients)
        coefficients.append((N+1)//2 + 2*accumulate_sum(1, (N+1)//2 - 1))
        coefficients.extend(coeff_rev)

    print(sum(A_elm * coeff for A_elm, coeff in zip(A, coefficients)))
    # Nが奇数 (N+1)//2=nとすると
    # 中央 n*1 + 2*sum(1, n-1)
    # 一つ横 (n-1)*3 + 2*sum(1, n-2)
    # 二つ横 (n-2)*5 + 2*sum(1,n-3)


    # Nが偶数 N//2=nとすると
    # 中央
    # 中央2つ n*0 + 2*sum(1,n)
    # 一つ横  (n-1)*2 + 2*sum(1,n-1)
if __name__ == "__main__":
    main()
0