結果
問題 | No.1096 Range Sums |
ユーザー |
|
提出日時 | 2022-10-20 13:32:19 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 165 ms / 2,000 ms |
コード長 | 1,169 bytes |
コンパイル時間 | 94 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 23,288 KB |
最終ジャッジ日時 | 2024-06-30 06:36:39 |
合計ジャッジ時間 | 1,918 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
def accumulate_sum(start: int, end: int) -> int:if start > end:return 0return (end * (end + 1) - start * (start - 1)) // 2def main():N = int(input())A = list(map(int, input().split()))if N == 1:print(A[0])returncoefficients = []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()